/******************************************************************* * This program prints out random numbers (7 per line) up to the * * number that the user has requested. It also keeps track of the * * minimum and maximum of these random numbers. * *******************************************************************/ #include #include /* function prototypes */ int max(int a, int b); int min(int, int); void print_random_numbers(int k); /* main function */ int main(void) { int n; printf("WARNING the maximum random value is %d\n", RAND_MAX); printf("Some random numbers will be printed\n"); printf("How many would you like to see? "); scanf("%d", &n); /* call the function to print random numbers */ print_random_numbers(n); return 0; } /* function to print out n random numbers */ void print_random_numbers(int k) { int i, r, biggest, smallest; r = biggest = smallest = rand(); printf("\n%7d", r); for(i = 1; i < k; i++) { /* go to a new line after every seven random numbers */ if(i % 7 == 0) printf("\n"); /* obtain a random number and store it in r */ r = rand(); /* keep track of the biggest and smallest random numbers */ biggest = max(r, biggest); smallest = min(r, smallest); printf("%7d", r); } printf("\n\n%s%5d\n%s%5d\n%s%5d\n\n", " Count: ", k, "Maximum: ", biggest, "Minimum: ", smallest); } /* end of print random numbers */ /* find the max of two numbers */ int max( int a, int b ) { if (a > b) return a; /* else */ return b; } /* find the min of two numbers */ int min( int a, int b ) { if(a < b) return a; return b; }