ernie.WPI.EDU> cat exam04_01.C // file exam04_01.C, cs2223, D97/98 // // 28Apr98 NW, First version // includes #include "headers.h" // definitions #define XMAX 640 #define YMAX 480 // prototypes void gray1(int xmax, int ymax); void gray2(int xmax, int ymax); // global variable char picture[YMAX][XMAX]; // global variable // main int main () { srand(time(NULL)); // seed the random number generator int x, y; float sum; for ( y = 0; y < YMAX; y++) for (x = 0; x < XMAX; x++) picture[y][x] = 0; gray1(XMAX, YMAX); // first function sum = 0; for ( y = 0; y < YMAX; y++) for (x = 0; x < XMAX; x++) sum += picture[y][x]; cout << endl << "average value for gray1() is " << sum / (XMAX * YMAX) << endl; for (y = 0; y < YMAX; y++) for (x = 0; x < XMAX; x++) picture[y][x] = 0; gray2(XMAX, YMAX); // first function sum = 0; for (y = 0; y < YMAX; y++) for (x = 0; x < XMAX; x++) sum += picture[y][x]; cout << endl << "average value for gray2() is " << sum / (XMAX * YMAX) << endl; } // end main() // functions void gray1(int xmax, int ymax) { for (int y = 0; y < ymax; y++) for (int x = 0; x < xmax; x++) if (rand()%2) picture[y][x] = 1; // black } // end gray1() void gray2(int xmax, int ymax) { for (int n = 0; n < xmax*ymax/2; n++) { int x = rand() % xmax; int y = rand() % ymax; picture[y][x] = 1; // black } } // end gray2() // Copyright 1998 Norman Wittels ernie.WPI.EDU> g++ exam04_01.C ernie.WPI.EDU> a.out average value for gray1() is 0.500615 average value for gray2() is 0.393623 ernie.WPI.EDU>