#include #include #include int main () { int x, y, xmax, ymax, champexists; /* Seed the random number generator by the current time. We get different random sequences in different runs. */ srand((unsigned int)time(NULL)); /* Initialize the variables. xmax stores the largest x-coordinate, and ymax the largest y-coordinate read so far. champexists is a flag indicating whether the champion exists or not in the current collection. */ xmax = ymax = -1; champexists = 0; while (1) { /* Randomly decide whether to quit or generate a new point. */ if (rand() % 20) { x = rand() % 1000; y = rand() % 1000; } else x = y = -1; printf("New point: (%3d, %3d). ", x, y); if ((x < 0) || (y < 0)) { printf("Quitting...\n"); break; } if ((x >= xmax) && (y >= ymax)) { /* New champion found */ champexists = 1; xmax = x; ymax = y; } else if (x > xmax) { /* Here y < ymax but x > xmax */ champexists = 0; xmax = x; } else if (y > ymax) { /* Here x < xmax but y > ymax */ champexists = 0; ymax = y; } printf("Current champion: "); if (champexists) printf("(%3d, %3d)\n", xmax, ymax); else printf("None\n"); } exit(0); }