code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
Terms of Agreement:
By using this code, you agree to the following terms...
1) You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for langauges that allow it) freely and with no charge.
2) You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.
3) You may link to this code from another website, but ONLY if it is not wrapped in a frame.
4) You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
#include "stdio.h"
#include "conio.h"
#include "stdlib.h"
#include "math.h"
void ReadInput(void);
void Display(void);
void Inverse(void);
float Factor(int ,int);
void Pivoting(int,int);
void Trunc(void);
void Diagonal(void);
float a[20][20]={0,0},num,factor,error;
int i,j,size,ii,jj,done=1;
void ReadInput(void)
{
FILE *f;
f=fopen("inverse.txt","r");
fscanf(f,"%d",&size);
fscanf(f,"%f",&error);
for(i=0;i { for(j=0;j fscanf(f,"%f",&a[i][j]); // scanning matrix from text file a[i][size+i]=1;// Adding identical matrix } } void Display() { for(i=0;i { printf("\n"); for(j=0;j printf("%0.3f\t",a[i][j]); } } float Factor(int row,int col) { return float (-1*(a[row][col]/a[col][col])); } void Pivoting(int row,int col) { float temp; int s,k; for(s=0;s { temp=a[row-1][s]; a[row-1][s]=a[row][s]; a[row][s]=temp; } } void Trunc(void) { int s,k; for(s=0;s for(k=0;k { if(fabsl(a[k][s]) } } void Inverse() { for(j=0;j { for(i=j+1;i { if(a[j][j]!=0) factor=Factor(i,j); else { Pivoting(i,j+1); factor=Factor(i,j); } for(ii=0;ii { a[i][ii]+=factor*a[j][ii]; } Trunc(); } } for(j=size-1;j>=0;j--) { for(i=j-1;i>=0;i--) { if(a[j][j]==0) done=0; else factor=Factor(i,j); for(ii=0;ii<=2*size;ii++) { a[i][ii]+=factor*a[j][ii]; } Trunc(); } } } void Diagonal(void) { int s,k; float diagonal; for(s=0;s<=size;s++) { diagonal=a[s][s]; for(k=0;k<=size*2;k++) a[s][k]/=diagonal; } } void Write() { FILE *s; int k,l; s=fopen("inverse2.txt","w"); for(k=0;k for(l=size;l<2*size;l++) fprintf(s,"%0.3f\t",a[k][l]); fprintf(s,"\n","c"); } } void main() { textbackground(0); textcolor(WHITE); clrscr(); printf("SERDAR ULUTAS CSE 19801006 \nMATRIX INVERSION PROGRAM"); ReadInput(); Display(); getch(); Inverse(); getch(); Diagonal(); Display(); Write(); getch(); getch(); }