简体   繁体   中英

testcase not running in c program

iam new to c program and facing difficulty in debugging programs.In the below code test case 2 is not running.I have found that the error is in reading interger n in the second test case.someone please hep me with this issue.Also please recommend me with some tools that can be ued for debugging c programs using terminal.Thanks for help

#include <stdio.h>
#include <stdlib.h>

int read(){
   int r = 0;
   char c = getchar_unlocked();
   while(c >= '0' && c <= '9'){
      r = r*10 + c - 48 ;
      c = getchar_unlocked();
   }
   return r;
}

void main(){
   int t = 0;
   t = read();
   int rr = 0;
   for(rr = 0;rr < t;rr++){
      int i,n = 0;
      n = read();
      int *p = (int *)calloc(n,sizeof(int));
      for(i = 0;i < n;++i){
         *(p+i) = getchar_unlocked() - 48;
      }
      int no,nz = 0;
      for(i = 0;i < n;++i){
         if(*(p+i) == 0){nz += 1;}
         if(*(p+i) == 1){no += 1;}
      }
      int k = 0;
      if(((no)%2 == 0) && ((nz)%2) == 0){
         k = -1;
      }
      if(((no)%2 == 0) && ((nz)%2) == 1){
         k = 0;
      }
      if(((no)%2 == 1) && ((nz)%2) == 0){
         k = 1;
      }
      if(((no)%2 == 1) && ((nz)%2) == 1){
         k = 1;
      }
      int result = 0;printf("%d\n",5556);
      if(k == 1){
         for(i = 0;i < n;++i){
            if(*(p+i) == 1){
               result = i+1 ;
               break;
            }
         }

      }
      if(k == 0){
         for(i = 0;i < n;++i){
            if(*(p+i) == 0){
               result = i+1 ;
               break;
            }
         }
      }
      printf("%d\n",result);
   }
}

Your strategy to read an integer is flawed. You don't have the logic to skip whitespaces. I would change the function name to read_int and change its implementation to

int read(){
   int n;
   if ( scanf("%d", &n) != 1 )
   {
       // Deal with the error
   }
   return n;
}

Also, change

*(p+i) = getchar_unlocked() - 48;

to

*(p+i) = read_int();

or a more intuitive version:

p[i] = read_int();

With those changes, I am able to read and process the numbers. But I still get the wrong output. I'll let you figure the logic error in your code.

Additional Comments

main is expected to return an int . If your compiler didn't complain about that, it's time to up the warning level. I use -Wall by default.

When you are in the process of debugging your code, it's always good to test the code that reads the input to make sure that there is no error in reading the input.

Here's what I did to your code:

#include <stdio.h>
#include <stdlib.h>

int read_int(){
   int n;
   if ( scanf("%d", &n) != 1 )
   {
      // Deal with the error.
   }
   return n;
}

int main(){
   int t = 0;
   int rr = 0;

   t = read_int();
   printf("t = %d\n", t);

   for(rr = 0;rr < t;rr++){
      int i,n = 0;

      n = read_int();
      printf("n = %d\n", n);

      int *p = (int *)calloc(n,sizeof(int));
      for(i = 0;i < n;++i){
         p[i] = read_int();
         printf("p[%d] = %d\n", i, p[i]);
      }

      int no,nz = 0;
      for(i = 0;i < n;++i){
         if(*(p+i) == 0){nz += 1;}
         if(*(p+i) == 1){no += 1;}
      }

      int k = 0;
      if(((no)%2 == 0) && ((nz)%2) == 0){
         k = -1;
      }
      if(((no)%2 == 0) && ((nz)%2) == 1){
         k = 0;
      }
      if(((no)%2 == 1) && ((nz)%2) == 0){
         k = 1;
      }
      if(((no)%2 == 1) && ((nz)%2) == 1){
         k = 1;
      }

      int result = 0;
      // printf("%d\n",5556);
      if(k == 1){
         for(i = 0;i < n;++i){
            if(*(p+i) == 1){
               result = i+1 ;
               break;
            }
         }

      }

      if(k == 0){
         for(i = 0;i < n;++i){
            if(*(p+i) == 0){
               result = i+1 ;
               break;
            }
         }
      }
      printf("%d\n",result);
   }

   return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM