reno.WPI.EDU> cat minval2.C // file minval2.C, cs2223, D97/98 // // program which finds the minimum value in an array // 28Feb98 NW, First version // global varaibles long int replaces; // includes #include "headers.h" // definitions #define NMAX 10000 // prototypes int minval(int, int *); // find the minimum value in an array void shuffle(int, int *); // shuffle the values in an array // main int main () { extern long int replaces; int *array = new int[NMAX]; float average; srand(time(NULL)); // seed the random number generator cout << "n average replaces" << endl; for (int array_size = 1; array_size <= NMAX; array_size *= 10) // { average = 0.0; for (int n = 1; n <= array_size; n++) // average NMAX cases { for (int i = 0; i < array_size; i++) array[i] = i + 1; // fill the array shuffle(array_size, array); // shuffle the array replaces = 0; minval(array_size, array); // find minimum value then ignore it average = ((n - 1) * average + replaces) / (float)n; } // end for(n) cout << array_size << " " << average << endl; } // end for(array_size) return 0; } // end main() // functions int minval(int n, int *array) // find the minimum value in an array { extern long int replaces; int min = INT_MAX; // default value for (int i = 0; i < n; i++) if (array[i] < min) replaces++, min = array[i]; return min; } // end minval() void shuffle(int n, int *array) // shuffle the values in an array { for (int i = 0; i < n; i++) { int temp = array[i]; // swap each value with a random one int other = rand() % n; array[i] = array[other]; array[other] = temp; } return; } // end shuffle() // Copyright 1998 Norman Wittels reno.WPI.EDU> g++ minval2.C reno.WPI.EDU> a.out n average replaces 1 1 10 3.2 100 5.14 1000 7.253 10000 9.65708 reno.WPI.EDU>