提示用户输入两个 3x3 int 数组并检查等效性
Prompt user to enter two 3x3 int arrays and check equivalence
这是我到目前为止想出的...我非常接近,实际上还剩下一个错误供它编译。
#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;
}
相关文章:
- 如何在cpp.中使用协议缓冲区存储大缓冲区/数组(char/int)
- 按字符值访问int数组
- 将 int 数组转换为 std::vector<int*>
- 比较if语句中的数组值和int值
- C++,在int数组中输入字符串或字符会输出0,而不是ascii或error
- 在c++中获取两个大int,并将它们存储在数组中
- 声明数组>> int arr[] 时出错;在C++(虽然在 Java 中有效)?
- 有没有办法用 2D 数组 (int) 重载构造函数?
- 当大小未知时,如何迭代指向数组 (int*) 的指针?
- 接受来自函数的二维数组(int 类型)并将其返回到主函数
- 数组 + int 作为一个参数做什么
- 如何制作'bit'数组而不是数组'int'?
- 动态2d数组:int(*ptr)[4]=new int[7][4];
- 为[z][y][x]int定义数组int[x][y][z]有什么好处?
- 用Visual c++编写二维数组int[n][m]到HDF5文件
- 数组int未正确返回
- 将元素推送到动态分配的指针数组int C++
- 如何在另一个数组中插入多维数组(int而非char)
- 将数组(int*)设置为NULL是否释放其占用的内存
- C++如何将数组 int[10] push_back std::vector<int[10]>?