15,610,483 members
See more:
Hi! I'm working on the Monte Carlo simulation of the NO-CO reaction on a square surface, and when I was writing the code, I needed to specify the states of "empty" or "occupied" of the sites on the surface that CO or NO can occupy.

In my code I defined those states as follows
C
```#define empty 0
#define occupied 1```

So, if the site is "occupied", the chosen site is written as
C
`grid[x][y] = occupied;`

But when we go to check if this is effectively true, I can't do something like
C
```if(grid[x][y] = occupied)
{
thing 1;
thing 2;
}```

Since I will obtain a warning or error message. I know that if, while statements work with "==", "!=", "<", ">", but I really need to check if a chosen site of my simulation is or not occupied and I'm not sure how to write it in C

What I have tried:

The following is the code that I've been writing so far.
I've defined some booleans but I don't know if I'am using them correctly. All that I need is to know how to check properly if a chosen site of my simulation is or not occupied.

C
```#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>

//define the size of the grid
#define xmax 3
#define ymax 3
//define the Monte Carlo Steps
#define MCSS 100
//define the states "empty" and "occupied"
#define empty 0
#define occupied 1
//define if the chosen site is occupied by por CO, NO, N, or O
#define co_occupied 2
#define no_occupied 3
#define n_occupied 4
#define o_occupied 5
#define p 0.300000
//define the probability of CO adsorption
#define xco 0.4
//define the disociation rate of NO
#define rno 0.6

//function that selects the x-coordinate of a site at random
int printrandomx(int lower, int upper, int cuenta)
{
int m, num1;
for (m = 0; m < cuenta ; m++) {
num1 = (rand() % (upper - lower + 1)) + lower;
// printf("random x: %d \n", num1);
}
return num1;
}

//function that selects the y-coordinate of a site at random
int printrandomy(int bajo, int alto, int contar)
{
int n,num2;
for (n = 0; n < contar; n++) {
num2 = (rand() % (alto - bajo + 1)) + bajo;
//printf("random y: %d \n", num2);
}
return num2;
}

//Function that locates four nearest neighbors of the chosen site (here we are considering periodic boundary conditions)
int get_limited_coord(int coord, int coord_max)
{
if (coord >= 0 && coord < coord_max) {

return coord;

} else if (coord >= coord_max) {

return coord - coord_max;

} else {

return coord + coord_max;

}
}

int main(){
FILE *fp;
fp  = fopen ("data.txt", "w");
srand(time(0));

int N = xmax*ymax;
int grid[xmax][ymax];
int lower = 0, bajo=0, upper=xmax-1, alto=ymax-1, cuenta=1, contar=1;
int count = 1, x=0, y=0;
int right, left, up, down, upleft, upright, downleft, downright;
double random,q,r;
int co = 0, no = 0, n = 0, o = 0, v = N, pco2 = 0, pn2 = 0, po2 = 0;

// Initialize lattice values to 0
for (int i = 0; i < xmax; i++) {
for (int j = 0; j < ymax; j++) {
grid[i][j] = 0;
}
}

step1:
//LOCATE AN ENTRY OF THE MATRIX RANDOMLY
for (int i = 0; i < xmax; i++) {
for (int j = 0; j < ymax; j++) {
grid[i][j] = count++;
printf("%d ", grid[i][j]);
}
printf("\n");
}

x = printrandomx(lower, upper, cuenta);
y = printrandomy(bajo, alto, contar);
printf("(x,y)=(%d,%d)\n\n",x,y);

for (int i = 0; i < xmax; i++)
{
for (int j = 0; j < ymax; j++ )
{

if(x == i && y == j){ grid[x][y] == count; }
// else{ printf("FAILED\n");}
}

}
printf("The coordinates are (%d,%d) and the solicited matrix entry is: %d\n\n", x, y, grid[y][x]);

if (x > xmax || x < 0 || y > ymax || y < 0) {
printf ("Invalid coordinates given\n");
return 1;
}

//EVALUATE IF THE CHOSEN SITE IS EITHER EMPTY OR OCCUPIED
printf("IS IT OCCUPIED???\n\n\n");

check:			if (grid[y][x] == occupied){

printf("It's occupied, the trial ends. Choose another site again\n\n");
goto step1;

}
else{
if (grid[y][x] == empty){

printf("It's empty. Let's go to generate a random number to choose the adsorbates\n\n");
goto step2;
}

}

seekneighbors: printf("FIND THE NEIGHBORS\n\n");
//THESE PRINT THE FOUR NEAREST NEIGHBORS OF THE CHOSEN SITE
//Looking up the nearest 8 neighbors (If the chosen entry is not on any edge of the matrix)
right = grid[get_limited_coord(y , ymax)][get_limited_coord(x+1, xmax)];
left = grid[get_limited_coord(y , ymax)][get_limited_coord(x-1, xmax)];
up = grid[get_limited_coord(y-1, ymax)][get_limited_coord(x , xmax)];
down = grid[get_limited_coord(y+1, ymax)][get_limited_coord(x , xmax)];
//printf("The coordinates are (%d,%d) and the solicited matrix entry is: %d\n", x, y, grid[y][x]);
//Printing the eight nearest neighbors
printf("Right: %d\nLeft: %d\nUp: %d\nDown: %d\n\n",right,left,up,down);
goto step3;

//ESCOGEMOS CUAL ESPECIE SE VA A ADSORBER
//genero un numero aleatorio q tal que si es menor que xco, se adsorbe CO y si es mayor que xco, se adsorbe NO

step2: q = (double) rand () / RAND_MAX;
printf("random q= %lf\n\n",q);

if(q < xco || q == xco){
grid[y][x] = co_occupied;
co = co + 1;
v = v - 1;
printf("adsorbed CO ---> co = %d\n\n",co);

goto seekneighbors;

step3:
printf("****THIS PART WORKS****\n\n");

right = o_occupied;
left = o_occupied;
up = o_occupied;
down = o_occupied;
bool flag1 = 0;
bool flag2 = 0;
bool flag3 = 0;
bool flag4 = 0;
flag1 = (right == o_occupied);
flag2 = (left == o_occupied);
flag3 = (up == o_occupied);
flag4 = (down == o_occupied);

if(flag1 || flag2 || flag3 || flag4){
pco2 = pco2 + 1;
v = v + 2;
printf("produced co2 = %d\n\n",pco2);
} else{
printf("CO2 IS NOT PRODUCED\n\n");
}
} else {
//se escoge NO
printf("q = %lf is greater than xco = %lf. We choose NO\n\n", q, xco);
r = (double) rand () / RAND_MAX;
printf("r= %lf\n\n", r);

if(r < rno){
printf("r= %lf is less than rno= %lf\n\n We seek another neighbor to decide if NO can disociate\n\n", r,rno);
bool flag5 = 0;
bool flag6 = 0;
bool flag7 = 0;
bool flag8 = 0;

flag5 = (right == empty);
flag6 = (left == empty);
flag7 = (up == empty);
flag8 = (down == empty);

if( flag5 || flag6 || flag7 || flag8 ){
n = n + 1;
o = o + 1;
v = v - 2;

}else{
printf("****THE TRIAL ENDS, START AGAIN****");
goto step1;
}
}

}

fclose(fp);
return 0;
}```

