|
|
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:
- Use a 40-by-40 grid for the walk, perhaps defined by:
char g[40][40];
Normally, we would use a defined constant for the size of the grid:
#define SIZE 40
char g[SIZE][SIZE];
- 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.
- 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.
- 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.
- Initialize all chars in the grid to blanks.
Use a separate function to do this, say with prototype:
void blankgrid(char g[SIZE][SIZE]);
- 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.
- Print the new version of the grid with the stars present.
Use a separate function to do this, say with prototype:
void printgrid(char g[SIZE][SIZE]);
- Keep a count of each step of the random walk, and print the
total length (number of steps) at the end.
- 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 '$'.]
- 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:
- 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.
- 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:
For extra credit, 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:
char alf[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
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.]
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.
- C source for item 1 above and two runs.
- C source for item 2 above and two runs.
- C source for the extra credit part.
One run is enough.
|
Revision date: 2006-04-05.
(Please use ISO
8601, the International Standard.)