samedi 30 mai 2020

need help in c++ matrix division [closed]

i write a code for multiplying matrix and division for 2 3x3 matrices the out put is a 3x3 matrix . the multiplication works well but division doesn't . i tried the inverse code it works alone . but when i put it in function to multiply it by mat1 result is wrong . any help please.i tried to do some more things on the code but it looks like i made a fatal error preventing it to get trure results

#include <iostream>

using namespace std;

int main()
{
    int mat1[3][3], mat2[3][3], x, j, k, sum, sumdiv;
    double mat3[3][3], mat4[3][3], mat5[3][3];
    cout << "Enter values for first 3 x 3 matrix:\n";
    for (x = 0; x <= 2; x++) {
        for (j = 0; j <= 2; j++) {
            cout << "Enter Value For item" << x << ',' << j << "\n";
            cin >> mat1[x][j];
        }
    }
    cout << "\n Enter values for second 3 x 3 matrix:\n";
    for (x = 0; x <= 2; x++) {
        for (j = 0; j <= 2; j++) {
            cout << "Enter Value For item" << x << ',' << j << "\n";
            cin >> mat2[x][j];
        }
    }
    cout << "\n The first 3 x 3 matrix entered by you is:\n";
    for (x = 0; x <= 2; x++) {
        for (j = 0; j <= 2; j++)
            cout << "\t" << mat1[x][j];
        cout << "\n";
    }
    cout << "\n the second 3 x 3 matrix entered :\n";
    for (x = 0; x <= 2; x++) {
        for (j = 0; j <= 2; j++)
            cout << "\t" << mat2[x][j];
        cout << "\n";
    }
    for (x = 0; x <= 2; x++) {
        for (j = 0; j <= 2; j++) {
            sum = 0;
            for (k = 0; k <= 2; k++)
                sum += mat1[x][k] * mat2[k][j];
            mat3[x][j] = sum;
        }
    }
    cout << "\nThe product of the above two matrices is:\n";
    for (x = 0; x <= 2; x++) {
        for (j = 0; j <= 2; j++)
            cout << "\t" << mat3[x][j];
        cout << "\n";
    }

    mat2[0][0] = mat1[1][1] * mat1[2][2] - mat1[1][2] * mat1[2][1];
    mat2[0][1] = -1 * (mat1[1][0] * mat1[2][2] - mat1[1][2] * mat1[2][0]);
    mat2[0][2] = mat1[1][0] * mat1[2][1] - mat1[1][1] * mat1[2][0];
    mat2[1][0] = -1 * (mat1[0][1] * mat1[2][2] - mat1[2][1] * mat1[0][2]);
    mat2[1][1] = mat1[0][0] * mat1[2][2] - mat1[0][2] * mat1[2][0];
    mat2[1][2] = -1 * (mat1[0][0] * mat1[2][1] - mat1[0][1] * mat1[2][0]);
    mat2[2][0] = mat1[0][1] * mat1[1][2] - mat1[0][2] * mat1[1][1];
    mat2[2][1] = -1 * (mat1[0][0] * mat1[1][2] - mat1[0][2] * mat1[1][0]);
    mat2[2][2] = mat1[0][0] * mat1[1][1] - mat1[0][1] * mat1[1][0];

    long int det = mat1[0][0] * mat2[0][0] + mat1[0][1] * mat2[0][1] + mat1[0][2] * mat2[0][2];

    for (int i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++)
            mat4[i][j] = mat2[j][i];
    }

    for (int i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++)
            mat4[i][j] = mat4[i][j] / det;
    }

    for (x = 0; x <= 2; x++) {
        for (j = 0; j <= 2; j++) {
            sumdiv = 0;
            for (k = 0; k <= 2; k++)
                sumdiv += mat1[x][k] * mat4[k][j];
            mat5[x][j] = sumdiv;
        }
    }

    cout << "\nThe division of the above two matrices is:\n";
    for (x = 0; x <= 2; x++) {
        for (j = 0; j <= 2; j++)
            cout << "\t" << mat5[x][j];
        cout << "\n";
    }

    return 0;
}

Aucun commentaire:

Enregistrer un commentaire