Frequency Counter

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2007
Posts: 2
Reputation: diegojoven is an unknown quantity at this point 
Solved Threads: 0
diegojoven diegojoven is offline Offline
Newbie Poster

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

 
0
  #1
Apr 19th, 2007
<< 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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,699
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 273
Lerner Lerner is offline Offline
Posting Virtuoso

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

 
0
  #2
Apr 19th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,117
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

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

 
0
  #3
Apr 19th, 2007
You never initialized cnt to 0 in the count() function
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 2
Reputation: diegojoven is an unknown quantity at this point 
Solved Threads: 0
diegojoven diegojoven is offline Offline
Newbie Poster

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

 
0
  #4
Apr 19th, 2007
Originally Posted by WaltP View Post
You never initialized cnt to 0 in the count() function
yahooo!!!!!

thanx a million you are the best
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 1
Reputation: heart_satish is an unknown quantity at this point 
Solved Threads: 0
heart_satish heart_satish is offline Offline
Newbie Poster

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

 
0
  #5
Apr 20th, 2007
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?
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,117
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

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

 
0
  #6
Apr 20th, 2007
Originally Posted by heart_satish View Post
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.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC