Note.....

Dear readers I Have collected these Programs from various sources of internet...I don't know what the header file is Please mail me if any error found hi2rashid@yahoo.co.in

Sponsored....

Tuesday, April 13, 2010

C++ Program to solve linear equation (3 variable) by gauss elimination method

//program to solve linear equation (3 variable) by gauss elimination method
#include
#include
#include
#include
#include
#define r 3
#define c 4
void main()
{
clrscr();
float a[r][c];
int i,j;
textcolor(GREEN);
cprintf("PROGRAM TO SOLVE LINEAR EQUATIONS BY GAUSS ELIMINATION
METHOD");
cout<<"\n";
textcolor(BLUE);
cprintf("enter the values in given format");
cout<<"\n";
cprintf("a[0][0] x + a[0][1] y + a[0][2] z = a[0][3]");
cout<<"\n";
for( i=0;i
{
for( j=0;j
{
cout<<"enter a["<<<"]["<<<"] : ";
cin>>a[i][j];
cout<<"\n";
}
}
delay(10);
step:
clrscr();
cout<<"the values entered are :\n";
for( i=0;i
{
for(j=0;j
{
cout<
cout<<"\t";
}
cout<<"\n";
}

float temp1=a[0][0];
for( j=0;j
{

a[0][j]=a[0][j]/temp1;
}
cout<<"the values after step 1 :\n";
cprintf("on dividing R1 by ");cout<<<"\n";
for( i=0;i
{
for(int j=0;j
{
cout<
cout<<"\t";
}
cout<<"\n";
}

float temp2=a[1][0];
for( j=0;j
{a[1][j]=a[1][j]-temp2*a[0][j]; }
cout<<"the values after step 2 :\n";
cprintf("on R2 - ");cout<<<"*R1\n";
for( i=0;i
{
for(int j=0;j
{
cout<
cout<<"\t";
}
cout<<"\n";
}

float temp3=a[2][0];
for( j=0;j
{a[2][j]=a[2][j]-temp3*a[0][j];}
cout<<"the values after step 3 :\n";
textcolor(GREEN);
cprintf("on R3 - ");cout<<<"*R1\n";
for( i=0;i
{
for(int j=0;j
{
cout<
cout<<"\t";
}
cout<<"\n";
}

float temp4=a[2][1];
for(j=0;j
a[2][j]=a[2][j]-temp4/a[1][1]*a[1][j];
cout<<"the values after step 4 :\n";
cprintf("on R3 - ");cout<<<"*R2\n";
for( i=0;i
{
for(int j=0;j
{
cout<
cout<<"\t";
}
cout<<"\n";
}
cout<<"Please wait while system calculates the result..............\n";
float z=a[r-1][c-1]/a[r-1][c-2];
float y=(a[r-2][c-1]-a[r-2][c-2]*z )/a[r-2][c-3];
float x=(a[r-3][c-1]-a[r-3][c-2]*z-a[r-3][c-3]*y)/a[r-3][c-4];
delay(10000);
clrscr();
textcolor(GREEN);
cprintf("SOLUTION OF GIVEN SYSTEM OF EQUATIONS");
cout<<"\n";
cout<<"x = "<
cout<<"\ny = "<
cout<<"\nz = "<
float ch;
cout<<"\n";
cprintf("Do you want to view steps again");cout<<"\n";
cprintf("press '1' else Press '2'");cout<<" ";cin>>ch;
if(ch==1)
goto step;
else
exit(0);
getch();
}

output:

enter the values in given format
a[0][0] x + a[0][1] y + a[0][2] z = a[0][3]
enter a[0][0] : 2

enter a[0][1] : 1

enter a[0][2] : 1

enter a[0][3] : 10

enter a[1][0] : 3

enter a[1][1] : 2

enter a[1][2] : 3

enter a[1][3] : 18

enter a[2][0] : 1

enter a[2][1] : 4

enter a[2][2] : 9

enter a[2][3] : 16

2 1 1 10
3 2 3 18
1 4 9 16
the values after step 1 :
on dividing R1 by 1
1 0.5 0.5 5
3 2 3 18
1 4 9 16
the values after step 2 :
on R2 - 3*R1
1 0.5 0.5 5
0 0.5 1.5 3
1 4 9 16
the values after step 3 :
on R3 - 1*R1
1 0.5 0.5 5
0 0.5 1.5 3
0 3.5 8.5 11
the values after step 4 :
on R3 - 7*R2
1 0.5 0.5 5
0 0.5 1.5 3
0 0 -2 -10
Please wait while system calculates the result..............

SOLUTION OF GIVEN SYSTEM OF EQUATIONS
x = 7
y = -9
z = 5
Do you want to view steps again
press '1' else Press '2'

No comments:

Post a Comment