Неправильные значения умножения матриц

0

Я пишу программу, которая умножает две матрицы вместе. Проблема, с которой я сталкиваюсь, заключается в том, что она вычисляет неверные значения для окончательного ответа. Я некоторое время перепутал часть умножения, но ничего не придумал. Любая помощь будет оценена по достоинству.

Вот код:

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;
}
Теги:
matrix
matrix-multiplication

2 ответа

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

Вы не инициализируете значения в матрице o, поэтому его значения не определены. Но вы все равно используете эти неопределенные (и случайные) значения в своих расчетах. Вычисление с неопределенными значениями приведет к неопределенным значениям.

Ещё вопросы

Сообщество Overcoder
Наверх
Меню