![]() |
|
#1
|
|||
|
|||
Generate a "random walk" across a 10x10 arrayHelp! 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:
This is what I have so far: CPP / C++ / C Code:
Last edited by admin : 21-Feb-2008 at 14:45.
Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
|
|
#2
|
|||
|
|||
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
|
|||
|
|||
Re: walk.c program ECS 30 help!Quote:
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:
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:
Regards, Dave |
Recent GIDBlog
More photos on Flickr by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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