You should print the answer after the search .So you should move the printf out of the loop,because you want it to be executed only once
You should print the answer after the search .So you should move the printf out of the loop,because you want it to be executed only once
Please fix the indentation of your code and post it again. In doing so, you may discover how to fix this problem that you're facing by yourself
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
tnx for advice
tnx for helping me!!! laserlight and std10093
Code:/* Input 10 numbers in an array and count the number of occurence of an element inside the array */ #include<stdio.h> #include<conio.h> void main() { clrscr(); int a[10],i,n,count=0; for(i=0;i<10;i++) { printf("\n\t Enter element a[%d] : ",i); scanf("%d",&a[i]); } printf("\n\n Enter the element you want to count : "); scanf("%d",&n); for(i=0;i<10;i++) { if(n==a[i]) count++; } printf("\n\n %d occurs %d times in your array ",n,count); getch(); }
Last edited by somali.cc; 10-08-2012 at 10:23 AM.
I would have to say no, your code doesn't work for me. First several members of this forum have been telling the OP to properly format the code, so when you post poorly formatted code you to are not following their advice. I recommend you also learn to properly format your code it will make reading your code much easier. Second you change int main() to void main() which is also wrong main should never be defined to return a void, except in very rare circumstances. Lastly providing a complete solution you rob the student of the joy that comes from actually finding their own problems. Along with the fact that they will learn much less.Will this work for you ?
Jim
Hey Marvin,
You don't want to print "Enter a number" without giving more information. Maybe "Enter 10 numbers that will be searched through". Then "Enter a number to search for: ". Just something to give the user a chance at entering the right info - because otherwise, they won't do it.
How do you want the display answer?
And I would have to say pls dont try to discourage ppl who want to share and learn C-programming with such out-of-context comments...I have just provided the code that I had felt might work for the thread-starter...So, allow them to reply back with their opinion instead of showing-off ur ' I am the ultimate' attitude... There r more forums for that. We signed in here to learn and discuss C-programming , not to lambast others..
Many of jimblumberg's comments critiqued your code. That is not out of context.Originally Posted by somali.cc
As jimblumberg noted, please don't do that until it is clear that the thread starter has a working solution, in which case you would be providing an alternative solution for discussion. Thus, your code example would be excellently posted after Marvin Flores has posted one last code sample that he/she is quite satisfied with.Originally Posted by somali.cc
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)