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

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

问题描述

我正在完成一项编程任务,但卡在了一个部分上.方向要求为二维数组中的相等相邻元素创建计数.

我尝试设置 2 个 for 循环,然后是具有多个条件的 if 语句,使用或"来测试元素是否相等.问题是 if 语句只能用于数组内的元素.我尝试的功能如下所示

int count(int** t, int r, int c) {int i, j, 计数 = 0;for (i = 0; i < r; i++) {for (j = 0; j 

}

我是编程新手,请帮忙!

解决方案

你提供的代码有几个问题我会一一给你解释,

将数组传递给函数

要将多维数组传递给函数,我将向您展示两种方法

1) 将多维数组作为一维数组传递这是有效的,因为我们知道数组在内存中是如何表示的,而我们的这种表示知识使 C/C++ 中的指针成为如此强大的工具.阅读此 .

Elements(here ints)按图中之字形线描述的方式填充.因此,在我们的示例数组 int arr[5][5] 中,第一行的第二个元素(arr[0][1]) 可以通过 *(arr+0*5+1) 访问,如 arr 给出数组的基地址,类似地,第 5 行的第 4 个元素 (arr[4][3]) 可以通过 *(arr+4*5+3) ,这里我们将行索引乘以 5,因为每行有 5 个元素(即列数),考虑到这些知识,我们可以编写代码以通过以下方式访问矩阵的数组元素

void display(int *arr,int r,int c){for(unsigned i=0;i

在调用函数时,需要将 arr 强制转换为 (int *) 因为原来的 arr 类型是 int (*)[3],即指向 3 个元素的 int 数组的指针.

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

const unsigned ROW=3,COL=3;void display(int (*arr)[COL],int r,int c){for(unsigned i=0;i

在此,不需要强制转换 arr

索引越界

在您的代码中,如果您的数组是 int arr[5][5] 并且您尝试访问 arr[-1][5]arr[5][3] ,结果将是 undefined ,从你的代码崩溃到你的系统着火(只是一个比喻).

牢记这些事情,满足您需求的工作代码是

int count(int *t, int r, int c){int i, j, 结果 = 0;for (i = 0; i 

<小时>

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!

解决方案

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;

}


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

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