我一直在研究一个程序来显示两个矩阵的减法结果。该程序允许用户输入矩阵的大小,然后提示用户输入这两个矩阵的值。最后,程序应该单独显示这两个矩阵,然后显示两个矩阵的减法结果。
目前,当代码运行时,输出只是无穷无尽的数字显示。我找不到创造这个问题的原因。对于导致此问题的原因,我将不胜感激。
谢谢!
using namespace std;
#include <iostream>
#include <conio.h>
int main(){
int i = 0, j = 0, n=0, a[10][10], b[10][10], c[10][10];
bool positive = false;
cout << "Enter the size of the two - dimensional array: ";
cin >> n;
while (!positive){
if (n >= 0){
positive = true;
}
else {
cout << "Please enter a positive number for the size of the matrix.";
cin >> n;
}
}
cout << "Enter the values of the elements of array A" << endl;
for (i = 0; i < n; i++){
for (j = 0; j < n; j++){
cin >> a[i][j];
}
}
cout << "Enter the values of the elements of array B" << endl;
for (i = 0; i < n; i++){
for (j = 0; j < n; j++){
cin >> b[i][j];
}
}
cout << "Matrix A:" << endl;
for (i = 0; i < n; i++){
for (j = 0; i < n; j++){
cout << a[i][j] << " ";
}
}
cout << "Matrix B:" << endl;
for (i = 0; i < n; i++){
for (j = 0; j < n; j++){
cout << b[i][j] << " ";
}
cout << "\n";
}
for (i = 0; i < n; i++){
cout << endl;
for (j = 0; j < n; j++){
c[i][j] = a[i][j] - b[i][j];
}
cout << "\n";
}
cout << "Matrix A - Matrix B: " << endl;
for (i = 0; i < n; i++){
for (j = 0; j < n; j++){
cout << c[i][j] << " ";
}
cout << "\n";
}
return 0;
}
答案 0 :(得分:1)
这条线路可能会造成麻烦吗?
cout << "Matrix A:" << endl;
for (i = 0; i < n; i++){
for (j = 0; i < n; j++){
cout << a[i][j] << " ";
}
}
改变j的for循环(即,j