GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 21-Feb-2008, 12:16
thunderchic5 thunderchic5 is offline
New Member
 
Join Date: Feb 2008
Posts: 1
thunderchic5 is on a distinguished road
Exclamation

Generate a "random walk" across a 10x10 array


Help! I can't seem to figure this program out and I've been working on it for over 7 hours! If you could take a look, that would be wonderful!

Problem 13 from the book is: Write a program that generate 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, right by one element. The elements
visited by the program will be labelled with the letters A through Z, in the order visited. Here's an example:

Code:
A . . . . . . . . . B C D . . . . . . . . F E . . . . . . . H G . . . . . . . . I . . . . . . . . . J . . . . . . . . . K . . R S T U V Y . L M P Q . . . W X . . N O . . . . . . . . . . . . . . . . .

This is what I have so far:

CPP / C++ / C Code:
# include <stdio.h>
# include <time.h>
# define TRUE 1
# define FALSE 0

time_t seed;

main()
{
seed = time(NULL);
srand(seed);

char grid[10][10];
char currentchar = 'A';

int i, j, r, loop, row, col;
for (i=0; i<10; i++) {
	for (j=0; j<10; j++) {
		grid[i][j]='.';
		grid[0][0]='A';
		printf("%c", grid[i][j]);
	}
	printf("\n");
}
printf("\n");

loop=1;
i=0; j=0;
row=i; col=j;
printf("%d %d, %d %d\n", i, j, row, col);
while (loop==1) {

for (;;) {
	r = rand()%4;
	printf("%d , %d %d\n", r, row, col);
	if (currentchar == 'Z') {
		printf("Zdone!\n"); loop = 0; break;
	}
	else if ((((j-1) > -1) && grid[i][j-1] != '.' || grid[i][j-1] < currentchar) && (((i-10) > -1) && grid[i-10][j] != '.' || grid[i-10][j] < currentchar) && (((j+1) < 10) && grid[i][j+1] != '.' || grid[i][j+1] < currentchar) && (((i+10) < 100) && grid[i+10][j] != '.' || grid[i+10][j] < currentchar)) {
		loop = 0; break;
	}
	else if ((((j-1) > -1) && grid[i][j-1] != '.' || grid[i][j-1] < currentchar) && (((i-10) > -1) && grid[i-10][j] != '.' || grid[i-10][j] < currentchar) && (((j+1) < 10) && grid[i][j+1] != '.' || grid[i][j+1] < currentchar)) {
		r = 3;
	}
	else if ((((j-1) > -1) && grid[i][j-1] != '.' || grid[i][j-1] < currentchar) && (((i-10) > -1) && grid[i-10][j] != '.' || grid[i-10][j] < currentchar)) {
		r = rand()%2;
	}
	else if ((((j-1) > -1) && grid[i][j-1] != '.' || grid[i][j-1] < currentchar)) {
		r = rand()%3;
	}

	if (r == 0) {
		printf("This is 0) %d %d\n", i, j);
		if (((j-1) > -1) && grid[i][j-1] == '.') {
		printf("ye!0");
			row=i; col=(j-1);
			printf("%d %d %d %d\n", i, j, row, col);
			grid[row][col] = currentchar++;
			for (i=0; i<10; i++) {
				for (j=0; j<10; j++) {
					grid[i][j]='.';	
					grid[row][col] = currentchar;	
					printf("%c", grid[i][j]);
				}
				printf("\n");
			}
			printf("\n");
		}
		else i=row; j=col; continue;
	}
	if (r == 1) {
		printf("This is 1) %d %d\n", i, j);
		if (((i-10) > -1) && grid[i-10][j] == '.') {
		printf("ye!1");
			row=(i-10); col=(j);
			printf("%d %d %d %d\n", i, j, row, col);
			grid[row][col] = currentchar++;
			for (i=0; i<10; i++) {
				for (j=0; j<10; j++) {
					grid[i][j]='.';	
					grid[row][col] = currentchar;	
					printf("%c", grid[i][j]);
				}
				printf("\n");
			}
			printf("\n");
		}
		else i=row; j=col; continue;
	}
	if (r == 2) {
		printf("This is 2) %d %d\n", i, j);
		if (((j+1) < 10) && grid[i][j+1] == '.') {
		printf("ye!2");
			row=i; col=(j+1);
			printf("%d %d %d %d\n", i, j, row, col);
			grid[row][col] = currentchar++;
			for (i=0; i<10; i++) {
				for (j=0; j<10; j++) {
					grid[i][j]='.';	
					grid[row][col] = currentchar;	
					printf("%c", grid[i][j]);
				}
				printf("\n");
			}
			printf("\n");
		}
		else i=row; j=col; continue;
	}
	if (r == 3) {
		printf("This is 3) %d %d\n", i, j);
		if (((i+10) < 100) && grid[i+10][j] == '.') {
		printf("ye!3");
			row=(i+10); col=(j);
			printf("%d %d %d %d\n", i, j, row, col);
			grid[row][col] = currentchar++;
			for (i=0; i<10; i++) {
				for (j=0; j<10; j++) {
					grid[i][j]='.';	
					grid[row][col] = currentchar;	
					printf("%c", grid[i][j]);
				}
				printf("\n");
			}
			printf("\n");
		}
		else i=row; j=col; continue;
	}
}
}
}
Last edited by admin : 21-Feb-2008 at 14:45. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
  #2  
