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