Why did I need to use this statement twice - Matrix Multiplication

耗尽温柔 提交于 2019-12-23 02:34:07

问题


I was developing a program to find matrix multiplication.

#include <iostream>

using namespace std;

int main()
{
    int a=0,b=0,c=0,d=0,e=0;
    cout<<"Enter the order of the first matrix  A  \n\nNumber of Rows : ";
    cin>>a;
    cout<<"\nNumber of Columns : ";
    cin>>b;
    cout<<endl;
    int matrixA[a][b];
    cout<<"Enter the matrix Elements "<<endl;
    for(int m=0; m<a; m++)
    {

        for(int n=0; n<b; n++)
        {
            cout<<"A ("<< m+1 <<" , "<<n+1<<" ) =";
            cin>>matrixA[m][n];
            //cout<<",";
        }
        cout<<endl;
    }

//////////////////////////  Startup

    cout<<"Enter the order of the Second matrix  A  \n\nNumber of Rows : "<<b;
    c=b;
    cout<<"\nNumber of Columns : ";
    cin>>d;
    cout<<endl;
    int matrixB[c][d];
    cout<<"Enter the matrix Elements "<<endl;
    for(int p=0; p<c; p++)
    {
        for(int q=0; q<d; q++)
        {
            cout<<"B ("<< p+1 <<" , "<<q+1<<" ) =";
            cin>>matrixB[p][q];
            //cout<<",";
        }
        cout<<endl;
    }

    ///////////// initialisting matrixAns
    int matrixAns[a][d];

    for(int p=0; p<a; p++)
    {
        for(int q=0; q<d; q++)
        {
            matrixAns[p][q]=0;    
        }    
    }

////////////////////  finding ans
    for(int r=0; r<a; r++)
    {
        for(int s=0; s<d; s++)
        {
            for(int t=0; t<b; t++)
            {    
                e = matrixA[r][t]*matrixB[t][s];       
            }
            matrixAns[r][s] = e+matrixAns[r][s];
            matrixAns[r][s] = e+matrixAns[r][s];   //dont know why i have to add this same code again
    }
    }

////////////////////// Ans Printing    
    cout<<"\nMatrix Multiplication Answer  \n"<<endl;
    for(int h=0; h<a; h++)
    {    
        for(int i=0; i<d; i++)
        {    
            cout<<" "<<matrixAns[h][i]<<" ";    
        }
        cout<<endl;
    }    
}

And one important thing: this is not my homework or assignment!

The method that I have used works perfectly but it does not give me the right answer until I use this statement twice. (I got this by a trial and error method).

matrixAns[r][s] = e+matrixAns[r][s];  

I have also initailised the matrixAns by using a loop (and had set it to 0).

I am new to C++ and want to know what was the error that I got and how the use of the two statements gives me correct answer.

Is there any way to get rid of one of the statements without destroying the app?


回答1:


You're not doing the dot products correctly when calculating your answer. You need to multiply individual cells between the two matrices, then sum all those products for an individual cell in your answer.

Your code is only taking the product, in e, of the last multiplication - matrix[r][b - 1] * matrixB[b - 1][s] - and discarding the first N-1 products. Adding e - this last multiplication - once, or twice or 3 times are all incorrect, though it may appear to work with certain inputs.

Your answer loop, with comments:

for(int r=0; r<a; r++)
{
    for(int s=0; s<d; s++)
    {
        for(int t=0; t<b; t++)
        {
            e = matrixA[r][t]*matrixB[t][s];
        }

        // now e only has the value from that final multiplication, of
        //    matrix[r][b - 1] * matrixB[b - 1][s]. All of the other
        //    products were lost.

        // so now it doesn't matter how many times you add e, you'll
        //    get the wrong product:
        matrixAns[r][s] = e+matrixAns[r][s];
        matrixAns[r][s] = e+matrixAns[r][s];
    }
}

Change your answer loop to:

for(int r=0; r<a; r++)
{
    for(int s=0; s<d; s++)
    {
        for(int t=0; t<b; t++)
        {
            e = matrixA[r][t]*matrixB[t][s];

            // accumulation of the products should be INSIDE the loop:
            matrixAns[r][s] = matrixAns[r][s] + e;
        }
    }
}



回答2:


Have you printed matrixAns[r][s] to make sure it holds something?

It sounds like matrixAns[r][s] is empty; this is what I think is happening:

matrixAns[r][s] = e+matrixAns[r][s]; //stores e in matrixAns[r][s]
matrixAns[r][s] = e+matrixAns[r][s]; //adds e to e (whats in matrixAns[r][s])


来源:https://stackoverflow.com/questions/12184233/why-did-i-need-to-use-this-statement-twice-matrix-multiplication

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!