> Why do people insist on attaching their .c files instead of just pasting the code in?
Dunno, maybe they haven't figured out code tags, or because it's an unsightly mess. People just make it too hard on themselves by writing code in such an undisciplined way.
Here's the code - formatted by muggins here - with some comments as to what is going wrong
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];
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);
count_nums(a, range, num);
for (i = 0; i < range; i++)
printf("%d:%d\n", i, a[i]);
printf("Maximum count:%d\n", get_max(a, range));
/*!! foo.c:37: warning: ‘s’ is used uninitialized in this function */
printf("Minimum count:%d\n", get_min(a, s));
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;
for (i = 0; i <= num; i++);
{
num = my_rand(range);
*(a + num) = 0;
}
}
/* function to find the max */
int get_max(int *a, int s)
{
int i, max, num; /*!! foo.c:67: warning: ‘max’ may be used uninitialized in this function */
num == s; /*!! foo.c:68: warning: statement with no effect */
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);
}
I'm guessing date_time_seed(); is one of those magic functions in your schools library code - but in any event you need to only call it once (and never in my_rand).
See the FAQ - reseeding the rand() generator just makes it return a constant.