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

时间:2016-04-03 06:16:37

标签: c++ c++11 multidimensional-array

我的代码到目前为止工作但我在第17到第30行代码中有一个逻辑错误,我似乎无法弄明白。我觉得我太过分了。

问题的提示如下:

如果两个二维数组m1m2具有相同的内容,则它们是相同的。编写一个函数,如果m1m2相同,则返回true,使用以下标题:

const int SIZE = 3;    

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

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

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

谢谢

1 个答案:

答案 0 :(得分: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。事实并非如此。

你可以找到这些错误的一种方法是在一张纸上制作测试用例。

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

相关问题