Click here to Skip to main content
15,879,490 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi! I've worked on a code that could give me the six nearest neighbors of a chosen site of a triangular lattice considering periodic boundary conditions.

First, the code asks the user to enter the x and y coordinates of a point inside a maximum "size" then it locates it and prints the neighbors mentioned

What I have tried:

This is my code:
#include <stdio.h>
#include <stdlib.h>
#define xmax 4
#define ymax 4 

//I define functions considering if the chosen site has even or odd x-coordinates

//There wasn't considered the oddness of the x-coordinates for the right and left neighbors since they are the same regardless where the chosen site is 

//This function gives me the y-coordinate of the upright neighbor (that's why I called up-righty) if I take into account that the x coordinate of the chosen site is even or odd. This is analogous for the y coordinates of the upleft, downright and downleft neighbors (which are up_lefty, down_righty and down_lefty, respectively)

int up_righty(int xcoord, int ycoord){
    if( (xcoord%2) != 0){
        return ycoord+1;
    } 
    else{ if( (xcoord%2) == 0 ){
        return ycoord;
         } 
      }
}

int up_lefty(int xcoord, int ycoord){
     if( (xcoord%2) != 0){
        return ycoord;
    } 
    else{ if( (xcoord%2) == 0 ){
        return ycoord-1;
         } 
      }
}

int down_righty(int xcoord, int ycoord){
     if( (xcoord%2) != 0){
        return ycoord+1;
    } 
    else{ if( (xcoord%2) == 0 ){
        return ycoord;
         } 
      }
}

int down_lefty(int xcoord, int ycoord){
     if( (xcoord%2) != 0){
        return ycoord;
    } 
    else{ if( (xcoord%2) == 0 ){
        return ycoord-1;
         } 
      }
}

//This function consider the 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() {
      int count = 1;
    int number;
    int x=0,y=0;
    int uprighty, uplefty, downrighty, downlefty;
    int right, left, upleft, upright, downleft, downright;
    int grid[xmax][ymax];

    
    for (y = 0; y < xmax ; y++) {
        for (x = 0; x < ymax; x++) {
            grid[y][x] = count++;
            //printf("%d   ", grid[y][x]);
        }
        //printf("\n");
    }

    //Here I ask the user to enter the coordinates of the entry
    printf("\nEnter a X-coordinate :\n");
    scanf("%d", &y);
    printf("\nEnter a Y-coordinate :\n");
    scanf("%d", &x);
    printf("(x,y)=(%d,%d)\n",y,x); //print the chosen coordinates

//Give some additional restrictions about the allowed values for x and y I can enter 
    if (x > xmax || x < 0 || y > ymax || y < 0) {
        printf ("Invalid coordinates given\n");
        return 1;
    }

// Here I assign to integers the results of evaluate the above functions for the y coordinates of the neighbours
//I do it since later I need to apply the "get_limited_coord" function to the following results 
    uprighty = up_righty(x,y);
    uplefty = up_lefty(x,y);
    downrighty = down_righty(x,y);
    downlefty = down_lefty(x,y);
 
    //Now the code finds the nearest 6 neighbors of the chosen site
    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)];
  
    upleft    = grid[get_limited_coord(y-1, ymax)][get_limited_coord(uplefty, xmax)];
    
    upright   = grid[get_limited_coord(y-1, ymax)][get_limited_coord(uprighty, xmax)];
    
    downleft  = grid[get_limited_coord(y+1, ymax)][get_limited_coord(downlefty, xmax)];
    
    downright = grid[get_limited_coord(y+1, ymax)][get_limited_coord(downrighty, xmax)];

     printf("The coordinates are (%d,%d) and the solicited matrix entry is: %d\n", y, x, grid[y][x]);
     //Printing the six nearest neighbors
     printf("Right: %d\nLeft: %d\nUpleft: %d\nUpright: %d\nDownleft: %d\nDownright: %d\n",right,left,upleft,upright,downleft,downright);


    return 0;
}


In this case, I have chosen that the max size for the x and y coordinates is 4. The problem is that it seems that this code only gives me the correct neighbors for the following ordered pairs (0,0), (0,1), (1,0), (1,1), (2,2), (2,3), (3,2) and (3,3). On the other hand, it doesn't work for the pairs (0,2), (0,3), (1,2), (1,3), (2,0), (2,1), (3,0), (3,1) (half of the lattice! o.O)

I have checked the functions over and over again but I still don't understand why it works for some entries and not for others.

Any clarification would be immensely appreciated. Thanks in advance!
Posted
Updated 22-Jan-23 18:47pm
v2

1 solution

Since we have no idea what makes something a "nearest neighbour" or not and we have no access to your data for checking there isn't much we can do to help.

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
Comments
Auyik 23-Jan-23 9:30am    
Thank you very much for your response. I fixed my code 20 minutes after I posted this question and now is working as I expected! But I'm happy I posted the question anyways :)

Also, thank you for your advice. I will check it out right now. Yes, I think I need to improve my debugging skills and this definitely will help me in my current project :D
OriginalGriff 23-Jan-23 10:09am    
You're welcome!

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