Click here to Skip to main content
15,846,211 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm simulating a square surface that "traps" molecules in C and, I don't know if it's correctly initialized. I want in the beginning of the whole simulation that the grid is totally empty (without any molecule on it) so that it can start to trap the mentioned molecules.

But, when I generate randomly the coordinates of an entry and start checking if the lattice is empty ( to attempt adding molecules to it later on), it seems that every entry the program choose is already "occupied" and I can't see why since I haven't added anything so far.

The following is my code:

What I have tried:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

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

//Define parameters to obtain both the x and y coordinates of a site in the lattice at random
#define LOWER 0 
#define UPPER xmax-1 
#define ALTO ymax-1 
#define CONTAR 1

//we define the Monte Carlo steps
#define MCSS 100

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


//Function that selects the x-coordinate of the chosen site randomly 
int printrandomx(int lower, int upper, int cuenta)
	{
		int m, num1;
		for (m = 0; m < cuenta ; m++) {
		num1 = (rand() % (upper - lower + 1)) + lower;
		}
	return num1;
	}	

//Function that selects the y-coordinate of the chosen site randomly 
	int printrandomy(int bajo, int alto, int contar)
	{
		int n,num2;
		for (n = 0; n < contar; n++) {
		num2 = (rand() % (alto - bajo + 1)) + bajo;
		}
	return num2;
	}

//Function that generates the random coordinates of a site in the lattice
	int generate_coords(int red[xmax][ymax]){
		int x_coord, y_coord;
		int count = 1;
		for (int i = 0; i < xmax; i++) {
					for (int j = 0; j < ymax; j++) {
						red[i][j] = count++;
						printf("%d ", red[i][j]);
					}
					printf("\n");
				}
	
			x_coord = printrandomx(LOWER, UPPER, CONTAR);
			y_coord = printrandomy(LOWER, ALTO, CONTAR);
			printf("\nSElECTED SITE ---> (x,y)=(%d,%d)\n\n", y_coord, x_coord);

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


//Here starts the main function

int main(){
	srand(time(0));
	int x = 0, y = 0;

	//define the grid or lattice we will working with
	int grid[xmax][ymax];
		
	// Initialize lattice to be empty
	for (int i = 0; i < xmax; i++) {
		for (int j = 0; j < ymax; j++) {
			grid[j][i] = empty;
		}
	}
	 
		
	//LOCATE AN ENTRY OF THE MATRIX RANDOMLY
		generate_coords(grid);
			
	//EVALUATE IF THE CHOSEN SITE IS EITHER EMPTY OR OCCUPIED 
	printf("IS IT OCCUPIED???\n\n");			
	if (grid[y][x] == empty){
		printf("It's empty. Let's fill it with a molecule\n\n");
		printf("Here I will specify other conditions :)\n\n");
	}else{ 
			printf("It's occupied, the trial ends. Choose another site again\n\n");
			generate_coords(grid);
	}
	return 0;
}


The chunk of the code that must set the initial array as empty is the following:
// Initialize lattice to be empty
    for (int i = 0; i < xmax; i++) {
        for (int j = 0; j < ymax; j++) {
            grid[j][i] = empty;
        }
    }


However, it doesn't work as expected and I don't know why.
Posted
Updated 30-Jan-23 15:53pm

Quote:
I do not know if it is initialized correctly.

Although at the beginning of the simulation the grid is completely initialized with empty, afterwards the address of the grid is passed to a function:
generate_coords(grid);

In the function you do the following with the grid:
C
int count = 1;
for (int i = 0; i < xmax; i++) {
  for (int j = 0; j < ymax; j++) {
    red[i][j] = count++;
    printf("%d ", red[i][j]);
  }
}

I am not sure if you are really surprised that the grid is not empty anymore.

One more thing: Since the function has a return value, this should always be returned. After the call the caller should also do something with this value, otherwise it would be in vain. The compiler should also notice this if you specify the flags correctly when calling the function.
Warning: "generate_coords": Not all control paths return a value.
 
Share this answer
 
Comments
Auyik 31-Jan-23 2:43am    
Oh, I totally forgot about the part of the code that makes my matrix always "occupied". It makes sense. But, how can I define the grid without this problem happens?
merano99 31-Jan-23 5:58am    
The grid is defined by the decalation and also initialized by the assignment with empty. The code that assigns all elements with consecutive numbers should probably just be deleted. From my point of view there is no additional problem here.
If you want to keep the grid variable and also use very large grids it would be recommended to get memory from the heap with calloc(). However, this would be much more complex and requires some experience.
Auyik 31-Jan-23 6:38am    
Thank you for your help! I just deleted this part of my code. Yes! I'm trying to understand "calloc()" and "malloc()", but it's hard for me right now.

I'll go to work with a 40x40 lattice indeed. Do you think I need to get memory with this lattice size?
merano99 31-Jan-23 7:49am    
First of all, you can do the math: 40 x 40 x 4 bytes = 6.25 kByte
As long as the know-how for dynamic memory management is not enough you can (if necessary) leave it like this. As soon as the grids get bigger, you need more than one or recursion is desired it will not work well anymore.
Auyik 31-Jan-23 9:05am    
Thanks!
Your indexes are mixed up. Here's the code :
C++
int grid[xmax][ymax];

// Initialize lattice to be empty
for (int i = 0; i < xmax; i++) {
    for (int j = 0; j < ymax; j++) {
        grid[j][i] = empty;
    }
}
You get away with it because xmax and ymax are the same but it is incorrect. What if xmax were 8 and ymax were 4? Typically, y is used for the first dimension and x for the second. That would make the code look like this :
C++
int grid[ ymax ][ xmax ];

 // Initialize lattice to be empty
 for (int i = 0; i < ymax; i++) {
     for (int j = 0; j < xmax; j++) {
         grid[i][j] = empty;
     }
 }
 
Share this answer
 
Comments
Auyik 31-Jan-23 2:44am    
Yes, you are right. I will fix it. Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900