|
|
 |
|
|
|
|
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. |
|
|
|
 |
|
|
|
|
Example: |
|
|
|
|
| |
|
|
|
|
The 2 would be regarded as an error and would be given a corrected value of 5. |
|
|
|
|
|
 |
|
|
|
|
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). |
|
|
|
 |
|
|
|
|
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. |
|
|
|
 |
|
 |
|
|
*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: |
|
|
|
 |
|
 |
|
|
randNum = rand() % 4 + 1; |
|
|
|
 |
|
 |
|
|
See Appendix C for further details. |
|
|
|
|
|