944,139 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 2735
  • C RSS
Apr 19th, 2007
0

Re: counts how many times the number 5 appears in array?

Expand Post »
<< split from here >>

how can i make this code to display how meany times a number appears in the array.. example... if there are 5 number 3 it will display this with asterisks..

3 *****

and so on for each number... here is my code... i almost have it

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. int num_generated[100];
  6. int count(int x);
  7.  
  8.  
  9. int main(void){
  10.  
  11. int j,m,a,b,ans,x=0,z=0;
  12. srand(time(NULL));
  13. for(j=0;j<100;j++){
  14. num_generated[j]=rand()%11;
  15. printf("%d. %d\n ",x, num_generated[j],x++);
  16. }
  17. printf("\nhere is how meany times each number exist on the array");
  18. for(a=1;a<=10;a++){
  19. ans= count(a);
  20. printf("\n%d %d",z,a,z++);
  21. for(b=1;b<=ans;b++){
  22. printf("*");
  23. }
  24. }
  25. }
  26.  
  27.  
  28.  
  29.  
  30. int count(int x){
  31. int cnt,j;
  32. for( j=0; j< 100;j++){
  33. if(num_generated[j]==x)
  34. cnt++;
  35. }
  36. return cnt;
  37. }
Last edited by ~s.o.s~; Apr 20th, 2007 at 11:21 am. Reason: Added CODE tags -- you actually typed right over how to use them when you entered this post...Added the link to original thread.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
diegojoven is offline Offline
2 posts
since Apr 2007
Apr 19th, 2007
0

Re: counts how many times the number 5 appears in array?

1) use code tags when posting code to the board
2) intialize cnt to 0 every time count() is called
3) it isn't polite to highjack someone elses thread. Though there is some similarity between your problem and the original problem presented, it's not the same and a new thread should be used.
Last edited by Lerner; Apr 19th, 2007 at 2:36 pm.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Apr 19th, 2007
0

Re: counts how many times the number 5 appears in array?

You never initialized cnt to 0 in the count() function
Moderator
Reputation Points: 3281
Solved Threads: 895
Posting Sage
WaltP is offline Offline
7,749 posts
since May 2006
Apr 19th, 2007
0

Re: counts how many times the number 5 appears in array?

Click to Expand / Collapse  Quote originally posted by WaltP ...
You never initialized cnt to 0 in the count() function
yahooo!!!!!

thanx a million you are the best
Reputation Points: 10
Solved Threads: 0
Newbie Poster
diegojoven is offline Offline
2 posts
since Apr 2007
Apr 20th, 2007
0

Re: counts how many times the number 5 appears in array?

hi i am sending the code to count no of 5's in an array
jus see this.........

  1. #include<stdio.h>#define MAX 5#define LIMIT 5int main(void){int a[MAX]={51,5,855,55,5};int c=0,i,n,r;char b[LIMIT];for(i=0;i<MAX;i++) { n=a[i];while(n!=0){r=n%10;if(r==5){c=c+1;}n=n/10;} }printf("\ncount of 5's is : %d\n",c);return 0;}
my personal id: snipped
Last edited by WaltP; Apr 20th, 2007 at 11:07 am. Reason: Email removed -- Do you really want to increase your SPAM count?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
heart_satish is offline Offline
1 posts
since Apr 2007
Apr 20th, 2007
0

Re: counts how many times the number 5 appears in array?

hi i am sending the code to count no of 5's in an array
jus see this.........

  1. #include<stdio.h>#define MAX 5#define LIMIT 5int main(void){int a[MAX]={51,5,855,55,5};int c=0,i,n,r;char b[LIMIT];for(i=0;i<MAX;i++) { n=a[i];while(n!=0){r=n%10;if(r==5){c=c+1;}n=n/10;} }printf("\ncount of 5's is : %d\n",c);return 0;}
my personal id: snipped
If you are going to post code, please make sure it's readable. That's what the PREVIEW button is for. And if you're going to do someone's homework for them, make sure you get the grade because he won't deserve it.
Moderator
Reputation Points: 3281
Solved Threads: 895
Posting Sage
WaltP is offline Offline
7,749 posts
since May 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: strcmp doesn't work... =(
Next Thread in C Forum Timeline: Can Templates Have prototypes ?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC