Count of Equal Adjacent Elements in 2D Array

回眸只為那壹抹淺笑 提交于 2019-12-11 16:05:56

问题


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!


回答1:


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. .

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;

}



来源:https://stackoverflow.com/questions/57023066/count-of-equal-adjacent-elements-in-2d-array

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!