0

I am working on a programming assignment and stuck on one part. The directions ask to create a count for equal adjacent elements within a 2d array.

I have tried setting up 2 for loops followed by if statements with multiple conditions using "or" to test if the elements are equal. The issue is the if statement can only be used for elements bounded within the array. The function i tried is shown below

int count(int** t, int r, int c) {

int i, j, count = 0;

for (i = 0; i < r; i++) {

for (j = 0; j < c; j++)

if (t[i][j] == t[i - 1][j - 1] || t[i][j] == t[i - 1][j] || t[i][j] == 
t[i - 1][j + 1] || t[i][j] == t[i][j - 1] || t[i][j] == t[i][j + 1] || 
t[i][j] == t[i + 1][j - 1] || t[i][j] == t[i + 1][j] || t[i][j] == t[i + 
1][j + 1])

count++; } 
return count;

}

I am new to programming, please help!

  • First thing is your using `i < r` & `i < c` instead of `i < rows` & `i < columns` – vmetelz Jul 13 '19 at 21:51
  • yeah I labeled them like that to make it easier for you guys to read, i have them as r and c in the original code – Emad Al Banna Jul 13 '19 at 21:53
  • @EmadAlBanna Please always post your **real** code. Otherwise you're just going to get answers that fix the problems in your imaginary code. – john Jul 13 '19 at 22:01
  • 1
    The problem with your if statement is that you don't take into account the bounds of your 2d array. Think about `t[i][j] == t[i - 1][j]`, what's that going to do when `i == 0`? – john Jul 13 '19 at 22:03
  • I think, the answer is 42. I guess you will not understand that, so, I will elaborate a little more. Please state your problem. Please ask a concrete question. Please describe what adjecent means, up, down, left, right, whatever. Please check your boundaries. Please use meaningful variable names. Please format your code – Armin Montigny Jul 13 '19 at 22:06
  • You should iterate like this: `for (i = 1; i < r-1; i++)` and `for (j = 1; j < r-1; j++)` to keep within the array bounds. You should need to write separate loops to check the sides. – doug Jul 13 '19 at 22:41

1 Answers1

0

Your provided code have few issues i will explain them to you one by one ,

Passing arrays to functions

To pass multidimensional arrays to functions , I will show you 2 ways to do that

1) Passing multidimensional arrays as single dimensional arrays This works because we know how array is represented in the memory , and our this knowledge of representation is what makes pointers in C/C++ such a powerful tool . Read this answer to get a better picture of representation of array in memory.

Arrays are represented linearly and contiguously in memory , thus if define array as arr[5][5] , we are telling compilers that we need a memory block having sufficient space for storing 5*5 = 25 int data types . And it's also worth to know that arrays are represented in row major form , read this to learn more about row major form. Order of filling elements in row and column major form .

Elements(here ints) are filled in the way as described by the zig-zag line in the picture . Thus in our example array int arr[5][5], 2nd element of the 1st row(arr[0][1]) can be accessed by *(arr+0*5+1) , as arr gives the base address of the array , similarly , 4th element of 5th row (arr[4][3]) can be accessed by *(arr+4*5+3) , here we are multiplying row index by 5 because each row have 5 elements(that is number of columns) , with this knowledge in mind we can write code to access array elements of a matrix in the following way

void display(int *arr,int r,int c)
{
    for(unsigned i=0;i<r;++i)
    {
        for(unsigned j=0;j<c;++j)
        {
            cout<<*(arr+i*c+j)<<ends;
        }
        cout<<endl;
    }
}
const unsigned ROW=3,COL=3;
int main()
{
    int arr[ROW][COL]={1,2,3,
                       4,5,6,
                       7,8,9
                    };
    display((int *)arr,ROW,COL);
}

While calling the function , casting arr to (int *) is necessary because originally arr type is int (*)[3] , that is pointer to an int array of 3 elements .

2) Passing multidimensional array as pointer to an array in the function argument .

const unsigned ROW=3,COL=3;
void display(int (*arr)[COL],int r,int c)
{
    for(unsigned i=0;i<r;++i)
    {
        for(unsigned j=0;j<c;++j)
        {
            cout<<arr[i][j]<<ends;
        }
        cout<<endl;
    }
}
int main()
{
    int arr[ROW][COL]={1,2,3,
                       4,5,6,
                       7,8,9
                    };
    display(arr,ROW,COL);
}

In this , there is no need to cast the arr

Index out of bounds

In your code you are not taking care of array index going out of bounds , if your array is int arr[5][5] and you try to access arr[-1][5] or arr[5][3] , the result will be undefined , that is anything can happen from your code just crashing to your system going up in flames(just a metaphor) .

Keeping these things in mind , a working code satisfying your needs is

int count(int *t, int r, int c)
{

    int i, j, result = 0;

    for (i = 0; i < r; i++)
    {

        for (j = 0; j < c; j++)
        {
            if(i!=r-1)
            {
                if(*(t+i*c+j)==*(t+(i+1)*c+j))
                    ++result;
                if(j!=r-1)
                {
                    if(*(t+i*c+j)==*(t+(i+1)*c+j+1))
                        ++result;
                }
                if(j!=0)
                {
                    if(*(t+i*c+j)==*(t+(i+1)*c+j-1))
                        ++result;
                }
            }
            if(j!=c-1)
            {
                if(*(t+i*c+j)==*(t+i*c+j+1))
                    ++result;
            }
        }
    }
    return result;
}
const unsigned ROW=3,COL=3;
int main()
{

    int arr[ROW][COL]={1,1,2,
                       1,2,3,
                       4,5,2
                    };
    cout<<count((int *)(arr),ROW,COL)<<endl;

}

parth_07
  • 892
  • 10
  • 18