CS 2073, Fall 2005
 Program 8
 Random Walk in the Plane
 Due (on time): 2005-10-31  23:59:59
 Due (late):        2005-11-02  23:59:59

Program 8 must be emailed to: nrwagner@cs.utsa.edu
following directions for: running and submitting a C program, with deadlines:
  • 2005-10-31  23:59:59 (that's Monday, 31 October 2005, 11:59:59 pm) for full credit.
  • 2005-11-02  23:59:59 (that's Wednesday, 2 November 2005, 11:59:59 pm) for 75% credit.


Introduction: For this assignment you are to write a C program to simulate a random walk in the plane. The program will record the progress of the walk in a 2-dimensional array, and then print the array. The program will also keep track of the number of steps to get to the boundary, and print this number. Finally, you will enhance the program so that it prints the walk in a more readable and interesting form.


Details of the basic program:
  1. Use a 40-by-40 grid for the walk, perhaps defined by:

    Normally, we would use a defined constant for the size of the grid:

  2. Start the walk at SIZE/2 for each coordinate.

  3. At each step in the walk, use a random number to decide on the new location, which must be either one space up, one space down, one space right, or one space left. In other words, one of the new coordinates stays the same and the other changes by +1 or -1.

  4. The random walk must stop when you first reach the boundary. This occurs when either coordinate would become equal to -1 or SIZE. If the walk actually moves to such a location, then it would reference an out-of-bounds coordinate of the grid, which is a serious mistake in C (serious because it does not cause a run-time error, as it does in Java). Instead, the walk should be terminated.

  5. Initialize all chars in the grid to blanks. Use a separate function to do this, say with prototype:

  6. For each location on the random walk, overwrite that char with a *, so that the walk appears as all stars. The walk may loop back on itself, and since it is all stars, you won't be able to tell exactly how the walk goes.

  7. Print the new version of the grid with the stars present. Use a separate function to do this, say with prototype:

  8. Keep a count of each step of the random walk, and print the total length (number of steps) at the end.


Enhanced version of the program:
  1. When you are all done, enhance the program so that instead of printing all stars, it prints an 'A' 10 times, then a 'B' 10 times, ... , then a 'Z' 10 times, then an 'a' 10 times, then a 'b' 10 times, ... , then a 'z' 10 times. After 520 steps in the random walk, you should start over with 'A' again. [Hint: you can use a string defined by:

    This defines a string of length 52, so that alf[0] through alf[51] refer to characters 'A' through 'z'. Then you can take the current number of steps up to a give square, divide by 10, and remainder by 52 to get an index into the alf array.]

  2. Also fix it up so that the first step of the walk is printed as a '#' (easy to do), and the last step (the one just before stepping out of bounds) is printed as a '$' (a little harder). [Hint: insert these two characters into the array after all the rest are inserted. '#' can just be inserted at the center, but for '$', you have to save the old location each time to that you can use the previous location for '$'.]

  3. As a final enhancement, fix up the boundary so that it looks like the following: Sample random walk. This should be done using the defined constant SIZE, so that changing this constant to 50, say, would still produce the enhanced boundary.


What to turn in: You should do the following:

  1. Create and run a C program that uses stars for the random walk. Be sure to print the number of steps. Show two runs, one with fewer than 520 steps and one with more than 520 steps.

  2. Create and run a C program that uses the enhanced look for the random walk. Be sure to print the number of steps. Again show two runs, one with fewer than 520 steps and one with more than 520 steps.


What you should email: Refer to the submissions directions and to deadlines at the top of this page. The text file that you submit should first have Your Name, the Course Number, and the Program Number. The rest of the file should have the following in it, in the order below, and clearly labeled, including at the beginning the appropriate item letters: a, b, c, etc.

 Contents of email submission for Program 8:

Last Name, First Name; Course Number; Program Number.

  1. C source for item 1 above and the results of two runs.
  2. C source for item 2 above and the results of two runs.


Revision date: 2005-10-23. (Please use ISO 8601, the International Standard.)