LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 02-02-2004, 05:49 PM   #1
balanagireddy
Member
 
Registered: Sep 2003
Location: India
Distribution: redhat linux 9.0
Posts: 46

Rep: Reputation: 15
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
 
Old 02-02-2004, 05:54 PM   #2
jinksys
Member
 
Registered: Aug 2003
Location: 63123
Distribution: OpenSuSE/Ubuntu
Posts: 418

Rep: Reputation: 31
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?
 
Old 02-02-2004, 05:55 PM   #3
balanagireddy
Member
 
Registered: Sep 2003
Location: India
Distribution: redhat linux 9.0
Posts: 46

Original Poster
Rep: Reputation: 15
Plz run this and tell me what this does.
I am not getting the output correctly.
 
Old 02-02-2004, 06:03 PM   #4
teval
Member
 
Registered: Jul 2003
Location: Toronto, Canada
Distribution: Gentoo
Posts: 720

Rep: Reputation: 30
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
 
Old 02-03-2004, 10:27 AM   #5
shishir
Member
 
Registered: Jul 2003
Location: bangalore . india
Distribution: openSUSE 10.3
Posts: 251

Rep: Reputation: 33
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..
 
Old 02-03-2004, 01:11 PM   #6
cjcuk
Member
 
Registered: Dec 2003
Distribution: Openwall, ~LFS
Posts: 128

Rep: Reputation: 15
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
plz plz help me regarding route mapping nedian123 Programming 1 07-13-2004 09:17 AM
plz plz solve my route mapping problem nedian123 Linux - Networking 1 07-12-2004 10:41 PM
[debug]what does the following debug information mean icoming Programming 21 06-08-2004 03:13 AM
Plz debug this... balanagireddy Linux - Newbie 3 02-03-2004 05:17 PM
redhat problems plz plz help sap666 Linux - Newbie 5 07-30-2003 11:57 AM


All times are GMT -5. The time now is 12:08 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration