# Problem with C++ seemigly skipping lines in program for calculating thermal equilbirum distrobutions with Monte Carlo Alogrithim

I am writing a piece of code as a part of my linear algebra project. its supposed to use a Monte Carlo algorithm known as the random walk property to calculate thermal equilibrium distributions for a body in static thermal equilibrium. I'm a mechanical engineering student and I have never taken a computer science class so everything on the code is a result of teaching myself so please excuse it since I understand its a Frankenstein of a program.

Basically the program takes a a specified point on a grid and based of a random input moves it left,right,up,down until it reaches a boundary point on that grid and then throws the number it landed on into a vector and repeats until the number of values in the vector are equal to the number of predefined iterations. The program isn't finished but the problem I'm having is that I want the program to "reset" back to the starting point once it hits a boundary point. This seems to work for when the number or iterations is small (1-5) but for anything higher it as thought the program skips that step... I have the program print the "coordinates" of where it is on the grid and my intention was that it will never be a number that is not within the grid but that doesn't seem to be the case for when the iterations is a large.

```
#include <iostream>
#include <stdlib.h>
#include <array>
#include <algorithm>
#include <iterator>
#include <random>
using namespace std;
int main () {
int grid[5][5] = { //makes grid, this will be the model of the body that we want to find temperatues for
{2, 0, 0, 0, 0},
{2, 0, 0, 0, 0},
{2, 0, 0, 0, 0},
{2, 0, 0, 0, 0},
{1, 1, 1, 1, 1}
};
srand(time(NULL)); //seeds the random number generator
int x=2; // starting 'x' position
int y=3; // starting 'y' position
vector<int> values; // vector that will store values of boundry points once reached
int iterations = 7; // number of iterations
auto numberOfValues = values.size(); // how many numbers in the vector (how many times has the program reached the boundry points)
while(numberOfValues != iterations) {
int randomPath = rand()%4; // get four random numbers to define to move left, right, up, or down
switch (randomPath)
{
case 0:
x++;
cout <<"right"<<endl;
break;
case 1:
x--;
cout <<"left"<<endl;
break;
case 2:
y++;
cout <<"up"<<endl;
break;
case 3:
y--;
cout <<"down"<<endl;
break;
}
cout<<x<<","<<y<<endl;
//code below defines that when boundry coordinate is reached, put that value in the vector
if((x== 0 && y== 0)||(x== 0 && y== 1)||(x== 0 && y== 2 )||(x== 0 && y== 3)||(x== 0 && y== 4)||(y == 0 && x == 1)||(y == 0 && x == 2)||(y == 0 && x == 3)||(y == 0 && x == 4)){
values.push_back(grid[y][x]);
numberOfValues++; //increase the number of values in the vector by 1
x = 2; // reset x position
y = 3;//reset y position
}
cout <<numberOfValues<<endl;
}
return 0;
}
```

**Tl;DR**
For large numbers of the iterations variable, the programs seems to skip this part of the code: (specifically the x=2,y=3 part)

```
if((x== 0 && y== 0)||(x== 0 && y== 1)||(x== 0 && y== 2 )||(x== 0 && y== 3)||(x== 0 && y== 4)||(y == 0 && x == 1)||(y == 0 && x == 2)||(y == 0 && x == 3)||(y == 0 && x == 4)){
values.push_back(grid[y][x]);
numberOfValues++; //increase the number of values in the vector by 1
x = 2; // reset x position
y = 3;//reset y position
}
```

Note: Code was written using Visual Studio Code on Ubuntu Note2: This is my first time posting on this site, if I have done anything wrong in terms of format please inform me as to avoid repeating it.

**Thank you in advance for your help**

### 1 Answer

I have the program print the "coordinates" of where it is on the grid and my intention was that it will never be a number that is not within the grid but that doesn't seem to be the case for when the iterations is a large.

It is happening because your boundary-test is not covering all cases. You seem to have hard-coded individual tests for every point, but after all that effort you are only covering two edges.

The following (and much simpler) test will tell you if you are on any edge:

```
if (x == 0 || x == 4 || y == 0 || y == 4)
```

Notice also that if you chose to change the dimensions of your grid, it would be much easier to adjust your boundary test. In fact, it's better practice to define the dimensions as a constant instead of a literal:

```
const int GRID_WIDTH = 5;
const int GRID_HEIGHT = 5;
int grid[GRID_HEIGHT][GRID_WIDTH];
// ...
if (x == 0 || x == GRID_WIDTH - 1 || y == 0 || y == GRID_HEIGHT - 1)
```

tl;and to badly formatted;dr