2016-03-04 64 views
0

我一直在研究一个程序来显示两个矩阵相减的结果。该程序允许用户输入矩阵的大小,然后提示用户输入这两个矩阵的值。最后,程序应该单独显示这两个矩阵,然后显示两者的相减结果。矩阵相减无尽显示问题

当前代码运行时,输出只是数字的无尽显示。我无法找到造成此问题的原因。我很感激任何有关导致此问题的原因。

谢谢!

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; 
} 
+3

这听起来像你可能需要学习如何使用调试器来逐步通过你的代码。使用一个好的调试器,您可以逐行执行您的程序,并查看它与您期望的偏离的位置。如果你打算做任何编程,这是一个重要的工具。进一步阅读:** [如何调试小程序](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/)** – NathanOliver

回答

1

大概这条线是造成麻烦的?

cout << "Matrix A:" << endl; 
for (i = 0; i < n; i++){ 
    for (j = 0; i < n; j++){ 

     cout << a[i][j] << " "; 
    } 
} 

更改j的for-loop(即,j < n)。看看是否有帮助..

+0

不能相信我错过了!非常感谢! – AddieCaddy

+0

没问题,这是一个常见的错误:)。为了长远的利益,请注意NathanOliver的建议 – Minh