CS2023 Assignment 4
Winter 2004
Due Friday, February 13, 10:30 AM, in the assignment bin on E-Level
This assignment will be marked out of 40 points.
Write a program that generates a "random walk" across a 10 x 10 array.
The array will contain characters (all '.' initially). The program must
randomly "walk" from element to element, always going up, down, left, or
right by one element. The elements visited by the program will be labeled
with the letters A through Z, in the order visited. Here's an example of
the desired output:
A B . . . . . . . .
D C . . . . . . . .
E . . . . . . . . .
F . J K L . . . . .
G H I . M N . . . .
. S R Q P O . . . .
. T U . . . . . . .
. W V . . . . . . .
. X . . . . . . . .
Z Y . . . . . . . .
(Hint: use srand and rand functions, as in assignment
2). Before performing a move, check that (a) it won't go outside the array
and (b) it doesn't take us to an element that already has a letter assigned.
If either condition is violated, try moving in another direction. If all
four directions are blocked, the program must terminate. Here's an example
of premature termination:
A P O N M L . . . .
B C . I J K . . . .
. D E H . . . . . .
. . F G . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
P is blocked on all three available sides, so there's no place to put Q.
Requirements
Your program, which must be called random_walk.c, must prompt
the user for a seed for the random number generator, and print out only
the final array, after 'Z' has been reached, or the walk is blocked. Your
program must include at least one function,
randMove, other than
main. randMove should have three parameters: a 2D array
containing the walk, a 1D array (with 2 elements) containing the current
position of the walk, and a character variable containing the next letter
in the walk. This function should randomly move to the next position in
the walk, updating both arrays. If no move is possible, randMove returns
BLOCKED, otherwise it returns OK (these are preprocessor
macros you can define).
Marking Scheme
2 marks will be deducted if your program does not compile (using gcc on
a Linux/Intel machine) and 2 marks will be deducted if execution of your
program does not terminate. Make sure you follow the C
Language Coding Standard, format your code clearly, and choose meaningful
variable names: these are all worth 8 marks. The remaining marks will be
assigned for program correctness and implementation of the randMove
function as required above.
Deliverables
A hardcopy of your assignment in the bin on E-level, which must include
source code of your program and electronic submission of the program, which
must be called random_walk.c (lower case only!) to the "Submit
your assignment" link on the CS2023
home page.
Eric Aubanel, Feb. 5 2004