2014-09-24 82 views
0

首先對於稍微凌亂的代碼感到抱歉,因爲我在擺弄不同的東西以嘗試使其工作。到目前爲止,我的代碼可以很好地乘以方矩陣;然而,計算非正方矩陣有困難。調試後的最佳猜測是我如何重新調整向量的大小,並且出現導致程序崩潰的超出界限的錯誤。任何幫助將不勝感激,我的代碼應該能夠使用矩陣乘法規則乘以任何向量大小。非正方矩陣乘法幫助C++

我也想指出,這是一個硬件分配,所以我僅限於如何建立我的代碼,基本上只使用載體,不能寫自己的類等....

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

void multiply_matrices(vector <vector<int> > matrix1,vector <vector<int> > matrix2, int cols, int rows2); 
void setMatrix(vector <vector<int> > &matrix, int rows, int cols); 

int main() 
{ 
    int rows, cols, rows2, cols2;  
    vector< vector<int> > matrix, matrix2;   
    cout<<"Please enter the number of Rows and Columns for your first Matrix."<<endl; 
    cout<<"Rows: "; 
    cin>>rows; 
    cout<<"Columns: "; 
    cin>>cols; 

    matrix.resize(cols, vector<int>(rows,0)); //Saw this online so not sure how it works but it works, if i take out one i cant do row<column and vice versa 
    matrix.resize(rows, vector<int>(cols,0)); 

    cout<<"Size has been declared, please enter data for your matrix"<<endl; 

    setMatrix(matrix,rows,cols); 

    cout<<"Second Matrix Automatically Set by Matrix Multiplication Rule"<<endl; //Just automatically sets second matrix as per Matrix Multiplication Rule 
    rows2=cols; 
    cols2=rows; 
    cout<<"Second Matrix Size is: " << rows2 << " by " << cols2 << endl; 
    matrix2.resize(cols2, vector<int>(rows2,0)); 
    matrix2.resize(rows2, vector<int>(cols2,0)); 

    setMatrix(matrix2,rows2,cols2);   

    cout<<"Multiplied Matrix is:"<<endl; 
    multiply_matrices(matrix,matrix2,cols,rows2); 

    system("PAUSE"); 
    return 0; 
} 

void setMatrix(vector <vector<int> > &matrix, int rows,int cols){ 
    int num; 
    for(int i = 0; i < rows; i ++) 
    { 
     for (int j = 0; j < cols; j++) 
     { 
      cout << "Enter Value for Row " << (i+1) << " Column " << (j+1) << ": "; 
      cin>>num; 
      matrix[i][j]=num;    
     }   
     cout << endl; 
    } 

/*for(int i = 0; i < rows; i ++) 
    { 
     for (int j = 0; j < cols; j++) 
     { 
      cout << matrix[i][j] << " "; 
     }  
     cout << endl; 
    }   
    */ 
    } 
void multiply_matrices(vector <vector<int> > matrix1,vector <vector<int> > matrix2, int cols, int rows2){ 
    vector< vector<int> > tempMatrix; 
    int newrows=rows2; 
    int newcols=cols; 
    int sum; 
    tempMatrix.resize(newcols, vector<int>(newrows,0)); //Resizing new matrix to proper size, so if it was (2x3)(3x2), new matrix is (3x3) 

    for (int i = 0; i < newrows; i++)     //This Works Fine for Square Matrixes but not for others, i have no clue how to fix it? 
    { 
     for (int j = 0; j < newcols; j++){ 
      //sum=0;  
      for (int u = 0; u < newcols; u++) 
      { 
       //sum+=matrix1[i][u] * matrix2[u][j]; 
       //tempMatrix[i][j]=sum; 
       tempMatrix[i][j] += matrix1[i][u] * matrix2[u][j]; 
      } 
     } 
    } 
    for(int i = 0; i < newrows; i ++) 
    { 
     for (int j = 0; j < newcols; j++) 
     { 
      cout << tempMatrix[i][j] << " "; 
     }   
     cout << endl; 
    }   
} 
+0

好了,一個使你的代碼更安全的方式使用是矢量::大小來限制你的循環() ,而不是容易出現錯誤最大值的變量。更好的是,使用從begin()到end()的迭代器。 – PaulMcKenzie 2014-09-24 10:01:22

+0

'我也想注意到這是一個硬件分配,所以我限於如何構建我的代碼,基本上只使用向量,不能編寫自己的類等....哇。最後是一個HW分配,它實際上允許你使用C++。通常我們在SO上得到相反的地方,學生不能使用'vector'。 – PaulMcKenzie 2014-09-24 10:03:31

+0

是啊我很新的2D矢量,所以我不知道正確的格式實現來檢查二維數組中的大小。 – user3051442 2014-09-24 10:06:47

回答

1

初始化您的第一個矩陣是這樣的:

matrix.resize(rows, vector<int>(cols,0)); 

和你的第二個這樣的:

matrix2.resize(rows2, vector<int>(cols2,0)); 

其中rows2 = cols。請注意,沒有暗示cols2 == rows的「乘法規則」。

的問題是在你的multiply_matrices功能在毛圈應該

for (int i = 0; i < rows; i++) // or matrix1.size() 
for (int j = 0; j < cols2; j++) // or tempMatrix[i].size() 
for (int u = 0; u < cols; u++) // or rows2 or matrix1[i].size() 

但在評論中已經指出,這將是更好地使用vector::size()強似尺寸爲附加參數。

此外,如果你乘(2×3)(3×2),結果爲(2×2):

tempMatrix.resize(rows, vector<int>(cols2,0)); 
+0

是的謝謝你,它應該是2x2讓它向後謝謝。讓我擺弄一下大家的建議,謝謝。 – user3051442 2014-09-24 10:54:07

+0

老兄謝謝!測試它,所以它非常完美!基本上是一個邏輯問題。但是,如果能夠使用vector :: size()向我展示循環,那麼我將非常感激,以便將來可以正確地實現二維數組循環。謝謝 – user3051442 2014-09-24 11:03:37

1

resize()函數沒有錯。可能是錯誤的是,你忽略了最大尺寸,只依靠傳遞給你函數的變量。

例如,您的setMatrix函數通過rowscols,但這不是必需的。

函數應該只使用矩陣提供的尺寸爲循環被改寫:

void setMatrix(vector<vector<int> > &matrix) 
{ 
    int num; 
    for(int i = 0; i < matrix.size(); ++i) 
    { 
     for (int j = 0; j < matrix[i].size(); ++j) 
     { 
      cout << "Enter Value for Row " << (i+1) << " Column " << (j+1) << ": "; 
      cin>>num; 
      matrix[i][j] = num;    
     }   
     cout << endl; 
    } 
} 

你有同樣的問題與multiply_matrix。你應該做的是確保你的循環使用返回值vector::size(),你不這樣做。問題就在這裏:

for (int i = 0; i < newrows; i++)  
{ 
    for (int j = 0; j < newcols; j++) 
    { 
     for (int u = 0; u < newcols; u++) 
     { 
      tempMatrix[i][j] += matrix1[i][u] * matrix2[u][j]; 

您的大小來tempMatrixnewrowsnewcols列。但是,您如何知道matrix1matrix2至少有newrows行和newcols列?你不知道,但你只是假設他們這樣做。

因此,您需要確保matrix1和matrix2的大小可以容納行數/列數,或者限制那些使用最小行數/列數的循環。

總的來說,問題是你沒有在你看到的代碼中使用vector::size()。因此,開始使用size()以利於您 - 不要創建可能表示行和列大小的多餘(可能錯誤設置)的變量。

+0

這是正確的乘法for(int i = 0;我 user3051442 2014-09-24 10:44:46

+0

oi抱歉,剛剛從第一個複製粘貼開始,對網站功能也很糟糕-_- – user3051442 2014-09-24 10:46:14