# Count_nums (need help ASAP!)

• 11-22-2003
legacye
Count_nums (need help ASAP!)
Count_nums in an array
---------------------------------------------------------
Hi..i need to get a solution to this pretty trivial problem asap.

This is a random number generator that has header functions that are meant to do most of the work.

The part im having trouble with is the count_nums section where the program is meant to output the number of occurrences of numbers 1->9 in the array a[i] which contains random numbers

Ive been able to do it with switch statements...but it needs to be done just using the variables *a (the array), range (defined range), and num (the number 1->9 which will be incased in a loop) as well as maybe int count or int i too.

I also need to be able to accept in a character r or R from the keyboard to restart the whole thing , and q to exit!!!!

PLLLLLLLLLLLEEEEEEEEEEEEASSSSSEEE help me if you can!

---------------------------------------------------------------------
Code:

``` #include <stdio.h> #include <stdlib.h> #include <time.h> #include <conio.h> /*int*a will be our array, and int s the size of the array*/         void SetRandomSeed(void);         int my_random(int range);         int get_max(int*a,int s); /* array, array size */         int get_min(int*a,int s); /*array , array size */         void count_nums(int*a,int range,int num); int count_num(int a[], int range, int num) /* checks array a for number of occurrances of value */ {   int i, count=0;   for (i=0; i<range; i++)   {         if (a[i] == num)         {                 ++count; /* it was found */         }   }   return(count); } void print_array(int a[], int range) {   int i;   for(i=0; i<range; i++)   {         printf("%d ", a[i]);   }   printf("\n"); }   int get_max(int*a,int s)                                 /* returns largest number */ {           int i, num_large;  /* cnum_large is largest value so far      */     /*  Initial array element is largest so far                  */           num_large = a[0];        /*  Compare remaining list to the largest so far; save larger */           for (i=1; i<s; ++i)                       if (a[i] > num_large)                         num_large = a[i];   return (num_large); } int get_min(int*a,int s)                 /* returns smallest number */ {                           int i, num_small;  /* num_small is smallest value so far      */     /*  Initial array element is smallest so far                  */                           num_small = a[0];        /*  Compare remaining list to the smallest so far; save smallest */                           for (i=1; i>s; ++i)                               if (a[i] < num_small)                         num_small = a[i];   return (num_small); } void SetRandomSeed(void) {   /* call time and feed it to srand */           srand( (int) time(0) ) ; } int my_random(int range) {  return 1+rand()%range; } int main () {   int a[10],one; /*array*/       int i,range,s,c; /*global variables*/         int max,min,randc; /*secondary variables*/         int num_occ, value;     /*Welcome line*/   printf("\nWelcome to the Random Number Generator.\n");   /* range */           printf("\nPlease enter the value for the range:\n");               scanf("%d",&range);                   printf("\n----------------------------------\n");                           printf("This program will generate random numbers in the range %d ",range);                           /*input number of random numbers you would like generated*/                 printf("\nHow many random numbers would you like generated?\n");                   scanf("%d",&s); /*length of s*/                     for (i=0;i<s;++i){       a[i] = my_random(range);//int n           printf("\nrandom number %d: %d\n",i,a[i]);//int n ends }         printf("\nPress any key to continue...\n");                 getch();                           SetRandomSeed();   /* set the random seed from the time */         printf("\nRAND_MAX equals %d \n",RAND_MAX);             printf("range equals %d",range);               printf("\n----------------------------------\n");   /* print the occurrances*/     for(value=0; value<=9; value++)   {           num_occ = count_num(a,range, value);           printf("The value %d was found %d times.\n", value, num_occ);   }               /*max and min of range*/   printf("\nPress enter for the max and min values for the range\n");       getch();         printf("\n min %d\n",get_min (a,s));             printf("\n max %d\n",get_max (a,s));   getch();     /* if everything okay return no errors */           return (0); }```
----------------------------------------------------------------------

Thanks a lot!
Legacye
• 11-22-2003
quzah
Code:

```int total[10] = {0}; int array_to_count[] = { 1, 3, 4, 9 , 0, 0, 3, 4, 5, 6, 7, 7, 0, 2, 1, 5, 6, 8, 8, }; int x;     /* in a loop */     total[ array_to_count[ x ] ] ++;```
What this does is take the value at "array_to_count[ x ]", find the appropriate cell in 'total', and increment that value by one.

At the end, you can loop through 'total' and see how much of each position there is.

Quzah.
• 11-22-2003
legacye
thanks...but could someone show me how to do that in the header?
This is the section that does the count work:
Code:

```int count_num(int a[], int range, int num) /* checks array a for number of occurrances of value */ {   int i, count=0;   for (i=0; i<range; i++)   {  if (a[i] == num)  { ++count; /* it was found */  }   }   return(count); }```
How can i apply quzah's method to this part of the program..so that i get the occurrances to output in the main() part of the program that loops as shown above?
• 11-22-2003
Salem
Perhaps you should be doing
Code:

```    for(value=0; value<=range; value++)   {           num_occ = count_num(a,s, range);           printf("The value %d was found %d times.\n", value, num_occ);   }```
It would help if you
a) picked better variable names
• 11-22-2003
legacye
no that doesnt work
i Tried that Salem, but all it does is extend the output past 9 (up to the maximum value in the array)..it doesnt help the functionality of occurrances at all...
• 11-22-2003
legacye
Tried the first way...no success
I tried the total [nums] etc...with and without Salems suggested way and it keeps crashing ...and doesnt count the numbers correctly

Code:

```int count_num(int a[], int range, int num) /* checks array a for number of occurrances of value */ {   int total[10]={0}; int answer; {   total[a[num]]++;   answer=total[a[num]]; }   return(answer); }```
• 11-22-2003
Salem
Bah, close

Code:

```    for(value=0; value<=range; value++)   {           num_occ = count_num(a,s,value);           printf("The value %d was found %d times.\n", value, num_occ);   }```
But then you should have been able to figure that out.