returnNULL
returnNULL

Reputation: 25

Equal elements in adjacent position 2D-Array in C

I'm coding a game and need a function that checks whether a play exists or not in the integer-defined 2D array (each element is an integer). A play exists when 2 or more equal elements are adjacents in position, row-wise or column wise. I've got the row-wise working fine but somehow when checking column-wise, it doesn't work.

Here's what I've got this far:

//Checks if in an array there are two or more adjacent elements
int adjArray(int arr[], int size)

{
    int i = 0, flag = 0;
    for( i = 0; i < size-1; i++)
    {
      if(arr[i] == arr[i+1])
      {
        flag = 1;
        break;
      }
    }

   return flag;
}

int func(int _board[][MAX_BOARD_POS], int numrows, int numcols)
{

   int i = 0 , j = 0, flag = 0,arr[numrows];


   for (i = 0; i < numrows; i++)
   {
      if (adjArray(_board[i],numcols) == 1)
      {
        flag = 1;
        break;
      }
   }

//If there's a play row-wise, no need to check column-wise
if (!flag)
{
    //Get array of column elements to use compareArray function
    for ( i = 0; i < numcols; i++)
    {
        for (j = 0; j < numrows; j++)
        {
            arr[i] = _board[j][i];   
        }

        if (adjArray(arr,numrows) == 1)
        {
            flag = 1;
            break;
        }
    }    
}



return flag;   
}

I don't know if I was clear enough. What's odd is that the columns are being printed accurately but then somehow adjarray(arr,numrows) fails...

Upvotes: 1

Views: 326

Answers (3)

Stephen Docy
Stephen Docy

Reputation: 4788

Your code is so close...there is just one tiny error :

for (i = 0; i < numcols; i++)
    {
        for (j = 0; j < numrows; j++)
        {
            arr[i] = _board[j][i];
        }

If you look closely, it will become obvious that in the j loop, you simply keep writing over the same element in arr, since the value for i does not change in the j loop, so this should be

for (i = 0; i < numcols; i++)
    {
        for (j = 0; j < numrows; j++)
        {
            arr[j] = _board[j][i];
        }

That being said, some of the other approaches given here are worth examining too, I just thought you would like to know why your code was not quite working.

Upvotes: 3

Stargateur
Stargateur

Reputation: 26697

I didn't test it so maybe there are some bugs.

#include <stdbool.h>
#include <stddef.h>
#include <assert.h>

// size_t must be used for size, board[][X] should be avoid write (*board)[X] instead
// use bool if you want return true or false
bool func(int (*board)[MAX_BOARD_POS], size_t numrows, size_t numcols)
{
    // could be just if depend or what you want
    assert(numrows != 0 && numcols != 0);
    // we loop on the first row manualy because board[0 - 1] will not make sense
    for (size_t j = 1; j < numcols; j++) { // declare for loop increment IN the for loop
        if (board[0][j] == board[0][j - 1])
        {
            return true;
        }
    }
    // we loop on the remains rows
    for (size_t i = 1; i < numrows; i++)
    {
        // we do the first manualy because board[i][0 - 1] will not make sense
        if (board[i][0] == board[i - 1][0])
        {
            return true;
        }
        // we can finnaly check the two case in the same loop
        for (size_t j = 1; j < numcols; j++) {
            if (board[i][j] == board[i][j - 1] || board[i][j] == board[i - 1][j])
            {
                return true;
            }
        }
    }
    return false;
}

Upvotes: 2

Pedro Lima
Pedro Lima

Reputation: 407

For the columns if you just need to check if 2 continous elements are equal, you onle need to

for(j = 0 ; j < numcols && !flag ; j++){
   for(i = 0 ; i < numrows -1 && !flag ; i++){
       if( _board[i][j] == _board[i+1][j])
         flag= 1;
   }
}

By the way i recomend you to use the flag as a condition in the for, so when you change it to 1 ( you found the element you want) it will pop out from the cycle without need to use a break. I found this way more elegant.

Hope it helps :)

Upvotes: 2

Related Questions