jeudi 17 janvier 2019

Equality test function

Below is a function which aims to perform an equality test between adjacent numbers in a one dimensional vector.

This 1D vector will have values which will represent an nxn grid.

When they are equal it returns false.

For example consider this 3x3 grid:

https://imgur.com/WgHR2tq [ I am trying to use the method described to upload pictures but I can't seem to get it to work, if anyone can help me understand this I would be grateful. It says press CTLR+G but this isn't doing anything? ]

The issue with the code I wrote is that not all of the numbers in the grid will have 4 other adjacent numbers and testing for indexes which don't exist e.g when trying to compare the number above the top right number in the grid (1 in the example) might lead to some inaccurate outcomes.

In addition to this what I wrote seems not to be the most efficient way to go about it. Surely there could be a simpler way to do this than having to list 5 new variables?

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

         int x = v[convert(i, j, n)];
         int c = v[convert(i-1, j, n)];
         int v = v[convert(i+1, j, n)];
         int b = v[convert(i, j+1, n)];
         int n = v[convert(i, j-1, n)];


         if(x==c || x==v || x==b || x==n ) {
             return false;

         }
      }
  }

//another function used to convert 2d into 1D index

int convert(int row, int col, int rowlen){

return row*rowlen+col;

}

I would appreciate any help.

Aucun commentaire:

Enregistrer un commentaire