c - Unable to find determinant using partial pivoting -
whats problem. executes doesnt show correct answer. gets compiled well. i've used calling function in many places. assumed matrix square , give input througgh terminal. ex=3 random 3x3 matrix value seems incorrect
#include<stdio.h> #include<math.h> #include<stdlib.h> void row(int r1,int r2,int n,float a[n][n]){ int c; float temp[n][n]; for(c=0;c<n;c++){ temp[r1][c]=a[r1][c]; a[r1][c]=a[r2][c]; a[r2][c]=temp[r1][c]; } } void maximum(int i, int n,float a[n][n]){ int j; float max=fabs(a[i][i]); for(j=i+1;j<n;j++){ if(fabs(a[j][i])>max){ max=fabs(a[j][i]); row(i,j,n,a); } } } void op(int k, int n,float a[n][n]){ int i,j; float f;f for(i=1;i<n-1-k;i++){ f=-(a[k+i][k]/a[k][k]); for(j=0;j<n-1-k;j++){ a[k+i][k+j]=a[k+i][k+j]+f*(a[k][k+j]); } } } int main(){ int i,j,n; printf("enter order of matrix:"); scanf("%d",&n); float a[n][n]; for(i=0;i<n;i++){ for(j=0;j<n;j++) scanf("%f",&a[i][j]); } float det=1; for(i=0;i<n-1;i++){ maximum(i,n,a); op(i,n,a); det*=a[i][i]; } det*=a[n-1][n-1]; printf("%f\n",det); return 0; }
you reading uninitialized values
for(j=i+1;j<n;j++){ if(fabs(a[j][i])>max){ max=fabs(a[j][i]); row(i,j,n,a); } }
should be
for(j=i+1;j<n;j++){ if(fabs(a[i][j])>max){ max=fabs(a[i][j]); row(i,j,n,a); } }
Comments
Post a Comment