Puzzle R6

Generate random integers in the range MIN..MAX

[H-3] Write a function int randInt(int min, int max) that returns a random integer in the range min..max (inclusive).

[M-20] Now write a main() program that uses the function to generate N random integers and tests that each one is in the proper range. Print each integer. Append a star to those integers that are out of range.

Here is sample output of the program when the randInt() function is defective, for min==50 and max==100:

  68   63   74   93   76   51   96   72   92   98
  70   89   64   49*  76   67   95   75   88   88
  79   72   52   87   65   74   84   69   75   73
  58   76   94   61   67   90   84   55   95   51
  59   60   80   87   98   67   82   59   56   81
  59   93   86   62   83   93  101*  84   49*  89
  59   69   60   58   80   98   88   97   64   54
  65   77   60   63   52   52   97   71   91   71
  72   55   93   97   69   74   86   56   62   76
  55   86   82   73   64   63   74   54   83   93

Of course, given the above display, which shows a defective randInt(), you would fix the function. But first you have to be sure that the display that shows the problem is itself working correctly!



Previous Page        Answer         Next Page         Home