15,998,673 members
See more:
Hi I'm working with a code that is intended to make the following choices:

1) I select at random the coordinates of a "site" in a square surface. I defined that surface as a 2d array, named "grid".

2) I defined two states "empty" and "occupied". If the selected site is empty, it will be filled by the molecule #1, which is represented by the integer variable "count1" (which will count the number of molecules the surface has trapped). Next, the state of the site will be occupied. Next, I continue to explore the sites so that I can continue filling the surface.

3) If the select site is occupied, the program asks to select another site until it finds another empty so that it also can be filled with the molecule.

What I have tried:

The following is the code I have tried so far:
```#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>

//define the dimensions of the latice
#define xmax 2
#define ymax 2

//define the "empty" and "occupied" states
#define empty 0
#define occupied 1

//Function generates random numbers to generate the coordinates of a site in the surface
int rand_min_max(int min, int max) {
return rand() % (max - min) + min;
}

//Function that generates the random coordinates of a site in the lattice
int generate_coords(int grid[xmax][ymax]) {
int x = rand_min_max(0, xmax);
int y = rand_min_max(0, ymax);

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

int main(){

srand(time(0));
int grid[ymax][xmax];
int x = 0, y = 0;
int right, left;
int count1 = 0, count2 = 0;

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

//LOCATE AN ENTRY OF THE MATRIX RANDOMLY
generate_coords(grid);

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

if (grid[y][x] == empty){
printf("IT'S EMPTY, LET'S FILL IT WITH A MOLECULE\n\n");
count1 = count1 + 1;
grid[y][x] = occupied;

}else{
printf("IT'S OCCUPIED. PLEASE, GENERATE ANOTHER SITE ON THE SURFACE\n\n");
generate_coords(grid);
}

return 0;
}```

I thought that placing some gotos could help me to the program come back and generates other coordinates but It's not good idea to use them since the program could become a mess if I add more conditions.

But, what is the better way to make the code calculate again other coordinates and repeat the process when it fill a site or when it find such a site occupied?
Posted
Updated 31-Jan-23 12:33pm
v3

## Solution 1

You just need a loop in the main function, something like:
C++
```int filledcells = 0; // keep a count of the number of occupied cells
while (filledcells != xmax * ymax)
{
// get the coordinates of the next cell
if (cell[x, y] == 0) // is it free?
{
cell[x, y] = molecule; // yes, set the molecule number in the cell
filledcells++; // count the filled cells
// do any other processing for this cell
}
}```

At some point you need to increase the molecule number, but you have not shown your rule for doing that.

yuike 2-Feb-23 20:20pm
Thank you!

## Solution 2

I would attempt a simple approach. Try
C
```#include <stdio.h>
#include <stdlib.h>
#include <time.h>

//define the dimensions of the lattice
#define XMAX 2
#define YMAX 2

//define the "EMPTY" state
#define EMPTY 0

int rndx()
{
return (rand() % XMAX);
}

int rndy()
{
return (rand() % YMAX);
}

int main(){

srand(time(0));
int grid[YMAX][XMAX];

// Initialize lattice to be EMPTY
for (int x = 0; x < XMAX; x++){
for (int y = 0; y < YMAX; y++){
grid[x][y] = EMPTY;
}
}

int molecule = 0;

while ( molecule < XMAX * YMAX ) // repeat till the grid is fully occupied...
{
//LOCATE AN ENTRY OF THE MATRIX RANDOMLY
int x = rndx();
int y = rndy();

//EVALUATE IF THE CHOSEN SITE IS EITHER EMPTY OR OCCUPIED
if ( grid[x][y] == EMPTY )
{
printf("[%d, %d] IT'S EMPTY, LET'S FILL IT WITH A MOLECULE\n\n", x, y);
grid[x][y] = ++molecule; // occupy the cell by assigning it the number of the molecule
}
else
{
printf("[%d, %d] IT'S OCCUPIED\n\n", x, y); // OOPS, it's already occupied, nothing to do
}
}

// print out final grid state
for (int y = 0; y < YMAX; y++){
for (int x = 0; x < XMAX; x++){
printf("%4d", grid[x][y]);
}
printf("\n");
}

return 0;
}```

merano99 31-Jan-23 18:33pm
+5
yuike 2-Feb-23 20:22pm
Thank you so much. If I have more questions, I'll come back :)

## Solution 3

A few remarks on my changes or extensions:
1. constants are usually capitalized
2. i have defined an own datatype for the grid to avoid wrong assignments
3. subroutine grid_init() to initialize, because this can probably occur several times
4. subroutine generate_coords() should return values for x and y
5. unused variables deleted
6. data type conflict srand() corrected
7. calculation availcells is done only once, then comparison with 0 possible
8. switch for the case that cells contain further states e.g. different molecules
9. counter how often was tried to occupy all cells
10.Proposal taken like Palini for random numbers, since MIN is probably always 0
C++
```//define the dimensions of the grid
#define MAX_X 2
#define MAX_Y 2

//define the states of a cell in grid
typedef enum  { S_EMPTY, S_OCCUPIED, S_NONE } gstate;

// help generate random coordinate of the grid
int gridrnd(int max)
{
return (rand() % max);
}

// generates random coordinates of the grid
int generate_coords(int* x, int* y )
{
if (!x || !y)
return 1;

*x = gridrnd(MAX_X);
*y = gridrnd(MAX_Y);

printf("(%d,%d)\n\n", *x, *y);
return 0;
}

void grid_init(gstate grid[MAX_Y][MAX_X])
{
for (int y = 0; y < MAX_Y; y++) {
for (int x = 0; x < MAX_X; x++) {
grid[y][x] = S_EMPTY;
}
}
}

int main()
{
int x = 0, y = 0;
gstate grid[MAX_Y][MAX_X];

srand((unsigned)time(0));

// Initialize grid to be S_EMPTY
grid_init(grid);

int availcells = MAX_X * MAX_Y;
int rounds = 0;

while (availcells) // repeat till the grid is full
{
//LOCATE AN ENTRY OF THE MATRIX RANDOMLY
generate_coords(&x, &y);
rounds++;

//EVALUATE THE CHOOSEN SITE
switch (grid[y][x])
{
case S_EMPTY:
printf("IT'S S_EMPTY, LET'S FILL IT WITH A MOLECULE\n\n");
grid[y][x] = S_OCCUPIED;
availcells--;
break;
case S_OCCUPIED:
printf("IT'S S_OCCUPIED. PLEASE, GENERATE ANOTHER SITE ON THE SURFACE\n\n");
break;
}
}

printf("Needed %d rounds to fill complete grid\n", rounds);
return 0;
}```

v2