Gauss Elimination Using C/C++

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
float a[3],b[3],c[3],d[3],x,y,z;
int i;
clrscr();
cout<<"Equation is of type ax+by+cz=d\n";
cout<<"Enter the value of a,b,c and d";
cout<<endl<<"a\tb\tc\td";
cout<<endl;
for(i=1;i<=3;i++)
{cin>>a[i];
cin>>b[i];
cin>>c[i];
cin>>d[i];
}
cout<<"\t\t Upper Triangular Matrix\n";
d[2]=d[2]-((d[1]*a[2])/a[1]);
b[2]=b[2]-((b[1]*a[2])/a[1]);
c[2]=c[2]-((c[1]*a[2])/a[1]);
a[2]=a[2]-((a[1]*a[2])/a[1]);
d[3]=d[3]-((d[1]*a[3])/a[1]);
b[3]=b[3]-((b[1]*a[3])/a[1]);
c[3]=c[3]-((c[1]*a[3])/a[1]);
a[3]=a[3]-((a[1]*a[3])/a[1]);
cout<<"Step 1\n";
for(i=1;i<=3;i++)
{cout<<a[i]<<"   "<<b[i]<<"   "<<c[i]<<"        "<<d[i]<<"\n";
}
cout<<"Step 2\n";
d[3]=d[3]-((d[2]*b[3])/b[2]);
c[3]=c[3]-((c[2]*b[3])/b[2]);
b[3]=b[3]-((b[2]*b[3])/b[2]);
for(i=1;i<=3;i++)
{cout<<a[i]<<"   "<<b[i]<<"   "<<c[i]<<"        "<<d[i]<<endl;
}
z=d[3]/c[3];
y=(d[2]-(c[2]*z))/b[2];
x=(d[1]-(c[1]*z)-(b[1]*y))/a[1];
cout<<"Value of:\nx="<<x<<"\ny="<<y<<"\nz="<<z;
getche();
}

posted under |

0 comments:

Post a Comment

Newer Post Older Post Home

About

Cool na? :) Now Learn How to create one by Clicking here


Followers

About This Blog