Here is a C++ program to solve simultaneous linear equation. First program solve simultaneous linear equation in two variable and second program solve simultaneous linear equation in three variable.
Note : to solve simultaneous linear equation in three variable I have applied Gauss-Jordan method.
C++ program to solve simultaneous linear equation in two variable
#include<iostream>
using namespace std;
int main()
{
float x,y,a[10][10];
cout<<"Enter coefficients of variables in matrix form: "<<endl;
for(int i=0;i<2;i++)
for(int j=0;j<3;j++)
cin>>a[i][j];
for(int i=0;i<2;i++)
cout<<a[i][0]<<"x + "<<a[i][1]<<"y = "<<a[i][2]<<endl;
for(int i=1,k=0;i>=0;i--,k++)
for(int j=2;j>=0;j--)
a[i][j]=a[i][j]-a[k][j]*(a[i][0]/a[k][k]);
x=a[0][2]/a[0][0];
y=a[1][2]/a[1][1];
cout<<"x = "<<x;
cout<<endl<<"y = "<<y;
return 0;
}
using namespace std;
int main()
{
float x,y,a[10][10];
cout<<"Enter coefficients of variables in matrix form: "<<endl;
for(int i=0;i<2;i++)
for(int j=0;j<3;j++)
cin>>a[i][j];
for(int i=0;i<2;i++)
cout<<a[i][0]<<"x + "<<a[i][1]<<"y = "<<a[i][2]<<endl;
for(int i=1,k=0;i>=0;i--,k++)
for(int j=2;j>=0;j--)
a[i][j]=a[i][j]-a[k][j]*(a[i][0]/a[k][k]);
x=a[0][2]/a[0][0];
y=a[1][2]/a[1][1];
cout<<"x = "<<x;
cout<<endl<<"y = "<<y;
return 0;
}
OUTPUT
C++ program to solve simultaneous linear equation in two variable
#include<iostream>
using namespace std;
int main()
{
float sol[3],a[10][10];
cout<<"Enter coefficients of variables in matrix form: "<<endl;
for(int i=0;i<3;i++)
for(int j=0;j<4;j++)
cin>>a[i][j];
cout<<endl<<endl;
for(int i=0;i<3;i++)
cout<<a[i][0]<<"x + "<<a[i][1]<<"y + "<<a[i][2]<<"z = "<<a[i][3]<<"\n";
for(int i=1;i<3;i++)
for(int j=3;j>=0;j--)
a[i][j]=a[i][j]-a[0][j]*(a[i][0]/a[0][0]);
for(int i=0;i<3;i+=2)
for(int j=3;j>=0;j--)
a[i][j]=a[i][j]-a[1][j]*(a[i][1]/a[1][1]);
for(int i=0;i<2;i++)
for(int j=3;j>=0;j--)
a[i][j]=a[i][j]-a[2][j]*(a[i][2]/a[2][2]);
for(int i=0;i<3;i++)
sol[i]=a[i][3]/a[i][i];
cout<<"\n\nx = "<<sol[0];
cout<<endl<<"y = "<<sol[1];
cout<<endl<<"z = "<<sol[2]<<endl;
return 0;
}
using namespace std;
int main()
{
float sol[3],a[10][10];
cout<<"Enter coefficients of variables in matrix form: "<<endl;
for(int i=0;i<3;i++)
for(int j=0;j<4;j++)
cin>>a[i][j];
cout<<endl<<endl;
for(int i=0;i<3;i++)
cout<<a[i][0]<<"x + "<<a[i][1]<<"y + "<<a[i][2]<<"z = "<<a[i][3]<<"\n";
for(int i=1;i<3;i++)
for(int j=3;j>=0;j--)
a[i][j]=a[i][j]-a[0][j]*(a[i][0]/a[0][0]);
for(int i=0;i<3;i+=2)
for(int j=3;j>=0;j--)
a[i][j]=a[i][j]-a[1][j]*(a[i][1]/a[1][1]);
for(int i=0;i<2;i++)
for(int j=3;j>=0;j--)
a[i][j]=a[i][j]-a[2][j]*(a[i][2]/a[2][2]);
for(int i=0;i<3;i++)
sol[i]=a[i][3]/a[i][i];
cout<<"\n\nx = "<<sol[0];
cout<<endl<<"y = "<<sol[1];
cout<<endl<<"z = "<<sol[2]<<endl;
return 0;
}
Please can you explain because I don't understand anything
ReplyDeleteoof
ReplyDeletecan a program be made by this same method to solve (n) linear equations in ( n) variables?
ReplyDeletefuck my life
ReplyDeletecan feel you
Delete