2 Dimensional Array/ Random Walk Sim

I've been working on an assignment for my class and can't figure this one out. All I need is some help in figuring out how to display the number of times the spot has been visited. Here's the assignment and the code. Thanks in advance for any help!


[1] Objectives: The main purpose of this assignment is to make sure you are familiar with using arrays (in particular, two dimensional arrays). In addition, it is important that you know how to use functions to structure your program.
[2] Description: We are going to simulate a random walk by using the random number generator that we have been using for a while by now. The walk is taken place on a 2 dimensional board of size n x n where n =15 is a fixed constant. You may want to test your program starting with a smaller n. Here are some rules.
• You start out at the center of the board. You should memorize your current position.
• You are allowed to walk in one of the four directions (1-E, 2-W, 3-N, and 4-S) from your current position.
• The walk is totally memory-independent, so you repeat the process again from the current position.
• The user is asked interactively on the number of steps to walk starting from your current position. A zero indicates termination of the walk.
• You are not allowed to walk off the board ("hit the wall"). If that happens, the walk is over.
• You should show every step (the coordinate of each action) of your walk so that we can know them whole path.
Suppose we have a good random number generator, our current position should hover around the starting position. The chance of hitting the "wall" is small but nonzero. The probability decreases as the size in-creases. Does the simulation agree with this prediction? We shall keep a counter for each location of the board to store the number of times the location was “visited.” The number is increased by 1 every time you step on that location.
[3] Input: A seed for the random number generator once, and the number of steps to walk (repeat).
[4] Output: Every time before you ask for another input of number of steps, you want to inform the user the current position of you and the counters. Two examples are shown below.
Random Walk Simulation
Seed: 800
Number of steps (0 to stop): 1
Coordinate: (6, 5)
Total Steps: 1
........... 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
....X...... 0 0 0 0 1 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
Number of steps (0 to stop): 20
Coordinate: (7, 5)
Coordinate: (7, 4)
Coordinate: (8, 4)
Coordinate: (8, 5)
Coordinate: (7, 5)
Coordinate: (7, 6)
Coordinate: (6, 6)
Coordinate: (6, 7)
Coordinate: (6, 8)
Coordinate: (7, 8)
Coordinate: (7, 9)
Coordinate: (7, 8)
Coordinate: (6, 8)
Coordinate: (6, 7)
Coordinate: (7, 7)
Coordinate: (8, 7)
Coordinate: (9, 7)
Coordinate: (10, 7)
Total Steps: 21
........... 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 1 1 2 2 0 0 0
........... 0 0 0 1 2 1 1 3 2 0 0
........... 0 0 0 1 1 0 1 0 0 0 0
........... 0 0 0 0 0 0 1 0 0 0 0
......X.... 0 0 0 0 0 0 1 0 0 0 0
........... 0 0 0 0 0 0 0 0 0 0 0
Number of steps (0 to stop): 40
Coordinate: (9, 7)
Coordinate: (8, 7)
Coordinate: (9, 7)
Coordinate: (8, 7)
Coordinate: (9, 7)
Coordinate: (9, 6)
Coordinate: (10, 6)
Coordinate: (10, 5)
Coordinate: (10, 6)
Coordinate: (10, 7)
Coordinate: (9, 7)
Coordinate: (9, 6)
Coordinate: (9, 7)
Coordinate: (9, 8)
Coordinate: (10, 8)
Coordinate: (10, 9)
Coordinate: (11, 9)
Total Steps: 38
........... 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 1 1 2 2 0 0 0
........... 0 0 0 1 2 1 1 3 2 0 0
........... 0 0 0 1 1 0 3 0 0 0 0
........... 0 0 0 0 0 2 6 1 0 0 0
........... 0 0 0 0 1 2 2 1 1 0 0
........X.. 0 0 0 0 0 0 0 0 1 0 0
The Random Walk is Over in 38 steps.
Press any key to continue . . .


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include<iostream>
#include<cstdlib>

using namespace std;

const int N = 5;

int main() {

    int board[N][N];
    int direction[4] = {1,2,3,4};
    //east = 1, west = 2, north =3, south = 4
    int totalSteps = 0, numSteps, curPos, startPos, dir, seed, visited = 0, row = 0, col = 0;
    //bool visited = false;
    
    startPos = 8;
    curPos = startPos;
    
    cout<<"Random Walk Simulation"<<endl;
    cout<<"Seed: ";
    cin>>seed;
    cout<<"Number of Steps (0 to stop): ";
    cin>>numSteps;
    cout<<"Current Position: "<<endl;
    totalSteps += numSteps;
    cout<<"Total Steps: "<< totalSteps<<endl;
    
    board[row][col] = 0;
    
    srand(seed);
    
    for(int cnt = 0; cnt < numSteps; cnt++) {
        dir = rand() %4 +1;
        if(dir == 1) {
            board[curPos][++curPos] = ++visited;
            //visited = true;
        }
        else if(dir == 2) {
            board[curPos][--curPos] = ++visited;
        }  
        else if(dir == 3) {
            board[--curPos][curPos] = ++visited;
        }  
        else if(dir == 4) {
            board[++curPos][curPos] = ++visited;
        }
                   
    }
 
    for(row = 0; row < N; row++) {
        board[row][col] = 0;
        if(board[row][col] == board[curPos][curPos]) {
            cout<<board[row][col]<<" ";
        }
        else {
            cout<<visited;
        }
        for(col = 0; col < N - 1; col++) {
            board[row][col] = 0;
            if(board[row][col] == board[curPos][curPos]) {
            cout<<board[row][col]<<" ";
            }
            else {
                cout<<visited;
            }
        }
        cout<<endl;
    } 

   system("pause");
   return 0;
}
Topic archived. No new replies allowed.