Introduction
Code for solving system of equation by Gaussian elimination method. The Code is well commented and would not need any further explanation
The code
Code: Cpp
#include <stdio.h>
#include <conio.h>
#define N 100
/****************************************************************/
/***** Gauss elimination method to solve system of equation *****/
/****************************************************************/
void main()
{
float coeff[N][N+1]={{4,2,1,11},{2,3,4,20},{3,5,3,22},};
/* Co-efficient inputing variables */
int i,j,k; /* Loop variables */
int n=3; /* Number of equations */
float pivot; /* pivoting variables */
float sum; /* Back substitution sum storing variable */
float x[N]; /* values of the variables i.e. x's */
char ch; /* choice inputing variable */
do
{
printf("\nEnter the number of variables\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<n+1;j++)
{
printf("\nEnter %d row %d col element\n",i+1,j+1);
scanf("%f",&coeff[i][j]);
}
}
for(k=0;k<n;k++)
{
for(i=k+1;i<n;i++)
{
pivot=coeff[i][k]/coeff[k][k];
for(j=k;j<n+1;j++)
coeff[i][j]=coeff[i][j]-pivot*coeff[k][j];
}
}
x[n-1]=coeff[n-1][n]/coeff[n-1][n-1];
for(i=n-2;i>=0;i--)
{
sum=0;
for(j=i;j<n+1;j++)
sum=sum+coeff[i][j]*x[j];
x[i]=(coeff[i][n]-sum)/coeff[i][i];
}
for(i=0;i<n;i++)
printf("\nx%d = %g\n",i+1,x[i]);
printf("\nDo you wish to continue[y/n]\n");
fflush(stdin);
scanf("%c",&ch);
}while(ch=='y' || ch=='Y');
getch();
}



+1 JamC.
