**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 determinantfor(i = 0; i < n; i++)det *= matrix[i][i];printf("The determinant of matrix is: %.2f\n\n", det);return 0;}

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

ReplyDeleteThis code fails in this case, you must perform partial or complete pivoting

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

DeleteAbove source code is working or not?

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

ReplyDeleteRatio 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.

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

Deletematrix[i][j] = matrix[i][j] - matrix[k][j]*ratio

//this reduces rows using the previous row, until matrix is diagonal.

exactly

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

ReplyDelete#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: %.2f\n\n", matrix[n][n]);

return 0;

}

Or you could optimize the algorithm and get this:

Delete#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: %.2f\n\n", det);

return 0;

}

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

Deletethis programme has some error in upper triangulr

ReplyDeletenot working for 0 matrix...

ReplyDelete