Question
Write a program that generates a random walk across a 10x10 array. The array will contain characters (all . initially). The program must randomly walk
Write a program that generates a random walk across a 10x10 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. Heres an example of the desired output:
A . . . . . . . . .
B C D . . . . . . .
. F E . . . . . . .
H G . . . . . . . .
I . . . . . . . . .
J . . . . . . . Z .
K . . R S T U V Y .
L M P Q . . . W X .
. N O . . . . . . .
. . . . . . . . . .
Implement two functions to achieve this task:
void generate_random_walk(char walk[10][10]);
void print_array(char walk[10][10]);
main first calls generate_random_walk, which initializes the array to contain . characters and then replaces some of these characters by the letters A through Z (see HINT below). main then calls print_array to display the array on the screen.
HINT: Use the srand and rand functions (see deal.c) to generate random numbers. After generating a number, look at its remainder when divided by 4. There are four possible values for the remainder (0, 1, 2, 3), indicating the direction of the next move. Before performing a move, check that (a) it wont go outside the array, and (b) it doesnt take us to an element that already has a letter assigned. If either condition is violated, try moving into another direction. If all four directions are blocked, the function generate_random_walk should return.
Heres an example of premature termination:
A B G H I . . . . .
. C F . J K . . . .
. D E . M L . . . .
. . . . N O . . . .
. . W X Y P Q . . .
. . V U T S R . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started