LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Plz debug this (https://www.linuxquestions.org/questions/programming-9/plz-debug-this-141526/)

balanagireddy 02-02-2004 04:49 PM

Plz debug this
 
Plz debug this...

Can neone plz debug the below program ...
Plz dont mind .I just started programming ....

Here...
#include<stdio.h>

//Program to search occurence of a particular character in an array
int main(void)
{
char a[20];
int n,i,count;
char c;
printf("Enter the no of the characters in the array\n");
scanf("%d",&n);
printf("Enter the characters into the array\n");
for(i=0;i<n;i++)
{
scanf("%c",&a[i]);
}
printf("Enter the character u want to search\n");
scanf("%c",&c);
for(i=0;i<n;i++)
{
count=1;
if(a[i]==c)
{
printf("%d's occurence at %d\n",count,i);
count++;
}
}
return 0;
}
Can neone suggest me a good site for learning C??

Thx for reading....

__________________
Bala Nagi Reddy

jinksys 02-02-2004 04:54 PM

You say debug this, as in something is wrong with it?

Im not near a compiler at the moment but I didnt see
any programmical errors.

OR, did you want us to tell you what it does?

balanagireddy 02-02-2004 04:55 PM

Plz run this and tell me what this does.
I am not getting the output correctly.

teval 02-02-2004 05:03 PM

First :) put code inside [ code] [/ code] tages (without the space inside) to make it look preety.

Code:

int main(void)
{
      char a[20];
      int n , i , count;
      char c;
      printf( "Enter the no of the characters in the array\n" );
      scanf( "%d" , &n );
      printf("Enter the characters into the array\n" );
      for( i = 0 ; i < n ; i++ )
      {
              scanf( "%c" , &a[i] );
      }
      printf( "Enter the character u want to search\n" );
      scanf("%c",&c);
      for(i=0;i<n;i++)
      {
              count=1;
              if(a[i]==c)
              {
                    printf("%d's occurence at %d\n",count,i);
                    count++;
              }
      }
      return 0;
}

Now.. that helps :)

Ok.. so 2nd of all, whenever you have a code problem, please.. please :) tell people specifically what's worng.
"Umm.. it doesn't work" helps noone.
"It doesn't access a certain index of an arry with input blah" helps everyone greately

So.. tell us what's wrong? I see a few obvious parts.. but tell us your take?

Oh.. and use gdb. Install it.. love it :)

shishir 02-03-2004 09:27 AM

try reaing how scanf can create problems :
the \n's in the previos lines cause them to be taken as inputs by the succeeding scanf's,
that is why it is a good idea to capture any stray \n's by using scanf in this way :
scanf("\n%c",&c);

so now you know what you should..
another thing..oft repeated...scanf usage is highly discouraged...
try using getchar for getting a character from stdin...or getc..

cjcuk 02-03-2004 12:11 PM

Another pointer, if you are going to statically allocate your array (as you have done with the character array `a') do not then take an unlimited dynamic value as the maximum indice. There are two straight-forward solutions: dynamically allocate the array to the requested length (read malloc(3)), or check that `n' <= 20 and if it is not set it as 20. At the moment it is possible to write out of bounds by specifying a number larger than 20 -- as well as munging the stack/heap (stack in this case) this can represent a security issue, and even where it does not the side affects of the munging can be annoying (eg, crashes and other unexpected behaviour).

Also, you need to move the initialisation of count to outside that for loop, otherwise it will become ``1'' at the beginning of each iteration. This means if the 4th printf() is called that it will always say ``1's occurance at ...''. When you look at an iteration, you should unroll the loop a few times in your head to make sure you have done what you intended -- this is not rigorous testing, but it will catch simple mistakes like that.


All times are GMT -5. The time now is 10:14 AM.