我一直在研究一个程序来显示两个矩阵相减的结果。该程序允许用户输入矩阵的大小,然后提示用户输入这两个矩阵的值。最后,程序应该单独显示这两个矩阵,然后显示两者的相减结果。矩阵相减无尽显示问题
当前代码运行时,输出只是数字的无尽显示。我无法找到造成此问题的原因。我很感激任何有关导致此问题的原因。
谢谢!
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;
}
这听起来像你可能需要学习如何使用调试器来逐步通过你的代码。使用一个好的调试器,您可以逐行执行您的程序,并查看它与您期望的偏离的位置。如果你打算做任何编程,这是一个重要的工具。进一步阅读:** [如何调试小程序](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/)** – NathanOliver