Purpose : write a program to simulate a random walk through a grid.
Consider a grid of m x n squares. This grid may be viewed as a pavement of m x n rectangular adjacent tiles. A walk in the grid starts at the south west corner and ends at the north east corner. A walk consists of several moves. Each move from the current tile in the pavement either goes to the adjacent tile north or to the adjacent tile east. A coin toss decides whether the next move goes north or east. A move leaving the grid may never be executed. The eccentricity of a walk in a grid with m rows is the maximum of |m-i-1-j| over all squares (i,j) visited during the walk.
Write a program that generates random walks as described above. After each walk is completed, the program displays the grid on screen, tiles that have been visited are marked with ``+'', unvisited tiles are left blank. Next the eccentricity of that walk is printed. Then the user is prompted either to continue the generation of walks or to stop. At the end, the program prints the number of walks generated and the minimal and maximal eccentricity over all walks. A sample output of such a session is given below.
[jan@obelix Project1]$ ./walk
Give number of rows : 8
Give number of columns : 8
+
+
++
++
++
+
++++
++
Eccentricity of the walk : 3
Type q to quit. Press enter to continue...
++++++
+
+
+
++
+
+
++
Eccentricity of the walk : 5
Type q to quit. Press enter to continue...
+
+
+
++
++++
++
+++
+
Eccentricity of the walk : 3
Type q to quit. Press enter to continue...q
Result of 3 random walks in 8-by-8 grid :
Minimal eccentricity of walks : 3
Maximal eccentricity of walks : 5
[jan@obelix Project1]$
To implement the coin toss, the following code may be used:
#include < stdlib.h >
int coin_toss () /* executes a coin toss, returns 1 or 2 */
{
int r = rand();
if (r < RAND_MAX/2)
return 1;
else
return 2;
}
This is a ``pseudo'' random number generator: each time you launch
the program, it generates the same sequence of numbers.
The listings of your program will be collected at the start of the lecture on Monday 29 January, at 1PM. Also send the code by e-mail to jan@math.uic.edu. The first line of your program should be like
/* MCS 275 Project One by < Author > */
where you replace the < Author > by your name.
Write appropriate documentation inside the code to comment on your subroutines. The gcc compiler will be used to test your program. So even if you have developed your program in a Windows environment, it may be good to run your final version on icarus.cc.uic.edu where the gcc compiler is installed. A final note to Windows users: with the Cygwin tools you can use the gnu tools from Windows.
If you have questions, comments, or difficulties, feel free to come to my office for help.