X
XLR3204S
So I'm going through K.N. King's C Programming: A Modern Approach, a
book with which most of you are familiar, as far as I can tell, and
Chapter 8 (Arrays) has this Programming Project (#9) requiring the
following:
---------------
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:
http://drp.ly/1nvue4
[the formatting is poor, so I hope this will do]
Hint: Use the srand() and rand() functions 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, and 3 --
indicating the direction of the next move. 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.
---------------
I've got a code which works, except for the last part: if all the
directions are blocked, it keeps looking for a possible one. I know
I'll have to change the entire loop, but here is how I've nailed it
down. Any comments on the code are greatly appreciated, but do note
that I'm not _supposed_ to know how to write custom functions or make
use of pointers.
----------------
/* Include the standard I/O library */
#include<stdio.h>
/* Include the time library */
#include<time.h>
/* Include the standard C library */
#include<stdlib.h>
/* Define main */
int main(void) {
/* Declare a 10x10 array of characters, initializing it to '.' */
char array[10][10] = {
{'A', '.', '.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.', '.', '.'},
};
/* Declare the loop counters, as well as the direction */
unsigned short int i, j, dir, x = 0, y = 0;
/* Declare a placeholder for the current character, initially 'A'
*/
char c = 'A';
/**
* Seed the current time as the initial value for the random
numbers
* generator
*/
srand((unsigned) time(NULL));
/**
* As long as the current character isn't Z (the last letter of
the
* alphabet), keep moving randomly across the array
*/
while(c != 'Z') {
/**
* Get the remainder of a random number divided by 4 and use
it as
* the direction of the next move
*/
dir = rand() % 4;
/**
* Directions:
* 0 -- up
* 1 -- right
* 2 -- down
* 3 -- left
*/
if(dir == 0) {
/* If everything's OK, make the move */
if(x - 1 >= 0 && array[x - 1][y] == '.') {
array[--x][y] = ++c;
} else { /* Otherwise, try the next direction */
dir = 1;
}
}
if(dir == 1) {
/* If everything's OK, make the move */
if(y + 1 < 10 && array[x][y + 1] == '.') {
array[x][++y] = ++c;
} else { /* Otherwise, try the next direction */
dir = 2;
}
}
if(dir == 2) {
/* If everything's OK, make the move */
if(x + 1 < 10 && array[x + 1][y] == '.') {
array[++x][y] = ++c;
} else { /* Otherwise, try the next direction */
dir = 3;
}
}
if(dir == 3) {
/* If everything's OK, make the move */
if(y - 1 >= 0 && array[x][y - 1] == '.') {
array[x][--y] = ++c;
} else { /* Otherwise, try the next direction */
dir = 1;
}
}
}
/* Print the multi-dimensional array */
for(i = 0; i < 10; i++) {
for(j = 0; j < 10; j++) {
printf(" %c ", array[j]); /* Print the current
character */
}
printf("\n"); /* Print a new line */
}
/* Return 0 upon success */
return 0;
}
----------------
book with which most of you are familiar, as far as I can tell, and
Chapter 8 (Arrays) has this Programming Project (#9) requiring the
following:
---------------
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:
http://drp.ly/1nvue4
[the formatting is poor, so I hope this will do]
Hint: Use the srand() and rand() functions 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, and 3 --
indicating the direction of the next move. 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.
---------------
I've got a code which works, except for the last part: if all the
directions are blocked, it keeps looking for a possible one. I know
I'll have to change the entire loop, but here is how I've nailed it
down. Any comments on the code are greatly appreciated, but do note
that I'm not _supposed_ to know how to write custom functions or make
use of pointers.
----------------
/* Include the standard I/O library */
#include<stdio.h>
/* Include the time library */
#include<time.h>
/* Include the standard C library */
#include<stdlib.h>
/* Define main */
int main(void) {
/* Declare a 10x10 array of characters, initializing it to '.' */
char array[10][10] = {
{'A', '.', '.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.', '.', '.'},
};
/* Declare the loop counters, as well as the direction */
unsigned short int i, j, dir, x = 0, y = 0;
/* Declare a placeholder for the current character, initially 'A'
*/
char c = 'A';
/**
* Seed the current time as the initial value for the random
numbers
* generator
*/
srand((unsigned) time(NULL));
/**
* As long as the current character isn't Z (the last letter of
the
* alphabet), keep moving randomly across the array
*/
while(c != 'Z') {
/**
* Get the remainder of a random number divided by 4 and use
it as
* the direction of the next move
*/
dir = rand() % 4;
/**
* Directions:
* 0 -- up
* 1 -- right
* 2 -- down
* 3 -- left
*/
if(dir == 0) {
/* If everything's OK, make the move */
if(x - 1 >= 0 && array[x - 1][y] == '.') {
array[--x][y] = ++c;
} else { /* Otherwise, try the next direction */
dir = 1;
}
}
if(dir == 1) {
/* If everything's OK, make the move */
if(y + 1 < 10 && array[x][y + 1] == '.') {
array[x][++y] = ++c;
} else { /* Otherwise, try the next direction */
dir = 2;
}
}
if(dir == 2) {
/* If everything's OK, make the move */
if(x + 1 < 10 && array[x + 1][y] == '.') {
array[++x][y] = ++c;
} else { /* Otherwise, try the next direction */
dir = 3;
}
}
if(dir == 3) {
/* If everything's OK, make the move */
if(y - 1 >= 0 && array[x][y - 1] == '.') {
array[x][--y] = ++c;
} else { /* Otherwise, try the next direction */
dir = 1;
}
}
}
/* Print the multi-dimensional array */
for(i = 0; i < 10; i++) {
for(j = 0; j < 10; j++) {
printf(" %c ", array[j]); /* Print the current
character */
}
printf("\n"); /* Print a new line */
}
/* Return 0 upon success */
return 0;
}
----------------