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

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int num_generated[100];
int count(int x);


int main(void){
    
int j,m,a,b,ans,x=0,z=0;
    srand(time(NULL));
    for(j=0;j<100;j++){
        num_generated[j]=rand()%11;
            printf("%d. %d\n  ",x, num_generated[j],x++);
    }
    printf("\nhere is how meany times each number exist on the array");
    for(a=1;a<=10;a++){
        ans= count(a);
        printf("\n%d %d",z,a,z++);
        for(b=1;b<=ans;b++){
            printf("*");    
        }
    }
}    




int count(int x){
    int cnt,j;
    for( j=0; j< 100;j++){
        if(num_generated[j]==x)
            cnt++;        
    }
    return cnt;
}

Recommended Answers

All 5 Replies

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.

You never initialized cnt to 0 in the count() function

You never initialized cnt to 0 in the count() function

yahooo!!!!!

thanx a million you are the best:D:-O:)

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: [email]snipped[/email]

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: [email]snipped[/email]

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.