0

私は行列の乗算を見つけるプログラムを開発していました。

#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;
    }    
}

そして重要なことの1つは、これは私の宿題や課題ではないということです。

私が使用した方法は完全に機能しますが、このステートメントを2回使用するまで、正しい答えは得られません。(試行錯誤で入手しました)。

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

またmatrixAns、ループを使用してを初期化しました(そしてそれを0に設定しました)。

私はC++を初めて使用し、どのようなエラーが発生したのか、2つのステートメントを使用するとどのように正しい答えが得られるのかを知りたいと思っています。

アプリを破壊せずにステートメントの1つを取り除く方法はありますか?

4

2 に答える 2

4

答えを計算するときに内積を正しく行っていません。2 つの行列の間で個々のセルを乗算し、答えの個々のセルのすべての積を合計する必要があります。

あなたのコードはe最後の乗算の積だけを取り、最初のN-1 個のmatrix[r][b - 1] * matrixB[b - 1][s]積を破棄しています。加算- この最後の乗算 - 1 回、2 回、または 3 回はすべて正しくありませんが、特定の入力では機能するように見える場合があります。e

コメント付きの回答ループ:

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];
    }
}

回答ループを次のように変更します。

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;
        }
    }
}
于 2012-08-29T18:42:49.807 に答える
0

何かを保持していることを確認するために、matrixAns[r][s] を印刷しましたか?

matrixAns[r][s] が空のようです。これは私が起こっていると思うことです:

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])
于 2012-08-29T18:35:56.783 に答える