Simulations with 2D arrays

Please be as verbose and patient as possible. I am still very new.

Problem:
A local government organization has been testing a new biological triggering device. The actual cost has gotten out of hand and they have recruited you to test the new style trigger through ‘simulation’ to reduce their cost. How does the trigger work?
The trigger device is a 20 by 20 grid where each square is a sensor. Each square in the grid starts with one flea. (The flea is the biological part) All the fleas jump at the same time and land on a neighboring square or the current square. So some squares now have more than one flea and other squares blank or without a flea. The fleas continue to jump and at some point when 73% of the squares are blank (no fleas) the trigger is fired. If the flea jumps off the 20 x 20 grid, the flea is zapped and will no longer be able to jump.
Using actual fleas and the termination of the fleas during testing became the big cost over run the government was having to deal with. Zapping electronic fleas is cheaper than real fleas.
Now to simulate this process, we need to know which way a given flea will jump. A flea will only jump to a neighboring square and never two or more squares over. (These fleas are breed with shorter legs so they can’t jump as far.) Now after much research, it was determined which direction a flea will jump. There are nine places/squares a flea can land, the neighboring square or the square the flea is currently jumping from. A flea will jump in one of the corner squares 5% of the time. Each corner is 5%. The top, bottom, left and the current square the flea will jump in 15% of the time and to the right 20% of the time. 20% direction is the way the dog’s hair grows and the fleas jump with the hair a little more often than in the other directions. A flea is in the middle square and the directions the flea will jump are shown below.

5 15 5
15 15 20
5 15 5

A simulation consists of setting one flea on each square then letting each flea jump. After each jumps check to see if 73% of the squares are empty. If they are not, then let each flea jump again. You continue letting the fleas jump until 73% of the squares are empty and the simulation stops, ie the bomb is detonated. Now since jumping is random movements, running one simulation will not be enough information needed to accomplish this task. You will need to run the simulation again and again to get an average. You will need to run the simulation 2500 times and then compute the following resulting values.

Inputs: None
Outputs:
1. How many times will the fleas jump before the trigger is fired? (avg)
2. If fleas jumps 4 times every 6 seconds, determine how much time does one have to clear the area before the trigger is fired?
3. What is the shortest time and the longest time you computed, given all simulations runs, which is the time needed to clear the area?
4. How many fleas on the average were lost before the trigger was fired? (This is to show the government the cost savings for using simulation rather than using real fleas in a simulation.)

Restrictions: Use 2-d arrays(2). Run the simulation 2500 times to generate the results.

Output: Format output in a readable style.

*Ok, we have to have 2 2D arrays, (ex. Grid1[20][20] and grid2[20][20])
He mentions in the lecture that the fleas will jump from grid 1 to grid 2 so grid 1 needs to be initialized with all 1's and grid 2 with 0's first. He suggests a function jump to use random numbers. So jump(a, b) and then zero out one array then jump(b, a) and repeat 2500 times. He also said we can call copy() for b,a.

Another thing he mentioned was to make the arrays 22x22 instead so that the outer rim is where you can count the number of fleas zapped.

*******we have not learned pointers, vectors, classes, etc. We have only learned basics, user-created functions (a few exceptions), and arrays.******

I have written the jump function, but my issue is how once you get your random numbers then check by the if statements, you move grid1 to grid 2 and sum. I tried writing it, but its not working. I thought I could sum it.
Someone has already solved this using 1 2d array so I tried to implement what they did with 2, but I can't figure out what I need to do.

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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
void initialize(int grid[][22], int num) 
{
    int r, c;
    for(r=0;r<20;r++)
    {
        for(c=0;c<20;c++)
        {
            grid[r][c]=num;
        }
    }
}

void jump(int grid1[][22], int grid2[][22])  //the flea jumping 
{
    int randNum, row, col;
    for(row=0;row<20;row++)
    {
        for(col=0;col<20;col++)
        {
    
           randNum=rand()%100+1;  //getting random numbers between 1-100
    
 
           //jumping on the squares 
           if((randNum>=1)&&(randNum<=5)) //jump up and left (5%)
            {
               grid2[row-1][col-1]+=grid1[row][col];
            }

           else if((randNum>=6)&&(randNum<=10))  //jump up and right (5%)
           {
                grid2[row-1][col+1]+=grid1[row][col];
           }

           else if((randNum>=11)&&(randNum<=15)) //jump down and right (5%)
           {
                grid2[row+1][col+1]+=grid1[row][col];
           }

           else if((randNum>=16)&&(randNum<=20)) //jump down and left (5%)
            {
                grid2[row+1][col-1]+=grid1[row][col];
            }

           else if((randNum>=21)&&(randNum<=35))  //jump left (15%)
            {
                grid2[row][col-1]+=grid1[row][col];
            }

           else if((randNum>=36)&&(randNum<=50)) //jump up (15%)
            {
                grid2[row-1][col]+=grid1[row][col];
            }

           else if((randNum>=51)&&(randNum<=65))  //jump down (15%)
            {
                grid2[row+1][col]+=grid1[row][col];
            }

           else if((randNum>=66)&&(randNum<=80))//stay in the same place (15%)
            {
                grid2[row][col]+=grid1[row][col];
            }

           else if((randNum>=81)&&(randNum<=100)) //jump right (20%)
            {
                grid2[row][col+1]+=grid1[row][col]; 
            }
           randNum=0;
        }

     }
    }


