×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

two dimensional array, matrix

two dimensional array, matrix

two dimensional array, matrix

(OP)
can any body give me the idea to solve this problem, not the whole answer.thanks,

Use a matrix represents an island surrounded by water. Two bridges lead out of
the island. A mouse is placed on the black square. Write a program to make the
mouse take a walk across the island. The mouse is allowed to travel one square
at a time, either horizontally or vertically. A random number from 1 through 4
should be used to decide which direction the mouse is to take. The mouse drowns
when he hits the water; he escapes when he enters a bridge. You may generate a
random number up to 100 times. If the mouse does not find his way by the
hundredth try, he will die of starvation. Restart the mouse in a reinitialized
array and go back and repeat the whole process. Count the number of times he
escapes, drowns, and starves.
Input File
1. First input line - the size of the array, including border of water and
bridges (not larger than 20 X 20)
2. Next N input lines-the rows of the two-dimensional array, where the
positions containing negative numbers represent the water, the positions in
the edge containing a 0 represent the bridges, the position containing a 1
represents the starting position of the mouse, and all other positions
contain 0s.
Output
1. A line stating whether the mouse escaped, drowned, or starved
2. A line showing the mouse's starting position and the position of the two
bridges
3. A map showing the frequency of the mouse's visits to each position

RE: two dimensional array, matrix

Break it into little steps and solve each step in turn.

1) Start by declaring a 2D array and reading in the array as specified in Input 1 and input 2. Call this map. Seed your random number generator (use srand)

2) Look for the position containing a 1 and print the row and column and save it.

3) Declare a second 2D array and set all values to zero. Call this frequency.

4) Generate a random number (use rand)

5) If 1 move 1 row up, 2, move one column right, 3, move one row down, 4 move one column left.

6) Increment position in frequency. Check the location in the array where the mouse will end up on map.

7) Repeat from step 4 for 100 times until either the map position is 0 or -1.

8) if last map position is 0, mouse escaped, 1, mouse starved, -1, mouse drowned (output 1)

9) Print position saved in step 2 (output 2)

10) Print the frequency array (output 3)

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close