Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 非平方矩阵乘法帮助C++;_C++_Matrix_Vector_Multiplication - Fatal编程技术网

C++ 非平方矩阵乘法帮助C++;

C++ 非平方矩阵乘法帮助C++;,c++,matrix,vector,multiplication,C++,Matrix,Vector,Multiplication,首先,很抱歉代码有点凌乱,因为我正在摆弄不同的东西试图让它工作。到目前为止,我的代码可以很好地乘以平方矩阵;然而,它在计算非平方矩阵时有困难。调试后,我的最佳猜测是如何重新调整向量大小,以及是否存在导致程序崩溃的越界错误。任何帮助都将不胜感激,我的代码应该能够使用矩阵乘法规则乘法任何向量大小 我还想指出,这是一个硬件作业,所以我仅限于如何构建我的代码,基本上只使用向量,不能编写自己的类等等 #include <iostream> #include <vector> usi

首先,很抱歉代码有点凌乱,因为我正在摆弄不同的东西试图让它工作。到目前为止,我的代码可以很好地乘以平方矩阵;然而,它在计算非平方矩阵时有困难。调试后,我的最佳猜测是如何重新调整向量大小,以及是否存在导致程序崩溃的越界错误。任何帮助都将不胜感激,我的代码应该能够使用矩阵乘法规则乘法任何向量大小

我还想指出,这是一个硬件作业,所以我仅限于如何构建我的代码,基本上只使用向量,不能编写自己的类等等

#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;
    }          
}
#包括
#包括
使用名称空间std;
无效乘法矩阵(向量矩阵1、向量矩阵2、整数列、整数行2);
void setMatrix(向量和矩阵、int行、int列);
int main()
{
int行、列、行2、列2;
向量<向量>矩阵,矩阵2;

cout函数resize()
没有任何错误。可能的错误是忽略了最大大小,只依赖传递给函数的变量

例如,您的
setMatrix
函数被传递到
rows
cols
,但这不是必需的

应仅使用矩阵重写函数,以提供循环的大小:

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;
    }
}
您将
tempMatrix
调整为
newrows
行和
newcols
列。但是您如何知道
matrix1
matrix2
至少有
newrows
行和
newcols
列?您不知道,但您只是假设它们有

因此,您要么需要确保matrix1和matrix2的大小能够容纳行/列的数量,要么限制这些循环以使用最小的行/列


总的来说,问题是在我看到的代码中没有地方使用
vector::size()
。因此,开始使用
size()
对您有利——不要创建多余的(可能设置错误的)变量来表示行和列的大小。

像这样初始化第一个矩阵:

matrix.resize(rows, vector<int>(cols,0));
matrix2.resize(rows2, vector<int>(cols2,0));
但正如评论中所述,最好使用
vector::size()
,而不是将大小作为附加参数传递

此外,如果乘以(2x3)(3x2),结果为(2x2):

tempMatrix.resize(行,向量(cols2,0));

让代码更安全的一种方法是使用vector::size()限制循环,而不是使用容易产生错误最大值的变量。更好的方法是使用从begin()到end()的迭代器<>代码>我还想注意这是一个HW作业,所以我只限于如何构建我的代码,基本上只用向量,不能编写你自己的类……< /代码>哇。最后一个HW赋值实际上允许你使用C++。通常我们得到的是相反的,所以学生不能使用<代码>矢量< /代码>。是的,我很新到2。D向量,因此我不知道检查2D数组大小的正确格式实现。为什么需要将
传递到
集合矩阵
?向量知道自己的大小,而不必传递这些值。除非在开始输入循环之前需要先调整矩阵的大小?这是用于for循环的增益我不熟悉如何使用2D数组大小实现和适当的比较,因此我更容易使用行和列来遍历矩阵。这对(int i=0;imatrix2.resize(rows2, vector<int>(cols2,0));
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()
tempMatrix.resize(rows, vector<int>(cols2,0));