< previous page page_767 next page >

Page 767
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
One approach to generating a picture is to print a dark character (like a $ ) when the brightness level is low, and to print a light character (like a blank or a period) when the level is high. Unfortunately, errors in transmission sometimes occur. Thus, your program should first attempt to find and correct these errors. Assume a value is in error if it differs by more than one from each of its four neighboring values. Correct the erroneous value by giving it the average of its neighboring values, rounded to the nearest integer.
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
Example:
0767-01.gif
The 2 would be regarded as an error and would be given a corrected value of 5.

3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
Note that values on the corners or boundaries of the matrix have to be processed differently than the values on the interior. Your program should print an image of the uncorrected picture and then an image of the corrected picture.
5. The following diagram represents an island surrounded by water (shaded area).
0767-02.gif
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
Two bridges lead out of the island. A mouse is placed on the black square. Write a program to make the mouse take a walk across the island. The mouse is allowed to travel one square at a time, either horizontally or vertically. A random number from 1 through 4 should be used to decide which direction the mouse is to take. * The mouse drowns when he hits the water; he escapes when he enters a bridge. You may generate a random number up to 100 times. If the mouse does not find his way by the hundredth try, he will die of starvation. Restart the mouse in a reinitialized array and go back and repeat the whole process. Count the number of times he escapes, drowns, and starves.
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif 3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
*Through the header file stdlib.h, the C++ standard library supplies a value-returning, parameterless function named rand. Each time it is called, rand returns a random int in the range 0 through RAND_MAX, a constant defined in stdlib.h (usually the same as INT_MAX). The following statement assigns to randNum a random integer in the range 1 through 4:
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif 3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
randNum = rand() % 4 + 1;
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif 3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
See Appendix C for further details.

 
< previous page page_767 next page >