It seems that my code doesn't even passes through the following chunk:

```//EVALUATE IF THE CHOSEN SITE IS EITHER EMPTY OR OCCUPIED
printf("IS IT OCCUPIED???\n\n\n");

//random = (double) rand () / RAND_MAX;
//printf("random = %lf\n\n",random);
// Include particle based on the probability
check:          if (grid[y][x] == occupied){

printf("It's occupied, the trial ends. Choose another site again\n\n");
goto step1;

}
else{
if (grid[y][x] == empty){

printf("It's empty. Let's go to generate a random number to choose the adsorbates\n\n");
goto step2;
}

}
```
Why could that happen?
I'll greatly appreciate if you could give me some hints to continue. Thanks for your time :)
Posted
Updated 29-Jan-23 8:24am
v7

## Solution 1

A single equals character is an assignment operator:
C
`grid[x][y] = 1;`

look at what you did in the if statement:
C
`if(grid[x][y] = 1)`

You're trying to do another assignment, not a comparison. Your if statement should be:
C
`if(grid[x][y] == 1)`

Auyik 29-Jan-23 13:39pm
Hi! thank you for your answer. I was thinking about using "==" but doesn't that action replace the value of the chosen site? In my code grid[x][y] is the chosen entry in the surface I'm working with.
Dave Kreskowiak 29-Jan-23 13:42pm
You didn't read my post, did you? Again, using a single = is an assignment. The expression value on the right is assigned to the expression on the left.

Two = characters, ==, is the equality operator. This compares the result of the expression on the right of the operator to the result of the expression on the left of it.
Auyik 29-Jan-23 14:28pm
OK, thanks. I made some modifications to my code, but it never passes through the following part of the code (where I'm trying to verify the occupation of a site):

```//EVALUATE IF THE CHOSEN SITE IS EITHER EMPTY OR OCCUPIED
printf("IS IT OCCUPIED???\n\n\n");

//random = (double) rand () / RAND_MAX;
//printf("random = %lf\n\n",random);
// Include particle based on the probability
check:          if (grid[y][x] == occupied){

printf("It's occupied, the trial ends. Choose another site again\n\n");
goto step1;

}
else{
if (grid[y][x] == empty){

printf("It's empty. Let's go to generate a random number to choose the adsorbates\n\n");
goto step2;
}

}```
Why it would happen?
Dave Kreskowiak 29-Jan-23 14:42pm
Why are you doing a second if statement if there only two possible states, occupied and empty?
Shouldn't that if statement look like this:
```    //EVALUATE IF THE CHOSEN SITE IS EITHER EMPTY OR OCCUPIED
printf("IS IT OCCUPIED???\n\n\n");

check:      if (grid[y][x] == occupied)
{
printf("It's occupied, the trial ends. Choose another site again\n\n");
goto step1;
}
else
{
// If it's not occupied, it must be empty, so why check for it?
printf("It's empty. Let's go to generate a random number to choose the adsorbates\n\n");
goto step2;
}```
Auyik 29-Jan-23 14:45pm
Oh, thank you very much. You're right. Let me check my code one more time.