write a program to find solution of linear equation using Gauss Elimination method in c++.
write a program to find solution of linear equation using Gauss Elimination method in c++.
Source Code:
/*******************************************
language: C
Author : Tanveer Khan
Rajasthan University, Jaipur
************************************************/
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[3][4],i,j,k,x,y,q,z;
float x1,x2,x3;
cout<<"enter vaues of constant for 3 eq \n a1x1+a2x2+a3x3 = d1 \n b1x1+b2x2+b3x3 = d2 \n c1x1+c2x2+c3x3 = d3 \n";
for(i=o;i<3;i++)
{
for(j=0;j<4;j++)
{
cin>>a[i][j];
}
}
cout<<"equations are \n";
cout<<endl<<a[0][0]<<"x1"<<a[0][1]<<"x2+"<<a[0][2]<<"x3="<<a[0][3];
cout<<endl<<a[1][0]<<"x1"<<a[1][1]<<"x2+"<<a[1][2]<<"x3="<<a[1][3];
cout<<endl<<a[2][0]<<"x1"<<a[2][1]<<"x2+"<<a[2][2]<<"x3="<<a[2][3];
cout<<"\n matrix \n";
for(i=o;i<3;i++)
{
cout<<endl;
for(j=0;j<4;j++)
{
cout<<a[i][j]<<"\t";
}
}
x = a[0][0];
y = a[1][0];
for(j=0;j<4;j++)
{
a[0][j] = a[0][j]*y;
a[1][j] = a[i][j]*x;
a[0][j] = a[0][j]-a[1][j];
}
q = a[1][0];
z = a[2][0];
for(j=0;j<4;j++)
{
a[1][j] = a[1][j]*z;
a[2][j] = a[2][j]*q;
a[1][j] = a[1][j]-a[2][j];
}
for(j=0;j<4;j++)
{
a[0][j] = a[0][j]*y;
a[1][j] = a[i][j]*x;
a[0][j] = a[0][j]-a[1][j];
}
x3=a[0][3]/a[0][2];
x2=(a[1][3]-a[1][2]*x3)/a[1][1];
x1=(a[2][3]-a[2][2]*x3-a[2][1]*x2)/a[2][0];
cout<<"\nx1="<<x1<<"\nx2="<<x2<<"\nx3="<<x3;
getch();
}
OUTPUT:
write a program to find solution of linear equation using Gauss Elimination method in c++.
Source Code:
/*******************************************
language: CAuthor : Tanveer KhanRajasthan University, Jaipur************************************************/#include<iostream.h> #include<conio.h> void main() { clrscr(); int a[3][4],i,j,k,x,y,q,z; float x1,x2,x3; cout<<"enter vaues of constant for 3 eq \n a1x1+a2x2+a3x3 = d1 \n b1x1+b2x2+b3x3 = d2 \n c1x1+c2x2+c3x3 = d3 \n"; for(i=o;i<3;i++) { for(j=0;j<4;j++) { cin>>a[i][j]; } } cout<<"equations are \n"; cout<<endl<<a[0][0]<<"x1"<<a[0][1]<<"x2+"<<a[0][2]<<"x3="<<a[0][3]; cout<<endl<<a[1][0]<<"x1"<<a[1][1]<<"x2+"<<a[1][2]<<"x3="<<a[1][3]; cout<<endl<<a[2][0]<<"x1"<<a[2][1]<<"x2+"<<a[2][2]<<"x3="<<a[2][3]; cout<<"\n matrix \n"; for(i=o;i<3;i++) { cout<<endl; for(j=0;j<4;j++) { cout<<a[i][j]<<"\t"; } } x = a[0][0]; y = a[1][0]; for(j=0;j<4;j++) { a[0][j] = a[0][j]*y; a[1][j] = a[i][j]*x; a[0][j] = a[0][j]-a[1][j]; } q = a[1][0]; z = a[2][0]; for(j=0;j<4;j++) { a[1][j] = a[1][j]*z; a[2][j] = a[2][j]*q; a[1][j] = a[1][j]-a[2][j]; } for(j=0;j<4;j++) { a[0][j] = a[0][j]*y; a[1][j] = a[i][j]*x; a[0][j] = a[0][j]-a[1][j]; } x3=a[0][3]/a[0][2]; x2=(a[1][3]-a[1][2]*x3)/a[1][1]; x1=(a[2][3]-a[2][2]*x3-a[2][1]*x2)/a[2][0]; cout<<"\nx1="<<x1<<"\nx2="<<x2<<"\nx3="<<x3; getch(); }
OUTPUT:
Comments
Post a Comment