二维数组中相等相邻元素的计数 [英] Count of Equal Adjacent Elements in 2D Array

查看:661
本文介绍了二维数组中相等相邻元素的计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从事一项编程任务,并且停留在一部分上。指示要求在2d数组中为相等的相邻元素创建一个计数。

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.

我尝试设置2个循环,然后在if语句中使用或的多个条件测试元素是否相等。问题是if语句只能用于数组内的元素。我尝试的函数如下所示

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!

推荐答案

您提供的代码很少有问题,我将一一介绍给您,

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

要将多维数组传递给函数,我将向您展示2种方法做到这一点

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

1)将多维数组作为一维数组传递
之所以可行,是因为我们知道数组如何在内存中表示,而我们对表示的了解正是使C / C ++中的指针如此强大的工具。
阅读此。

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.

元素(此处为ints)的填充方式如图片中的之字形线所描述。
因此,在我们的示例数组 int arr [5] [5] 中,
第一行的第二个元素( arr [ 0] [1] )可以通过 *(arr + 0 * 5 + 1)进行访问,就像 arr 给出数组的基地址,类似地,<$ c可以访问第5行的第4个元素( arr [4] [3] ) $ c> *(arr + 4 * 5 + 3),这里我们将行索引乘以5,因为每行有5个元素(即列数),有了这些知识,我们可以

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 

在调用函数时,强制转换 arr (int *)是必要的,因为最初的arr类型是 int(*)[3]

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); }

2)将多维数组作为指针传递给函数参数中的数组。

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) p>

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);
}

在此,无需强制转换 arr

在您的代码中,您并没有在意数组索引超出范围,如果您的数组是 int arr [5] [5] 并且您尝试访问 arr [-1] [ 5] arr [5] [3] ,结果将是不确定的,这就是代码崩溃到系统都会发生的一切

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;

}

这篇关于二维数组中相等相邻元素的计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