dimanche 25 février 2018

What is the simplest way for me to expand this program to calculate the inverse of up to a 10x10 matrix?

The program I have below finds the inverse of only a 3x3 matrix, but I'm wondering if there is a simple way for me to expand it to find the inverse of a user entered sized matrix UP TO 10x10. Thanks

#include<iostream>
using namespace std;

int main()
{
    int mat[3][3], i, j;
    float determinant = 0;

    cout<<"Enter elements of the matrix:"<<endl;
    for(i = 0; i < 3; i++)
        for(j = 0; j < 3; j++)
           cin>>mat[i][j];


    //finding determinant
    for(i = 0; i < 3; i++)
        determinant = determinant + (mat[0][i] * (mat[1][(i+1)%3] * mat[2][(i+2)%3] - mat[1][(i+2)%3] * mat[2][(i+1)%3]));


    cout<<"\n\nInverse of matrix is: "<<endl;
    for(i = 0; i < 3; i++){
        for(j = 0; j < 3; j++)
            cout<<((mat[(j+1)%3][(i+1)%3] * mat[(j+2)%3][(i+2)%3]) - (mat[(j+1)%3][(i+2)%3] * mat[(j+2)%3][(i+1)%3]))/ determinant<<"\t";

        cout<<endl;
    }

    return 0;
}

Aucun commentaire:

Enregistrer un commentaire