【发布时间】:2014-09-24 09:54:56
【问题描述】:
首先抱歉代码有点混乱,因为我正在摆弄不同的东西来尝试让它工作。到目前为止,我的代码可以很好地乘方矩阵;但是,它在计算非方阵方面存在困难。调试后我最好的猜测是我如何重新调整向量的大小,并且存在导致程序崩溃的越界错误。任何帮助将不胜感激,我的代码应该能够使用矩阵乘法规则乘以任何向量大小。
我还想指出这是一个硬件分配,所以我仅限于如何构建我的代码,基本上只能使用向量,不能编写自己的类等......
#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;
}
}
【问题讨论】:
-
好吧,让代码更安全的一种方法是使用 vector::size() 来限制循环,而不是使用容易具有错误最大值的变量。更好的是,使用从 begin() 到 end() 的迭代器。
-
I also would like to note this is a HW assignment so I am limited to how I can build my code, basically ONLY using vectors, cannot write your own class etc....哇。最后是一个实际允许您使用 C++ 的硬件分配。通常我们在 SO 上得到相反的结果,学生不能使用vector。 -
是的,我对二维向量很陌生,所以我不知道检查二维数组大小的正确格式实现。
-
为什么需要将
rows和cols传递给setMatrix?向量知道自己的大小,而不必传递这些值。除非您需要在开始输入循环之前先调整矩阵的大小? -
它用于 for 循环,我再次不熟悉如何使用 2D 数组大小实现和适当的比较,所以我更容易使用行和列来遍历矩阵。跨度>
标签: c++ matrix vector multiplication