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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
02-02-2004, 04:49 PM
|
#1
|
Member
Registered: Sep 2003
Location: India
Distribution: redhat linux 9.0
Posts: 46
Rep:
|
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
|
|
|
02-02-2004, 04:54 PM
|
#2
|
Member
Registered: Aug 2003
Location: 63123
Distribution: OpenSuSE/Ubuntu
Posts: 419
Rep:
|
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?
|
|
|
02-02-2004, 04:55 PM
|
#3
|
Member
Registered: Sep 2003
Location: India
Distribution: redhat linux 9.0
Posts: 46
Original Poster
Rep:
|
Plz run this and tell me what this does.
I am not getting the output correctly.
|
|
|
02-02-2004, 05:03 PM
|
#4
|
Member
Registered: Jul 2003
Location: Toronto, Canada
Distribution: Gentoo
Posts: 720
Rep:
|
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 
|
|
|
02-03-2004, 09:27 AM
|
#5
|
Member
Registered: Jul 2003
Location: bangalore . india
Distribution: openSUSE 10.3
Posts: 251
Rep:
|
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..
|
|
|
02-03-2004, 12:11 PM
|
#6
|
Member
Registered: Dec 2003
Distribution: Openwall, ~LFS
Posts: 128
Rep:
|
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 08:17 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|