矩阵乘法值不正确

时间:2013-10-05 19:36:22

标签: c++ matrix matrix-multiplication

我正在编写一个将两个矩阵相乘的程序。我遇到的问题是它正在计算最终答案的错误值。我已经把乘法部分弄乱了一段时间,但却什么都没有。任何帮助将不胜感激。

以下是代码:

typedef int* IntArrayPtr;

int main(int argc, char *argv[])
{
    int d1, d2; //rows and columns for first matrix

    cout << "Enter the row and column dimensions of the first array:\n";
    cin >> d1 >> d2;

    //creates the first matrix
    IntArrayPtr *m = new IntArrayPtr[d1];
    int i, j;
    for (i = 0; i < d1; i++)
    m[i] = new int[d2];
    //m is now a d1 by d2 array.


    //takes in the values of the first matrix entered by the user
    cout << "Enter " << d1 << " rows of " << d2 << " integers each:\n";
    for (i = 0; i < d1; i++)
        for (j = 0; j < d2; j++)
            cin >> m[i][j];

    //displays first matrix
    cout << "Here is two-dimensional array 1:\n";
    for (i = 0; i < d1; i++)
    {
        for (j = 0; j < d2; j++)
            cout << m[i][j] << " ";
        cout << endl;
    }

    int s1, s2; //rows and columns for second matrix

    cout << "Enter the row and column dimensions of the second array:\n";
    cin >> s1 >> s2;

    //creates 2nd array
    IntArrayPtr *n = new IntArrayPtr[s1];
    int k, l;
    for (k = 0; k < s1; k++)
    n[k] = new int[s2];
    //m is now a s1 by s2 array.

    //takes in the values of the second matrix entered by the user
    cout << "Enter " << s1 << " rows of " << s2 << " integers each:\n";
    for (k = 0; k < s1; k++)
        for (l = 0; l < s2; l++)
            cin >> n[k][l];

    //displays 2nd matrix
    cout << "Here is two-dimensional array 2:\n";
    for (k = 0; k < s1; k++)
    {
        for (l = 0; l < s2; l++)
            cout << n[k][l] << " ";
        cout << endl;
    }

    //checks if matrices can be multiplied. If they can, the multiplication is done in the first stage of the if statement.
    if(i == l)
    {
         cout << "Matrices are correct size and are able to be multiplied.\n";

    //creates the product matrix
    IntArrayPtr *o = new IntArrayPtr[d1];
    for (i = 0; i < d1; i++)
    o[i] = new int[s2];
    //o is now a s1 by s2 array.

    int x;
     // multiplies the two matrices together
     for(i = 0; i < d1; i++)
     {
          for(j = 0; j < s2; j++)
          {
          for(x = 0; x < d1; x++)
          {
               o[i][j] = o[i][j] + m[i][x] * n[x][j];
          }
          }
     }


    //displays the product matrix
    cout << "Here is two-dimensional array 3:\n";
    for (i = 0; i < d1; i++)
    {
        for (l = 0; l < s2; l++)
            cout << o[i][l] << " ";
        cout << endl;
    }

    }
    else
    {
        cout << "Matricies are not the correct size and cannot be multiplied.\n";
    }

    system("PAUSE");
    return EXIT_SUCCESS;
}

2 个答案:

答案 0 :(得分:1)

您没有初始化o矩阵中的值,因此其值未定义。但是您仍然在计算中使用这些未定义(和随机)值。使用未定义的值进行计算将导致未定义的值。

答案 1 :(得分:0)

使用:

o[i][j] =0; //Initialize it

 for(i = 0; i < d1; i++)
     {   
          for(j = 0; j < s2; j++)
          {  o[i][j]  =0; //Here
          for(x = 0; x < d1; x++)
          {
               o[i][j] = o[i][j] + m[i][x] * n[x][j];
          }
          }
     }