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
#define MAX_X 2
#define MAX_Y 2
typedef enum { S_EMPTY, S_OCCUPIED, S_NONE } gstate;
int gridrnd(int max)
{
return (rand() % max);
}
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));
grid_init(grid);
int availcells = MAX_X * MAX_Y;
int rounds = 0;
while (availcells) {
generate_coords(&x, &y);
rounds++;
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;
}