initialize(grid1, 1);    //initializing both grids 
initialize(grid2, 0);

//jump 
jump(grid1, grid2);


for(row=0;row<20;row++)
{
    for(col=0;col<20;col++)
    {
        cout<<grid2[row][col]<<" ";  //just to look at what squares were filled.
    }
}



//how someone else did it. They used global variables, we are not allowed to do that. 
void jump(int grid[][22], int r, int c) 
{
    --grid[r][c];   //I don't understand why they did this here first 
  int randNum=rand()%100+1;

if (randNum >= 1 && randNum <= 5) 
 {
   --r; --c;
 } 
   
else if (randNum >= 6 && randNum <= 20) 
  {
    --r;
  } 

else if (randNum >= 21 && randNum <= 25) 
  {
     --r; ++c;
  } 
  
else if (randNum >= 26 && randNum <= 40) 
  {
   --c;
  } 
  
else if (randNum >= 41 && randNum <= 55) 
  {

  } 
  
else if (randNum >= 56 && randNum <= 75) 
  {
    ++c;
  } 
  
else if (randNum >= 76 && randNum <= 80) 
  {
   ++r; --c;
  } 
  
else if (randNum >= 81 && randNum <= 95) 
  {
   ++r;
  } 
  
else 
  {
   ++r; ++c;

  }

if (r >= 0 && c >= 0 && r < 20 && c < 20)
{
  ++grid[r][c];  //not understanding this part either 
}

}
Last edited on
Your code only, yes?:
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
73
74
75
void initialize(int grid[][22], int num) 
{
    int r, c;
    for(r=0;r<20;r++)
    {
        for(c=0;c<20;c++)
        {
            grid[r][c]=num;
        }
    }
}

void jump(int grid1[][22], int grid2[][22])  //the flea jumping 
{
    int randNum, row, col;
    for(row=0;row<20;row++)
    {
        for(col=0;col<20;col++)
        {
    
           randNum=rand()%100+1;  //getting random numbers between 1-100
    
 
           //jumping on the squares 
           if((randNum>=1)&&(randNum<=5)) //jump up and left (5%)
            {
               grid2[row-1][col-1]+=grid1[row][col];
            }

           else if((randNum>=6)&&(randNum<=10))  //jump up and right (5%)
           {
                grid2[row-1][col+1]+=grid1[row][col];
           }

           else if((randNum>=11)&&(randNum<=15)) //jump down and right (5%)
           {
                grid2[row+1][col+1]+=grid1[row][col];
           }

           else if((randNum>=16)&&(randNum<=20)) //jump down and left (5%)
            {
                grid2[row+1][col-1]+=grid1[row][col];
            }

           else if((randNum>=21)&&(randNum<=35))  //jump left (15%)
            {
                grid2[row][col-1]+=grid1[row][col];
            }

           else if((randNum>=36)&&(randNum<=50)) //jump up (15%)
            {
                grid2[row-1][col]+=grid1[row][col];
            }

           else if((randNum>=51)&&(randNum<=65))  //jump down (15%)
            {
                grid2[row+1][col]+=grid1[row][col];
            }

           else if((randNum>=66)&&(randNum<=80))//stay in the same place (15%)
            {
                grid2[row][col]+=grid1[row][col];
            }

           else if((randNum>=81)&&(randNum<=100)) //jump right (20%)
            {
                grid2[row][col+1]+=grid1[row][col]; 
            }
           randNum=0;
        }

     }
    }




I would think this is taking all the fleas and moving them to the new square, so that what you want?
1
2
#line 27
grid[row-1][col-1]+=grid[row][col];


Maybe loop though the randNum generation and if statements for every flea on a square.

What is the problem with the jump function specifically?


Its hard to explain Haha. So all the fleas are on grid 1. 20x20=400 fleas. Everytime rand is called whatever the number is, they move in 1 of 9 directions. So the ones on row 0 could potentially fall off and would be counted as dead. He said that while(numsquares<73%) empty, keep running the simulation. I dont know if I'm doing it right because my code, no output happens. Basically how do I move a 1 in grid 1 to grid 2 only certain times and multiple 1s can be added to each. Ugh this is a hard one...
I'll show my output when I get home, but from what I remember, it is only reading the first random number which is between 21-35 and filling different spots in grid 2 with 1.
As of right now it is just printing so many spots and then failing. I have cout<<"num range""<<endl; to show what random number its trying to print on each if statement


21-35
36-50
51-65
21-35
1-5
6-10
51-65
51-65
51-65
16-20
11-15
51-65
36-50
6-10
6-10
81-100

RUN FAILED (exit value 1, total time: 870ms)




Topic archived. No new replies allowed.