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 27-Feb-2004, 12:55
sanman10535 sanman10535 is offline
New Member
 
Join Date: Feb 2004
Posts: 5
sanman10535 is on a distinguished road

C programming help!!!


Hey guys, I'm having a problem with this part of my assignment, I know I need to construct and array but I'm a little lost on what method I should use. Here are the directions...

Write a program that gen
erates a “random walk” across a 10x10 integer array. The array will contain integers (all 0 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 numbers 1 to 30, in the order visited. Here is an example of the desired output,

1 0 0 0 0 0 0 0 0 0
2 3 4 0 0 0 0 0 0 0
0 0 5 0 0 0 13 14 15 0
0 0 6 7 8 9 12 0 16 0
0 0 0 0 0 10 11 0 17 18
0 0 0 0 0 0 0 0 0 19

0 0 0 0 0 24 23 22 21 20
0 0 0 0 0 25 0 0 0 0
0 0 0 0 0 26 27 28 0 0
0 0 0 0 0 0 0 29 30 0

Hints:

1. Use two variables row and column to store the current position of the walk. Initially row = 0 and column = 0

2. To make sure you randomize the “walk” use this srand and rand functions provided by the stdlib.h library.

For example, you can have the following code for generating the walk,

# include <stdlib.h>
# include <time.h>



main () {

srand ((unsigned) time (NULL)); /* This is to seed the randomizing function. USE THIS FUNCTION ONLY ONCE AT THE START OF MAIN. */

move = rand () % 4; /* This will give you a value between 0 and 3. You can assume that 0 will be going up, 1 will be going right, 2 will be going left, and 3 will be going down. You will have to call this for every move you make */

}

3. You have to make sure you don’t go outside the array. If you have defined the array as int a [10][10] then that means both the subscripts have to be between 0 and 9. If this happens generate another direction using the method described above.

4. You have to make sure you don’t go into an element that has a move assigned. For example, suppose you are moving into element a[2][3]. You can check whether there has been a move in a[2][3] by using the following code,

if (a[2][3] == 0) {

/* There hasn’t been a move */
}

Again, if you do move into an element that has a move assigned, make another move.

5. If all four directions are blocked, the program must terminate. You can use an easy way to find out whether the four directions are blocked. Make sure before you make every move you check the four directions you can move to for the conditions. A premature termination because all four directions are blocked look like this,

1 0 0 12 11 0 0 0 0 0
2 3 4 13 10 0 0 0 0 0
0 0 5 14 9 0 0 0 0 0
0 0 6 7 8 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0

6. Break up the jobs into as many functions as possible. For example you can have the following break up,

initialize (int a[][]) /* Initializes the elements of array a to 0 */
check (int a[][], int r, int c) /* Checks whether a [r][c] is within the required boundaries or that there hasn’t already been a walk on a[r][c]. Returns 1 if the a[r][c] is a valid move, and 0 if a[r][c] is not a valid move */
move () /* Generate a move between 0 and 3 as described in part 2 */

You can think of some of your own.

Call this program walk.c. You will be graded on design for this question. Try to come up with a nice break up of the code. Also, for the output print the array as shown above.
  #2  
Old 27-Feb-2004, 13:17
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,623
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
If you have questions, this is a great place to get information
from people with different levels of experience. One question could
be, "How can I construct a two-dimensional array in C."

So, is that the question? How to construct a two-dimensional array
in C? Try

CPP / C++ / C Code:
int a[10][10];

What else?

Dave
  #3  
Old 27-Feb-2004, 13:30
Garth Farley Garth Farley is offline
Invalid Email Address
 
Join Date: May 2002
Location: Ireland
Posts: 638
Garth Farley is a jewel in the roughGarth Farley is a jewel in the roughGarth Farley is a jewel in the rough
We don't mean to be critical, but it seems that you are asking us to code your assignment for you. And I'm afraid that just ain't gonna happen

It's *your* assignment, try figuring out exactly what's needed. You've got to understand a 2D array, and how to move about in it. How will you use random numbers to make you go up, down, left or right in the array?

Don't forget you've got to look at where you are moving to in case you (a) try to go over the edge of the array or (b) overwrite a non-zero number you've already set.

We'll be delighted to answer any coding questions you have, but we'll only put in the work when it's evident that you have too.
GF
  #4  
Old 27-Feb-2004, 14:26
sanman10535 sanman10535 is offline
New Member
 
Join Date: Feb 2004
Posts: 5
sanman10535 is on a distinguished road
Quote:
Originally Posted by Garth Farley
We don't mean to be critical, but it seems that you are asking us to code your assignment for you. And I'm afraid that just ain't gonna happen

It's *your* assignment, try figuring out exactly what's needed. You've got to understand a 2D array, and how to move about in it. How will you use random numbers to make you go up, down, left or right in the array?

Don't forget you've got to look at where you are moving to in case you (a) try to go over the edge of the array or (b) overwrite a non-zero number you've already set.

We'll be delighted to answer any coding questions you have, but we'll only put in the work when it's evident that you have too.
GF

Ni I don't anyone to do MY assignment for me. I am very eager and want to learn C but this problem is haunting everyone in my class and we're all having the same problem.
  #5  
Old 27-Feb-2004, 16:53
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
Here's a general tip for coding anything, especially larger programs. Take it one step at a time. Your solution should be composed of a series of steps to complete the problem. Here's a quick example...

A baseball player needs to hit a ball...
1. Pick up bat
2. Get in stance
3. Wait for ball
4. Swing

Each of these steps might be broken down into smaller sub-steps, like this:
1a. Bend down
1b. Grip bat
1c. Stand up

2a. Spread legs to shoulder width
2b. Square shoulders
2c. Raise elbows

3a. Watch for pitching motion
3b. Track ball movement

4a. Adjust stance according to ball position
4b. Step forward
4c. Bring bat around
4b. Stop swinging
4d. Did we hit the ball?
If so, run to first base
else, begin random cursing

As you can see, I have broken the problem of hitting the baseball into a series of more simple steps. If you try to do the entire thing at once, you might become confused as to what to do next to solve your problem. Break it down and decide exactly what needs to be done to solve the problem, then write the code.
 
 

Recent GIDBlogNARMY 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
some Great site for programming issues priyanka Web Design Forum 8 19-Dec-2005 08:55
question of practice magiccreative CPP / C++ Forum 1 06-Feb-2004 07:17
Web and Flash design. PHP, MySQL, ASP, JSP programming. artem Web Design Forum 4 28-Apr-2002 04:36

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

All times are GMT -6. The time now is 14:59.


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