Question ( 1 ) = Write a computer program in C++ to find solution of the following systems of equations using Gauss
Elimination Method.
1. 2x+y+z=10,
3x+2y+3z=18,
X+4y+9z=16
(Answer: x=7, y = -9, z=5)
Ans :
/*
Program: Gauss Elimination Method
All array indexes are assumed to start from 1
*/
#include<iostream>
#include<iomanip>
#include<math.h>
#include<stdlib.h>
#define SIZE 10
using namespace std;
int main()
{
float a[SIZE][SIZE], x[SIZE], ratio;
int i,j,k,n;
/* Setting precision and writing floating point values in fixed-point notation. */
cout<< setprecision(3)<< fixed;
/* Inputs */
/* 1. Reading number of unknowns */
cout<<"Deepak Shriwastav ,Regestration no:";cin>>n;
/* 1. Reading number of unknowns */
cout<<"Enter number of unknowns: ";
cin>>n;
/* 2. Reading Augmented Matrix */
cout<<"Enter Coefficients of Augmented Matrix: "<< endl;
for(i=1;i<=n;i++)
{
for(j=1;j<=n+1;j++)
{
cout<<"a["<< i<<"]"<< j<<"]= ";
cin>>a[i][j];
}
}
/* Applying Gauss Elimination */
for(i=1;i<=n-1;i++)
{
if(a[i][i] == 0.0)
{
cout<<"Mathematical Error!";
exit(0);
}
for(j=i+1;j<=n;j++)
{
ratio = a[j][i]/a[i][i];
for(k=1;k<=n+1;k++)
{
a[j][k] = a[j][k] - ratio*a[i][k];
}
}
}
/* Obtaining Solution by Back Substitution Method */
x[n] = a[n][n+1]/a[n][n];
for(i=n-1;i>=1;i--)
{
x[i] = a[i][n+1];
for(j=i+1;j<=n;j++)
{
x[i] = x[i] - a[i][j]*x[j];
}
x[i] = x[i]/a[i][i];
}
/* Displaying Solution */
cout<< endl<<"Solution: "<< endl;
for(i=1;i<=n;i++)
{
cout<<"x["<< i<<"] = "<< x[i]<< endl;
}
return(0);
}
Question ( 2 ) : Write a computer program in C++ to find solution of the following systems of equations using Gauss
Elimination Method
2x+2y+z=12
3x+2y+2z=8,
5x+10y-8z=10.
(Answer: x= -12.75, y=14.375, z=8.75)
Ans :
/*
Program: Gauss Elimination Method
All array indexes are assumed to start from 1
*/
#include<iostream>
#include<iomanip>
#include<math.h>
#include<stdlib.h>
#define SIZE 10
using namespace std;
int main()
{
float a[SIZE][SIZE], x[SIZE], ratio;
int i,j,k,n;
/* Setting precision and writing floating point values in fixed-point notation. */
cout<< setprecision(3)<< fixed;
/* Inputs */
/* 1. Reading number of unknowns */
cout<<"Deepak Shriwastav ,Regestration no:";cin>>n;
/* 1. Reading number of unknowns */
cout<<"Enter number of unknowns: ";
cin>>n;
/* 2. Reading Augmented Matrix */
cout<<"Enter Coefficients of Augmented Matrix: "<< endl;
for(i=1;i<=n;i++)
{
for(j=1;j<=n+1;j++)
{
cout<<"a["<< i<<"]"<< j<<"]= ";
cin>>a[i][j];
}
}
/* Applying Gauss Elimination */
for(i=1;i<=n-1;i++)
{
if(a[i][i] == 0.0)
{
cout<<"Mathematical Error!";
exit(0);
}
for(j=i+1;j<=n;j++)
{
ratio = a[j][i]/a[i][i];
for(k=1;k<=n+1;k++)
{
a[j][k] = a[j][k] - ratio*a[i][k];
}
}
}
/* Obtaining Solution by Back Substitution Method */
x[n] = a[n][n+1]/a[n][n];
for(i=n-1;i>=1;i--)
{
x[i] = a[i][n+1];
for(j=i+1;j<=n;j++)
{
x[i] = x[i] - a[i][j]*x[j];
}
x[i] = x[i]/a[i][i];
}
/* Displaying Solution */
cout<< endl<<"Solution: "<< endl;
for(i=1;i<=n;i++)
{
cout<<"x["<< i<<"] = "<< x[i]<< endl;
}
return(0);
}
Thank you for visiting
0 Comments
Please do not enter any spam link in the comment box.