如何比较两个数组以查看它们是否具有相同的内容

How do I compare 2 arrays to see if they have the same contents?

本文关键字:是否 比较 何比较 两个 数组      更新时间:2023-10-16

到目前为止,我的代码可以工作,但在第17到第30行代码中出现了逻辑错误,我似乎无法解决。我觉得我有太多事情要做。

问题提示如下:

如果两个二维阵列CCD_ 1和CCD_。如果m1m2相同,则使用以下标头编写一个返回true的函数:

const int SIZE = 3;    
bool equals(const int m1[][SIZE], const int m2[][SIZE]);

编写一个测试程序,提示用户输入两个3x3的整数数组,并显示两者是否相同。以下是示例运行(如下)。


Enter m1: 51 25 22 6 1 4 24 54 6
Enter m2: 52 22 25 6 1 4 24 54 6
output : Two arrays are identical
Enter m1: 51 5 22 6 1 4 24 54 6
Enter m2: 51 22 25 6 1 4 24 54 6
output: Two arrays are not identical

这是我的代码(很抱歉,如果它很糟糕,我还在学习)

#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
using namespace std;
const int SIZE = 3;
bool equals(const int m1[][SIZE], const int m2[][SIZE]) {
    bool choice = true;
    while (choice) {
        for (int i = 0; i < SIZE; i++) {
            for (int j = 0; j < SIZE; j++) {
                if (m1[i][j] != m2[i][j]) {                                                        // code is fine until here. There is a logic error somewhere. m1: 123456789 m2: 987654321 output: array not identical
                    for (int k = 0; k < SIZE; k++) {
                        for (int h = 0; h < SIZE; h++) {
                            if (m1[i][j] == m2[k][h]) {
                                return true;
                            }
                            else {
                                choice = false;
                                return false;
                            }
                        }
                    }
                }
                else {
                    return true;
                }
            }
        }
    }
}
int main() {
    string input1, input2, num1, num2;
    int count1 = 0, count2 = 0;
    int a = 0, b = 0, c = 0;
    int a2 = 0, b2 = 0, c2 = 0;
    int arr1[SIZE][SIZE];
    int arr2[SIZE][SIZE];
    cout << "Enter m1: ";
    getline(cin, input1);
    stringstream line1(input1);
    cout << "Enter m2: ";
    getline(cin, input2);
    stringstream line2(input2);
    while (line1 >> num1) {
        if (count1 < 3) {
            stringstream(num1) >> arr1[0][a];
            a++;
        }
        else if (count1 >= 3 && count1 <= 5) {
            stringstream(num1) >> arr1[1][b];
            b++;
        }
        else {
            stringstream(num1) >> arr1[2][c];
            c++;
        }
        count1++;
    }
    while (line2 >> num2) {
        if (count2 < 3) {
            stringstream(num2) >> arr2[0][a2];
            a2++;
        }
        else if (count2 >= 3 && count2 <= 5) {
            stringstream(num2) >> arr2[1][b2];
            b2++;
        }
        else {
            stringstream(num2) >> arr2[2][c2];
            c2++;
        }
        count2++;
    }
    bool answer = equals(arr1, arr2);
   if (answer) {
        cout << "Two arrays are identical" << endl;
   }
   else {
        cout << "Two arrays are not identical" << endl;
   }
   system("Pause");
   return 0;
}

谢谢

这个函数中确实有很多内容。你有很多嵌套的循环。

要使代码更容易理解,只需使用两个for循环(嵌套)来检查是否存在错误条件。如果遇到,返回false。如果没有遇到false条件,您将在某个时刻离开循环,然后确保遇到return true语句。

for(int i = 0; i < SIZE; ++i)
    for(int j = 0; j <SIZE; ++j)
        if (m1[i][j] != m2[i][j])
            return false;
return true;

这就是我编写函数的方式。以下是您的功能中的一些冗余:

  • while循环。这有什么用?您已经有一个for循环,它将一直运行到满足结束条件。

  • 第一个if条件内的另一组for循环。

即使不满足所需的条件,函数也会返回true。即使a1[1][2]等于a2[3][3],第21行也将返回true。事实并非如此。

找出这些错误的一种方法是在一张纸上写出测试用例。

我希望我能帮上忙!(我希望我是对的):)