Old 21-Feb-2008, 12:48
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 479
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: walk.c program ECS 30 help!


Looks like fun. Is there a specific question or something that you're stuck on? Also, please use C tags around your code. I refuse to read through it as is.
  #3  
Old 21-Feb-2008, 14:43
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,693
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: walk.c program ECS 30 help!


Quote:
Originally Posted by thunderchic5
Help! I can't seem to figure this program out ...

We define a grid in terms of rows and columns: an array[rows][cols]
of chars.

We arbitrarily define row zero to be the "top" edge, column zero is the "left" edge, etc.

A move in the "down" direction is accomplished by incrementing the current row index. Moving "right" means incrementing the column
index. Etc.


Given a grid and a starting row and column mumber and a character that has been placed there, here's the logic that I might think about:

Code:
While the character is less than or equal to 'Z' and a move is possible Begin the big loop Set a variable "moved" to zero to indicate that no move has been made. While the value of the variable "moved" is equal to zero Begin the "move" loop Get a random direction (left, right, up, down) If it is possible to move in that direction, then Begin the If stuff change the current row or column index in the indicated direction place the character. increment the character. Set the variable "moved" to one to indicate that a move was made. End of the If stuff. End of the "move" loop. End of the big loop.
There are two ways to leave the big loop:

Success is indicated by the fact that the character has been incremented to one more than 'Z'

If the loop quit early because of a dead end, the values of row and column can be used to tell the user where the last character was placed.

Now, looking at that orginization, what do we need?

1. Decide on what direction is indicated by numbers 0, 1, 2, 3. You can simply #define LEFT to be 0, UP to be 1, etc. (in any order that you want). A neat thing from a program point of view might be to use enums if you have had them. (Don't worry if you haven't; just use integer values.)

2. You need to know how to figure whether you can move in any given direction:

For example, given position (row, col) you can move left if (and only if)
(row > 0) and (grid[row-1][col] == '.'). Figure out and write down the other possible moves.

Simply "or"ing together the return values of the four directional move functions tells us whether any move is possible (the thing you need for the big loop).

In order to keep from having long drawn-out, easy-to-screw-up logic expressions, I would probably make functions for the possible moves:

CPP / C++ / C Code:
int can_move_left(char g[][10], int r, int c)
{
    return (c > 0) && (g[r][c-1] == '.');
}
int can_move_right(...)
{
.
.
.
}
.
.
.
int can_move(char g[][10], int r, int c)
{
    return can_move_up   (g, r, c) || 
           can_move_down (g, r, c) || 
           can_move_right(g, r, c) ||
           can_move_left (g, r, c);
}


Regards,

Dave
 
 

Recent GIDBlogMore photos on Flickr by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Two-Tier data dissemination code installation problem nidhibansal1984 Computer Software Forum - Linux 6 16-Sep-2007 10:13
Text-Based Roulette Game mfm1983 C++ Forum 5 29-Nov-2006 12:20
BOOKEEPING program, HELP!! yabud C Programming Language 10 17-Nov-2006 03:48
Pipeline freeze simulation darklightred C++ Forum 6 27-Jul-2006 19:37
How to read particular memory location ? realnapster C Programming Language 10 10-May-2006 09:11

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 17:19.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.