提示用户输入两个 3x3 int 数组并检查等效性

Prompt user to enter two 3x3 int arrays and check equivalence

本文关键字:数组 int 检查 3x3 两个 输入 用户 提示      更新时间:2023-10-16

这是我到目前为止想出的...我非常接近,实际上还剩下一个错误供它编译。

    #include <iostream>
#include <string>
#include <sstream>
using namespace std;
const int SIZE = 3;
bool equals(const int m1[][SIZE], const int m2[][SIZE])
{
    bool identical = true;
    for (int i=0; i < SIZE && identical; i++ )
    {
        if (m1[i] != m2[i]){
            identical = false;
        }
    }
    return identical = true;
}
void printArray(const int m1[], int size)
{
    for (int i = 0; i < size; i++)
        cout << m1[i] << " ";
}
void printArray2(const int m2[], int size)  //not sure if I need this 2nd 
{                                           //void
    for (int i = 0; i < size; i++)
        cout << m2[i] << " ";
}

int main()
{
    string input1, input2;
    int const SIZE = 3;
    double inputnumber;
    int m2[SIZE];
    int m1[SIZE];
    cout << "Please enter the first array: " << endl;
    getline(cin, input1);
    stringstream ss (input1);
    ss >> inputnumber;
    cout << "Please enter the second array: " << endl;
    getline(cin, input2);
    stringstream si (input2);
    si>>inputnumber;
    for (int i=0; i< inputnumber ; ++i) {
        ss >> m1[i];}
    if (equals(m1, SIZE)){
        cout << "The two arrays are identical ! ";
    }
    else{
    cout << "The two arrays are NOT identical !";
    }
    cout << endl;
    return 0;
}

或者至少我认为我很接近...任何帮助都非常感谢。我当前的错误出现在主函数中的 IF 语句上。我可能会有更多的错误,因为我对C++非常陌生。就像我说的,如果可以的话,请帮助我。

如果你只是想要代码,它是,但如果你想练习,我建议你在这个代码中实现错误检查(格式不正确),并将代码中的循环更改为函数。

#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
    string inp;
    cout<<"Please enter an array of numbers in the following formn"
          "(where N's are any numbers) :n"
          "[N N NnN N NnN N N"<<endl;
    double m1[3][3],m2[3][3];
    for(short i=0;i<3;i++){
        cout<<"> ";
        if(i==0) cout<<"[ ";
        getline(cin,inp);
        stringstream inps(inp);
        for(short j=0;j<3;j++){
            inps>>m1[i][j];
        }
    }// Get the first array
    cout<<"Now enter the second array :n";
    for(short i=0;i<3;i++){
        cout<<"> ";
        if(i==0) cout<<"[ ";
        getline(cin,inp);
        stringstream inps(inp);
        for(short j=0;j<3;j++){
            inps>>m2[i][j];
        }
    }// Get the second one
    bool not_equal=false;
    for(short i=0; i<3 && !not_equal ;i++)
        for(short j=0; j<3 && !not_equal ;j++)
            if(m1[i][j]!=m2[i][j])
                not_equal=true;// Test equality
    if(not_equal)
        cout<<"The two arrays you entered are not equal !!"<<endl;
    else
        cout<<"The two arrays you entered are EQUAL ."<<endl;
    return 0;
}
#include <iostream>
using namespace std;
int main()
 {
int size = 3;
int m2[size][size];
int m1[size][size];
cout << "Please enter the first array: " << endl;
for(int i=0;i<size;i++)
 {
  for(int j=0;j<size;j++)
   {
    cin >> m1[i][j];
   }
 }
cout << "Please enter the second array: " << endl;
for(int i=0;i<size;i++)
 {
  for(int j=0;j<size;j++)
   {
    cin >> m2[i][j];
   }
 }
int flag=1;
for(int i=0;i<size;i++)
 {
  for(int j=0;j<size;j++)
   {
    if(m2[i][j]!=m1[i][j])
     {
      flag=0;
     }
   }
 }
if(flag==1)
{
 cout <<"SAME"<<endl;
}
else if(flag==0)
{
 cout <<"NOT SAME"<<endl;
}
return 0;

}