Monday, July 27, 2009

I need help with this C++ task...i cant understand it...any help??

A (drunken) cockroach is placed on a given square in the middle of a tile floor in a rectangular room of size n x m tiles. The bug wanders (possibly in search of an aspirin) randomly from tile to tile throughout the room. Assuming that he may move from his present tile to any of the eight tiles surrounding him (unless he is against a wall) with equal probability, how long will it take him to touch every tile on the floor at least once?


Hard as this problem may be to solve by pure probability theory techniques, the answer is quite easy to solve using the computer. The technique for doing so is called "simulation" and is of wide-scale use in industry to predict traffic-flow, inventory control and so forth. The problem may be simulated using the following method:


An array **** dimensioned N X M is used to represent the number of times our cockroach has reached each tile on the floor. All the cells of this array are initialized to zero. The position of the bug on the floor is represented by the coordinates (IBUG,JBUG) and is initialized by a data card. The 8 possible moves of the bug are represented by the tiles located at (IBUG + IMVE(K), JBUG + JMVE(K)) where 1 K 8 and:


IMVE(1) = -1 JMVE(1) = 1IMVE(2) = 0 JMVE(2) = 1IMVE(3) = 1 JMVE(3) = 1IMVE(4) = 1 JMVE(4) = 0IMVE(5) = 1 JMVE(5) = -1IMVE(6) = 0 JMVE(6) = -1IMVE(7) = -1 JMVE(7) = -1IMVE(8) = -1 JMVE(8) = 0A random walk to one of the 8 given squares is simulated by generating a random value for K lying between 1 and 8. Of course the bug cannot move outside the room, so that coordinates which lead up a wall must be ignored and a new random combination formed. Each time a square is entered, the count for that square is incremented so that a non-zero entry shows the number of times the bug has landed on that square so far. When every square has been entered at least once, the experiment is complete.


Write a program to perform the specified simulation experiment. Your program MUST:


1) Handle values of N and M


2 %26lt; N 40


2 M 20


2) Perform the experiment for


a) N = 15, M = 15 starting point: (20,10)


b) N = 39, M = 19 starting point: (1,1)


3) Have an iteration limit, that is, a maximum number of squares the bug may enter during the experiment. This assures that your program does not get "hung" in an "infinite" loop. A maximum of 50,000 is appropriate for this lab.


4) For each experiment print: a) the total number of legal moves which the cockroach makes; b) the final **** array. This will show the "density" of the walk, that is the number of times each tile on the floor was touched during the experiment

I need help with this C++ task...i cant understand it...any help??
Ok I am developing it . Hope that you will get a reply soon.
Reply:Homework? Ask at appropriate newsgroup: you will get full answer -- or they will start firewar :-)


No comments:

Post a Comment