Puzzle DA10


Fill an array with an ascending sequence of random doubles, with an average step size of avg

[M-15] Write a function that fills an array with a sequence of double precision floats. Each float is greater than its predecessor by an average amount avg, but may be greater or less than avg by up to but not including an amount dev. In other words, the amount inc added to one element to get the next is in the range (avg-dev)< inc < (avg+dev).

A prototype for the function is:

void fillDoubleArrayRandomAscending( int size, double arr[], double avg, double dev )

Start the array with a value (avg-dev)< arr[0]< (avg+dev). Sample output of a main program that tests the function for an avg of 1.0 and a dev of 1.5 is:

  0.4564   2.5835   3.9723   4.6389   5.0146
  7.2102   6.7249   7.8433   9.0546   9.9227
 11.3645  11.0373  12.0200  13.9607  16.0684
 16.1864  17.5251  18.2726  20.6133  22.2673
 24.6562  25.9857  26.4264  27.8649  28.9353
 30.9444  31.2108  31.3442  31.1604  32.2450
 32.4328  32.1780  32.1044  33.7280  33.6398
 36.0319  37.7449  37.5206  39.5221  39.2572
 40.0321  40.3241  42.2031  43.4218  44.7634
 45.0128  46.1254  47.1837  48.4832  49.9341

With that choice for avg and dev, the array is more-or-less ascending, but sometimes elements are out of order.



Previous Page        Answer         Next Page         Home