Hi!
I am learning C, and am at very introductory level, using this book called C Programming: A Modern Approach. I have just studied about arrays, and while tackling the programming exercises,i got stuck in Que 9 in Chapter 8.
This is what the question says
Write a program that generates a "random walk" across a 10 x 10 array. The array will contain characters (all '.' initially).The program must "walk" from element to element, always going up,down,left, or right by one element. The elements visited will be labelled with the letters A through Z, in the order visited. Here is an example of the desired output.
see it at the topmost comment
HINT:Use srand and rand functions to generate random numbers. After generating a number, look at its remainder when divided by 4. There are four possibile values: 0,1,2,3 - indicating the direction of next move. Before perfoming a move, check that it doesn't go outside the array and that it doesn't take us to an element already with a letter.If all the four direction are blocked, the program must terminate.
Here is many failed trial. (Though i arrived at this after my trials. Am just not getting it right :sad:)
/*
This is the desired output:
. . U V W . . . . .
. . T . X Y Z . . .
Q R S . . . . . . .
P O N . H G F . . .
. . M . I . E . . .
. . L K J . D C B A
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
#define ROW 10
#define COL 10
int main(){
char grid [ROW][COL];
int ro,co;
char letters = 'A';
//fill the grid with '.' characters
for(int r=0; r < ROW; r++){
for (int c=0;c< COL;c++){
grid[r][c]='.';
}
}
srand((unsigned)time(NULL));
//pick starting point
ro = rand() % ROW;
co = rand() % COL;
grid[ro][co]= letters;
/*
* Now, taking the random walk.
* There are 4 possible moves at maximum. I shall consider each at a time
* I shall label the moves 0, 1 , 2 , 3
* each reperesents a direcition as follows:
*
* move direction
* 0 row++
* 1 column++
* 2 row--
* 3 column--
*/
//temporary positions for row and column points
int row = ro;
int col = co;
bool blocked = false;
while(!blocked){
int move = rand() % 4;
switch (move){
case 0:
if (grid[++row][col] == '.' && row < ROW ){ //unoccupied position
++ro;
grid[ro][co] = ++letters;
break;
}
else{
blocked = true;
break;
}
case 1:
if (grid[row][++col] == '.' && col < COL){ //unoccupied position
++co;
grid[ro][co] = ++letters;
break;
}else{
blocked = true;
break;
}
case 2:
if (grid[--row][col] == '.' && row < ROW){ //unoccupied position
--ro;
grid[ro][co] = ++letters;
break;
}else{
blocked = true;
break;
}
case 3:
if (grid[row][--co] == '.' & col < COL){ //unoccupied position
--co;
grid[ro][co] = ++letters;
break;
}else{
blocked = true;
break;
}
}
if (blocked)
break;
}
//print the walk
for(int r=0; r < ROW; r++){
for (int c=0;c< COL;c++){
printf("%c ",grid[r][c]);
}
printf("\n");
}
return 0;
}
Please, give me a better approach. Limit the approach to arrays and control structures. No functions.