Can you change(write) this code a little differrent? and this is the question but pls change this code Write a computer program for Gauss elimination method using C programming language. Decide the number of significant figures yourselves. While writing your program, consider the effects of the number of significant figures, pivoting, scaling and do not forget to check if the system is ill conditioned. #include #include
Hi, Can you change(write) this code a little differrent? and this is the question but pls change this code
Write a computer program for Gauss elimination method using C
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int n,i,j,k;// initializing variables
cout<<"\nEnter the no. of equations\n";
cin>>n; //input the no. of equations
float matrix[n][n+1],x[n]; //declare an array to store the elements of augmented-matrix
cout<<"\nEnter the elements of the augmented-matrix row-wise:\n";
for (i=0;i<n;i++)// taking the input from the user
for (j=0;j<=n;j++)
cin>>matrix[i][j];//input the elements of array
for (i=0;i<n;i++) //Pivotisation for the given matrix
for (k=i+1;k<n;k++)
if (abs(matrix[i][i])<abs(matirx[k][i]))// condition for checking Pivotisation
for (j=0;j<=n;j++)
{
double temp=matrix[i][j];// performing swap
matrix[i][j]=matrix[k][j];
matrix[k][j]=temp;
}
cout<<"\nThe matrix after Pivotisation is:\n";
for (i=0;i<n;i++) //print the new matrix
{
for (j=0;j<=n;j++)
cout<<matrix[i][j]<<setw(16);
cout<<"\n";
}
for (i=0;i<n-1;i++) //loop to perform the gauss elimination
for (k=i+1;k<n;k++)
{
double t=matrix[k][i]/matrix[i][i];
for (j=0;j<=n;j++)
matrix[k][j]=matrix[k][j]-t*matrix[i][j]; //make the elements below the pivot elements equal to zero or elimnate the variables
}
cout<<"\n\nThe matrix after gauss-elimination is as follows:\n";
for (i=0;i<n;i++) //print the new matrix
{
for (j=0;j<=n;j++)
cout<<matrix[i][j]<<setw(16);
cout<<"\n";
}
for (i=n-1;i>=0;i--)//back-substitution
{ //x is an array whose values correspond to the values of x,y,z..
x[i]=matrix[i][n]; //make the variable to be calculated equal to the rhs of the last equation
for (j=i+1;j<n;j++)
if (j!=i) //then subtract all the lhs values except the coefficient of the variable whose value is being calculated
x[i]=x[i]-matrix[i][j]*x[j];
x[i]=x[i]/matrix[i][i]; //now finally divide the rhs by the coefficient of the variable to be calculated
}
cout<<"\nThe values of the variables are \n";
for (i=0;i<n;i++)
cout<<x[i]<<endl;// Print the values of x, y,z
return 0;
}
Step by step
Solved in 2 steps