I dont seem to compile it. Please help me to check and guide me through. Please. Help really needed.
Question:
Write a program to show the operation of the random number generator.
The program will generate a specified number of random numbers in a given range.
It will count the number of times each number occurs.
It will display on the screen how many times each number occurred, in the form:
0 occurred 100 times
1 occurred 102 times
2 occurred 98 times
etc.
It will calculate the minimum number of occurrences, the maximum number of occurrences and the number of times each number could be expected.
e.g.
Minimum count =98, maximum count =102, expected count =100.
Use date_time_seed() and rand() (see example program below)
The prpgram will accept the following single keystroke commands from the keyboard ..
• ‘R’ to run the program, prompting for the number range and the number of numbers to generate. After running it prompts for either R or Q again.
• ‘Q’ to quit the program.
Code:
#include <stdlib.h>
#include <stdio.h>
#include <DBOS\LIB.h>
#include <CLIB.H>
void count_nums(int *a, int range, int num);
int my_rand(int);
int get_max(int *a, int s);
int get_min(int *a, int s);
int main(void)
{
int range, num;
/*!! foo.c:14: warning: implicit declaration of function ‘date_time_seed’ */
date_time_seed();
/*!! foo.c:15: warning: ISO C90 forbids mixed declarations and code */
int a[50], t[50];
char c;
int s, i;
printf(" * Please press r/R to run or press q/Q to exit *\n");
/*!! foo.c:20: warning: implicit declaration of function ‘getch’ */
c = getch();
/*run by pressing r and exit by pressing q */
if (c == 'r' || c == 'R') { /*accept both lower and upper case */
/*move to next statment only if the input in range 1 to 20 */
do {
printf("Enter the range size(max=20)");
scanf("%d", &range);
}
while (range < 0 || range > 20);
printf("How many numbers do you wish to generate?");
scanf("%d", &num);
date_time_seed;
count_nums(a, range, num);
for (i = 0; i < range; i++)
printf("%d:%d\n", i, t[i]);
printf("Maximum count:%d\n", get_max(a, range));
printf("Minimum count:%d\n", get_min(a, range));
printf("Expected: %d\n", (int) (num / range));
}
if (c == 'q' || c == 'Q')
exit(1);
return (0);
}
int my_rand(int range)
{
int r;
date_time_seed();
r = 1 + rand() % range;
return r;
}
void count_nums(int *a, int range, int num)
{
int i;
int t[50];
for (i = 0; i <= num; i++)
{
a[i] = my_rand(range);
t[i] = 0;
t[a[i]]= t[a[i]]+1;
printf("%d", a[i]);
}
}
/* function to find the max */
int get_max(int *a, int s)
{
int i, max;
max= *a;
for (i = 0; i < s; i++)
if (*(a + i) > max)
max = *(a + i);
return (max);
}
/*function to find the min */
int get_min(int *a, int s)
{
int i, min;
min = *a;
for (i = 0; i < s; i++)
if (*(a + i) < min)
min = *(a + i);
return (min);
}