CS 2073, Spring 2006
 Program 8
 Random Walk in the Plane
 Due (on time): 2006-04-07  23:59:59
 Due (late):        2006-04-10  23:59:59

Program 8 must be emailed to: nrwagner@cs.utsa.edu
following directions for: running and submitting a C program, with deadlines:
  • 2006-04-07  23:59:59 (that's Friday, 7 April 2006, 11:59:59 pm) for full credit.
  • 2006-04-10  23:59:59 (that's Monday, 10 April 2006, 11:59:59 pm) for 75% credit.

New: The web page Mulitplication Table illustrates some of the code needed for this assignment.


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.


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. Use two variables, say x and y, to keep track of the x and y coordinates of the location of the random walk at each step. Start the walk at SIZE/2 for each coordinate, so that x and y will each be initialized to this quantity.

  3. Use a loop to carry out the random walk, starting the walk out by the initialization in the previous item. At each step in the walk (that is, each time the loop is executed), 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 means that the loop in the item above must be terminated (break from the loop) in this case. 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.

  9. 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 so that you can use the previous location for '$'.]

  10. As a final feature, fix up the print-out 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 SIZE equal to 50. 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.


For Extra Credit:
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 two runs.
  2. C source for item 2 above and two runs.
  3. C source for the extra credit part. One run is enough.


Revision date: 2006-04-05. (Please use ISO 8601, the International Standard.)