2015-05-08 45 views
0

我輸入矩陣的值。現在它已被修改爲矢量而不是陣列,這要感謝某人幫助我修復我的代碼。然而,程序現在在進入Matrix B的元素後崩潰了。另外,這非常奇怪,但是在輸入任意矩陣的列數之後,程序允許我繼續輸入更多的值在提示我輸入矩陣的元素之前。當我輸入矩陣的值時,爲什麼我的程序崩潰?

#include <iostream> 
#include <vector> 

using namespace std; 

int main() 
{ 
    int x,y,i,j,m,n; 
    vector<vector<int> > A; 
    vector<vector<int> > B; 
    vector<vector<int> > C; 


    cout<<"Enter the number of rows for Matrix A: "<<endl; 
    cin>>x; 
    cout<<"Enter the number of rows for Matrix B: "<<endl; 
    cin>>y; 

    //dynamically resizeable array of dynamically resizeable arrays 
    A.resize(x); // allocate storage for x dimension. 
    for (i = 0; i < x; i++) 
    { 
    A[i].resize(y); // allocate storage for y dimension for this one x 
    for (j = 0; j < y; j++) 
    { 
     cin >> A[i][j]; 
    } 
    cout << endl; 
    } 

    cout<<"\n\nEnter the elements of Matrix A: "<<endl; 

    for(i=0;i<x;i++) 
    { 
     for(j=0;j<y;j++) 
     { 
      cin>>A[i][j]; 
     } 
     cout<<endl; 
    } 

    cout<<"\n\nMatrix A :\n\n"; 

    for(i=0;i<x;i++) 
    { 
     for(j=0;j<y;j++) 
     { 
      cout<<"\t"<<A[i][j]; 
     } 
     cout<<endl; 
    } 

    cout<<"********************************************************"<<endl; 

    cout<<"Enter the number of rows for Matrix B: "<<endl; 
    cin>>m; 
    cout<<"Enter the number of columns for Matrix B: "<<endl; 
    cin>>n; 

    B.resize(m); // allocate storage for x dimension. 
    for (i = 0; i < m; i++) 
    { 
    B[i].resize(n); // allocate storage for y dimension for this one x 
    for (j = 0; j < n; j++) 
    { 
     cin >> A[i][j]; 
    } 
    cout << endl; 
    } 

    cout<<"\n\nEnter elements for Matrix B :\n\n"; 

    for(i=0;i<m;i++) 
    { 
     for(j=0;j<n;j++) 
     { 
      cin>>B[i][j]; 
     } 
     cout<<endl; 
    } 


    cout<<"\n\nMatrix B :\n\n"; 

    for(i=0;i<m;i++) 
    { 
     for(j=0;j<n;j++) 
     { 
      cout<<"\t"<<B[i][j]; 
     } 
     cout<<endl; 
    } 

    if(y==m) 
    { 

     for(i=0;i<x;i++) 
     { 
      for(j=0;j<n;j++) 
      { 
       C[i][j]=0; 
       for(int k=0;k<m;k++) 
       { 
        C[i][j]=C[i][j]+A[i][k]*B[k][j]; 
       } 
      } 
     } 

     cout<<"*******************************************************"<<endl; 

     cout<<"\n\nMultiplication of Matrix A and Matrix B: \n\n"; 

     for(i=0;i<x;i++) 
     { 
      for(j=0;j<n;j++) 
      { 
       cout<<"\t"<<C[i][j]; 
      } 
      cout<<endl; 
     } 
    } 
    else 
    { 
     cout<<"\n\nMultiplication is not possible"<<endl;; 
    } 

    system("pause"); 
    return 0; 
} 
+3

當然,你的意思是「我的程序崩潰」......你的程序只有10×10的矩陣空間。猜猜你有20×1矩陣會發生什麼? – lmz

+0

這是編譯的程序或編譯器崩潰? – SleuthEye

+0

說編譯器崩潰使得它聽起來像編譯器本身有一個錯誤,因爲編譯器在構建程序時不應該真正「崩潰」 - 它應該成功或告訴你在編寫程序時出現錯誤。但是就像其他評論者所說的那樣,這聽起來像是你編譯成功後的程序在編譯後以特定的方式運行時會崩潰。 –

回答

1

你說矩陣是20×1,但你的數組是10×10。所以你的描述是不對的。

但是說了這麼多,如果您尊重數組是10 x 10,仍然可以挽救您的原始代碼,並且不會崩潰。您只需編寫循環,以便不超過數組無論輸入的是什麼輸入。

但是,您不需要自行重寫循環 - 您需要做的就是調整輸入,使其不超過數組的邊界。

#include <iostream> 
#include <algorithm> 
using namespace std; 

int main() 
{ 
    const int MAXSIZE_X = 10; 
    const int MAXSIZE_Y = 10; 
    int A[MAXSIZE_X][MAXSIZE_Y], B[MAXSIZE_X][MAXSIZE_Y], C[MAXSIZE_X][MAXSIZE_Y]; 
    int x,y,i,j,m,n; 
    int m_x, m_y; // used for inputting 
    cout<<"Enter the number of rows for Matrix A: "<<endl; 
    cin>> m_x; 
    cout<<"Enter the number of rows for Matrix B: "<<endl; 
    cin>> m_y; 

    // set the minimum value for x and y here 
    x = std::max(0, std::min(m_x, MAXSIZE_X)); 
    y = std::max(0, std::min(m_y, MAXSIZE_Y)); 

    //... Rest of the code to populate arrays 
    //... 
    cout<<"Enter the number of rows for Matrix B: "<<endl; 
    cin>>m_x; 
    cout<<"Enter the number of columns for Matrix B: "<<endl; 
    cin>>m_y; 

    // set the minimum allowed values for m and n 
    m = std::max(0, std::min(m_x, MAXSIZE_X)); 
    n = std::max(0, std::min(m_y, MAXSIZE_Y)); 
    //... 
    // Rest of the code goes here 
    //... 
} 

