矩阵的行列式n x n

时间:2014-04-07 23:21:54

标签: c++ cycle

谁能告诉我我的错误?

决定因素不正确!

我在一张纸上做了一个测试,答案是正确的!

我相信也许我的错误就在那时,在行中“Matriz [i + 1] [j] =(Matriz [i + 1] [j] - (Matriz [fila] [j] *(Matriz [i + 1] [0] / Matriz [fila] [fila])));“但我的测试是正确的。

#include <iostream>
#include <stdio.h>

using namespace std;

class Matriznxm{

private:

    int n,m;
    float **Matriz;

public:


    Matriznxm(int f, int c){

        n = f;
        m = c;
        Matriz = new float *[n];

            for (int i=0; i<n; i++){

                Matriz[i]=new float [m];
            }

        for(int i=0; i<n; i++){

            for(int j=0; j<m; j++ ){

                Matriz[i][j]=0.0;

            }

        }
    }


    void llenarMatriz(){

        for(int i=0; i<n; i++){

            for(int j=0; j<m; j++){

                cout << "\nMatriz ["<<i+1<<"]["<<j+1<<"]: ";
                cin >> Matriz[i][j];

            }

        }

    }

    void mostrarMatriz(){

        for(int i=0; i<n; i++){


            for(int j=0; j<m; j++){


                cout << Matriz[i][j]<< "  ";

            }
            cout << "\n";
        }

    }


    int determinante(){

        float det = 1.0;
        int fila = 0;


        while(fila < n-1){

            for(int i=fila; i<n-1; i++){

                for(int j=fila; j<m; j++){


                    Matriz[i+1][j] = (Matriz[i+1][j] - (Matriz[fila][j]*(Matriz[i+1][0]/Matriz[fila][fila])));  

                }

            }


            fila++;

        }


        for (int i=0; i<n; i++){

            det= det * Matriz[i][i];

        }




        return det;
    }

};

int main(){

int n,m;

cout<<"\nNumero de Filas y Columnas: ";
cin >> n;
cout << "\n\n\n";
m=n;

Matriznxm m1(n,m);

m1.llenarMatriz();
m1.mostrarMatriz();

cout << "\nEl determinante es: "<< m1.determinante() <<"\n\n";
m1.mostrarMatriz();



return 1;

}

1 个答案:

答案 0 :(得分:0)

#include <iostream>
using namespace std;
int main()
{
    int siz;
    cout<< "enter the size of you matrix AxA\nA = ";
    cin >>siz;
    int mat[siz][siz],rez=0,rezA=1,rezB=1;
    for(int i=0;i<siz;i++)
        for(int j=0;j<siz;j++)
            cin >> mat[i][j];
    for(int t=0;t<siz;t++){
        for(int i=0;i<siz;i++)
        {
            rezA = rezA *mat[i][i+t>siz-1?(i+t-siz):i+t];
            rezB = rezB *mat[i][siz-t-1-i<0?(2*siz-t-1-i):(siz-t-1-i)];
        }
        rez  = rez + (rezA - rezB);
        rezA =rezB = 1;
    }
    cout <<endl<<"The Determinat is : "<<rez;
    return 0;
}