Numerical Methods: Determinant of nxn matrix using C

Source Code:

 

SHARE Numerical Methods: Determinant of nxn matrix using C

You may also like...

15 Responses

  1. if diagonal i.e matrix[1][1]=0 then ratio will be infinity ?

  2. This code fails in this case, you must perform partial or complete pivoting

  3. Unknown says:

    can you explain that 'raio' is what to do?

  4. Ratio actually prevents the division by zero error. If you put matrix[j][i]/matrix[i][i] instead of ratio inside third nested for loop, it throws division by zero run time error.

  5. Anonymous says:

    This is how you reduce the matrix to an upper triangular, therefore the determinant is just the multiplication of diagonal elements.

    matrix[i][j] = matrix[i][j] – matrix[k][j]*ratio
    //this reduces rows using the previous row, until matrix is diagonal.

  6. Anonymous says:

    You can always check matrix[1][1]==0, if so, add a whole column to matrix[i][1].

  7. Anonymous says:

    /* u can simplify ur alghorithme by the following code*/

    #include

    int main(){

    float matrix[10][10];

    int i, j, k, n;

    printf("Enter order of matrix: ");

    scanf("%d", &n);

    printf("Enter the matrix: n");

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

    for(j = 0; j < n; j++){

    scanf("%f", &matrix[i][j]);

    }

    }

    /* Calculate directly the determinante */

    for(k = 1; k < n; k++)

    for(i = k+1; i <= n; i++)

    for(j = k+1; j <= n; j++){

    matrix[i][j] = matrix[i][j]matrix[k][k]-matrix[i][k]matrix[k][j];
    if(k>=2)
    matrix[i][j]=matrix[i][j]/matrix[k][k];

    }
    printf("The determinant of matrix is: %.2fnn", matrix[n][n]);

    return 0;

    }

  8. Anonymous says:

    Or you could optimize the algorithm and get this:

    #include

    int main(){
    int i, j, n;
    printf("Enter order of matrix: ");
    scanf("%d", &n);
    float*m=(float*)malloc(n*n*sizeof(float));
    printf("Enter the matrix: n");
    for(i = 0; i < n; i++){
    for(j = 0; j < n; j++){
    scanf("%f", &m[i*n+j]);
    }
    }
    float ary[n-1];
    float* subc=&ary[-1];
    float det=m[0];
    for(i=1;i<n;++i){
    subc[i]=m[i]/det;
    }
    for(i=1;i<n-1;++i){
    float dc=m[i*(n+1)]-subc[i];
    for(j=i+1;j<n;++j){
    subc[j]+=(m[j*n+i]-subc[j])/rc;
    }
    det*=dc;
    }
    det*=(m[i*(n+1)]-subc[i]);
    free(m);
    printf("The determinant of matrix is: %.2fnn", det);
    return 0;
    }

  9. this programme has some error in upper triangulr

  10. Anonymous says:

    Above source code is working or not?

  11. Sree Ram says:

    not working for 0 matrix…

  12. debayan says:

    What is rc in the line "subc[j]+=(m[j*n+i]-subc[j])/rc;?"

  13. You just need to declare rc at the main

  14. Anonymous says:

    Similar program, but can apply for degenerate matrix:
    // Gauss-Jordan elimination with full pivoting.
    // Computing determinants of square matrices
    //
    // Running time: O(n^3)
    // INPUT: a[][] = an nxn matrix
    // OUTPUT: determinant of a[][]

    #include
    #include
    #include

    using namespace std;

    const double EPS = 1e-16;
    double a[100][100];

    double DetGJ(int n, double a[100][100]) {
    int i, j, jmax;
    double det=1.0, s;
    for (int i=0;i> n;
    cout << "n = " << n << "n" << "MATRIX A IS n";
    for (int i=0; i> a[i][j];
    cout << a[i][j] << " ";
    }
    cout << "n";
    }

    double det = DetGJ(n, a);
    cout << "Determinant: " << det << endl;
    return 0;
    }

Leave a Reply

Your email address will not be published. Required fields are marked *

Share