Wednesday 13 February 2013

Numerical Methods: Determinant of nxn matrix using C

Source Code:
#include<stdio.h>
int main(){
    float  matrix[10][10], ratio, det;
    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]);
        }
    }
    /* Conversion of matrix to upper triangular */
    for(i = 0; i < n; i++){
        for(j = 0; j < n; j++){
            if(j>i){
                ratio = matrix[j][i]/matrix[i][i];
                for(k = 0; k < n; k++){
                    matrix[j][k] -= ratio * matrix[i][k];
                }
            }
        }
    }
    det = 1; //storage for determinant
    for(i = 0; i < n; i++)
        det *= matrix[i][i];
    printf("The determinant of matrix is: %.2f\n\n", det);
    return 0;
}

5 comments:

  1. Thanks, it helped me a lot. Try this as well, ANOTHER METHOD

    ReplyDelete
  2. This is not a good method, for example
    | 0 1 1 |
    | 1 0 1 | = 2
    | 1 1 0 |
    but this algorithm cant calculate/gives a wrong answer...

    ReplyDelete
  3. can u give the paralel version?

    ReplyDelete

Comment