我所做的就是用std::min分配給xymn最小什麼的被輸入和數組的實際尺寸大小。另請注意,我們不允許使用負值,以確保該值不低於0(使用std::max)。

一旦你這樣做,循環現在不會超過數組的邊界。底線是,如果你有一個限制,你應該扼殺你的陣列,以便你永遠不會超出界限。

另請注意,如果您確實將大小更改爲20 x 1(通過更改MAXSIZE_XMAXSIZE_Y的值),則其餘代碼都不需要更改。


編輯:

現在,你已經改變了你原來的問題,我的回答上面不再成立。請不要這樣做,因爲現在其他人已經失去了所給出答案的所有背景以及您發佈的問題。

鑑於這種情況,你的新代碼的問題是這樣的:

B.resize(m); // allocate storage for x dimension. 
for (i = 0; i < m; i++) 
{ 
    B[i].resize(n); // allocate storage for y dimension for this one x 
    for (j = 0; j < n; j++) 
    { 
     cin >> A[i][j]; // <-- This is supposed to be B, not A 
    } 
    cout << endl; 
} 

而且,你不需要調用vector::resize在一個循環。您可以在一次調用中調整整個矢量,包括行和列。

實施例:

A.resize(x, vector<int>(y, 0)); 

即調整大小的A矩陣x行和y列。

另一個錯誤是您無法調整C或生成矩陣的大小。

所以爲了更加全面,代碼應該是這個樣子:

//dynamically resizeable array of dynamically resizeable arrays 
A.resize(x, vector<int>(y, 0)); 
for (i = 0; i < A.size(); i++) 
{ 
    for (j = 0; j < A[i].size(); j++) 
     cin >> A[i][j]; 
    cout << endl; 
} 

cout << "Enter the number of rows for Matrix B: " << endl; 
cin >> m; 
cout << "Enter the number of columns for Matrix B: " << endl; 
cin >> n; 

B.resize(m, vector<int>(n, 0)); // allocate storage for x dimension. 
for (i = 0; i < m; i++) 
{ 
    for (j = 0; j < n; j++) 
     cin >> B[i][j]; 
    cout << endl; 
} 

// resize the resultant matrix accordingly 
C.resize(x, vector<int>(n, 0)); 

我刪除了所有,只是輸出你輸入什麼樣的絨毛,如在理解什麼是真正需要的並不重要完成。

除此之外,std::vector通過使用vector::size()函數知道它的大小。您不再需要攜帶變量,如x,y等來表示項目的數量。使用無關的變量可能會導致某些地方出現問題。

+0

我從來沒有想過要這樣調整矢量。美麗。玩得更多一點,它似乎也與構造函數一起工作。我應該注意的任何警告,還是應該提出這個問題? – user4581301

1

一個20X1矩陣

int A[10][10] 

將有困難得到在陣列20層的元件具有最大尺寸的一丁點兒10.

嘗試:

cout<<"Enter the number of rows for Matrix A: "<<endl; 
cin>>x; 
cout<<"Enter the number of rows for Matrix B: "<<endl; 
cin>>y; 

cout << "\n\nEnter the elements of Matrix A: " << endl; 

int ** A; // raw pointer. Not a good idea. 
A = new int*[x]; // allocate storage for x dimension. 

for (i = 0; i < x; i++) 
{ 
    A[i] = new int[y]; // allocate storage for y dimension for this one x 
    for (j = 0; j < y; j++) 
    { 
     cin >> A[i][j]; 
    } 
    cout << endl; 
} 

但是最後你有很多刪除[]要做,以釋放所有的內存。所以了在你的程序文件的頂部:

#include<iostream> 
#include<vector> 

using namespace std; 

而在主下來,你是在用戶輸入閱讀:

vector<vector<int> > A; //dynamically resizeable array of dynamically resizeable arrays 
A.resize(x); // allocate storage for x dimension. 
for (i = 0; i < x; i++) 
{ 
    A[i].resize(y); // allocate storage for y dimension for this one x 
    for (j = 0; j < y; j++) 
    { 
     cin >> A[i][j]; 
    } 
    cout << endl; 
} 

雖然我編輯,有在移動輸入值代碼到一個函數,所以你不必重複它來讀取B矩陣。

這個向量beastie會在函數結束後自動清理完畢。所有人都快樂。

注意在vector<vector<int> > A;> >的空間你需要這個空間,舊的編譯器,因爲>>告訴編譯器將數據移出權利,而不是終止模板參數列表。這個可憐的,困惑的編譯器拋出了一個可怕的錯誤消息鏈,沒有人願意穿過或解釋。

+0

從我的答案可以解釋的是,我應該使用矢量,而不是數組。我也對''using namespace std;''和'vector heyheythere

+0

@heyheythere如果你只是不想讓你的程序崩潰,那麼你可以根據我的回答調整輸入。如果出於某種原因,您必須根據用戶輸入動態調整矩陣大小,那麼您應該使用'vector',如上所述。 – PaulMcKenzie

+0

@heyheythere不,那只是標誌着代碼中的巨大差距。 '#include '最好放在其他包含的頂部。其餘的則放入你正在讀取用戶值的主函數中。我會更新以清楚說明。 – user4581301

相關問題