User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 426,127 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,731 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 878 | Replies: 5
Reply
Join Date: Apr 2007
Posts: 2
Reputation: diegojoven is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
diegojoven diegojoven is offline Offline
Newbie Poster

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

  #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 10: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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2005
Posts: 1,157
Reputation: Lerner is just really nice Lerner is just really nice Lerner is just really nice Lerner is just really nice 
Rep Power: 9
Solved Threads: 150
Lerner Lerner is offline Offline
Veteran Poster

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

  #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 1:36 pm.
Reply With Quote  
Join Date: May 2006
Posts: 2,723
Reputation: WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold 
Rep Power: 15
Solved Threads: 222
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Maven

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

  #3  
Apr 19th, 2007
You never initialized cnt to 0 in the count() function
Age is unimportant -- except in cheese
Reply With Quote  
Join Date: Apr 2007
Posts: 2
Reputation: diegojoven is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
diegojoven diegojoven is offline Offline
Newbie Poster

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

  #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  
Join Date: Apr 2007
Posts: 1
Reputation: heart_satish is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
heart_satish heart_satish is offline Offline
Newbie Poster

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

  #5  
Apr 20th, 2007
hi i am sending the code to count no of 5's in an array
jus see this.........

#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 10:07 am. Reason: Email removed -- Do you really want to increase your SPAM count?
Reply With Quote  
Join Date: May 2006
Posts: 2,723
Reputation: WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold 
Rep Power: 15
Solved Threads: 222
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Maven

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

  #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.........

#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.
Age is unimportant -- except in cheese
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 4:32 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC