编写一个 C++ 程序,使用示例检查两个矩阵是否相等。在此矩阵相等示例中,我们使用嵌套 for 循环来迭代元素。接下来,我们在 for 循环中使用 If 语句 (if(a[rows][columns] != b[rows][columns])) 来检查矩阵 a 中的每个元素是否不等于矩阵 b 中的元素。
如果为真,则将 isEqual 更改为 0 并应用 break 语句退出循环。接下来,我们在 If Else 语句中使用 isEqual 变量来打印矩阵输出。
#include<iostream>
using namespace std;
int main()
{
int i, j, rows, columns, isEqual;
cout << "\nPlease Enter the rows and Columns a Equal Matrix = ";
cin >> i >> j;
int a[i][j], b[i][j];
cout << "\nPlease Enter the First Matrix Items = ";
for(rows = 0; rows < i; rows++) {
for(columns = 0; columns < i; columns++) {
cin >> a[rows][columns];
}
}
cout << "\nPlease Enter the Second Matrix Items = ";
for(rows = 0; rows < i; rows++) {
for(columns = 0; columns < i; columns++) {
cin >> b[rows][columns];
}
}
isEqual = 1;
for(rows = 0; rows < i; rows++)
{
for(columns = 0; columns < j; columns++)
{
if(a[rows][columns] != b[rows][columns])
{
isEqual = 0;
break;
}
}
}
if(isEqual == 1)
{
cout << "\nMatrix a is Equal to Matrix b";
}
else
{
cout << "\nMatrix a is Not Equal to Matrix b";
}
return 0;
}

我来尝试两个不同的输入。
Please Enter the rows and Columns a Equal Matrix = 2 2
Please Enter the First Matrix Items =
1 2
3 4
Please Enter the Second Matrix Items =
1 2
3 5
Matrix a is Not Equal to Matrix b
在此矩阵相等代码中,我们使用了一些额外的 cout 语句来显示迭代次数、行号、列号和元素。请参考 C++ 程序。
#include<iostream>
using namespace std;
int main()
{
int i, j, rows, columns, isEqual;
cout << "\nPlease Enter the rows and Columns = ";
cin >> i >> j;
int a[i][j], b[i][j];
cout << "\nPlease Enter the First Mat Items = ";
for(rows = 0; rows < i; rows++) {
for(columns = 0; columns < i; columns++) {
cin >> a[rows][columns];
}
}
cout << "\nPlease Enter the Second Mat Items = ";
for(rows = 0; rows < i; rows++) {
for(columns = 0; columns < i; columns++) {
cin >> b[rows][columns];
}
}
isEqual = 1;
for(rows = 0; rows < i; rows++)
{
cout << "\nRow Iteration = " << rows + 1 << ", Row Number = " << rows;
for(columns = 0; columns < j; columns++)
{
cout << "\nColumn Iteration = " << columns + 1 << ", Column Number = " <<
columns << ", and Row Number = " << rows;
cout << "\nThe Result of a[" << rows << "][" << columns << "] != "
<< "b[" << rows << "][" << columns << "] = " << (a[rows][columns] != b[rows][columns]);
if(a[rows][columns] != b[rows][columns])
{
isEqual = 0;
break;
}
}
}
if(isEqual == 1)
{
cout << "\nMatrix a is Equal to b";
}
else
{
cout << "\nMatrix a is Not Equal to b";
}
return 0;
}
Please Enter the rows and Columns = 2 2
Please Enter the First Mat Items =
1 2
3 4
Please Enter the Second Mat Items =
1 2
3 4
Row Iteration = 1, Row Number = 0
Column Iteration = 1, Column Number = 0, and Row Number = 0
The Result of a[0][0] != b[0][0] = 0
Column Iteration = 2, Column Number = 1, and Row Number = 0
The Result of a[0][1] != b[0][1] = 0
Row Iteration = 2, Row Number = 1
Column Iteration = 1, Column Number = 0, and Row Number = 1
The Result of a[1][0] != b[1][0] = 0
Column Iteration = 2, Column Number = 1, and Row Number = 1
The Result of a[1][1] != b[1][1] = 0
Matrix a is Equal to b
C++ 程序使用 While 循环检查两个矩阵是否相等
#include<iostream>
using namespace std;
int main()
{
int i, j, rows, columns, isEqual;
cout << "\nPlease Enter the rows and Columns = ";
cin >> i >> j;
int a[i][j], b[i][j];
cout << "\nPlease Enter the First Matrix Items = ";
for(rows = 0; rows < i; rows++) {
for(columns = 0; columns < i; columns++) {
cin >> a[rows][columns];
}
}
cout << "\nPlease Enter the Second Matrix Items = ";
for(rows = 0; rows < i; rows++) {
for(columns = 0; columns < i; columns++) {
cin >> b[rows][columns];
}
}
isEqual = 1;
rows = 0;
while(rows < i)
{
columns = 0;
while(columns < j)
{
if(a[rows][columns] != b[rows][columns])
{
isEqual = 0;
break;
}
columns++;
}
rows++;
}
(isEqual == 1) ? cout << "\nMatrix a is Equal to b" :
cout << "\nMatrix a is Not Equal to b";
return 0;
}
Please Enter the rows and Columns = 3 3
Please Enter the First Matrix Items = 10 20 30 40 50 60 70 80 90
Please Enter the Second Matrix Items = 10 20 30 40 50 60 70 80 90
Matrix a is Equal to b