You are on page 1of 2

H1.

2 Computer Programming
Write a computer program (in FORTRAN or C++) to solve a system of linear equations by Gauss
Elimination Procedure. Test your program on sample problems of a set of linear equations with known
solutions. Submit the computer program listing and the sample problem solutions.

Solution:

#include "stdafx.h"
using namespace std;
#include <iostream>
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include <stdlib.h>
int _tmain(int argc, _TCHAR argv[])
{
int n,i,j,k,temp;
float a[10][10],c,d[10]={0};
system("cls");
cout<<"No of equation ? ";
cin>>n;
cout<<"Coefficient of all : \n";
for(i=0;i<n;i++)
{
cout<<"equation: "<<i+1<< " ";
for(j=0;j<=n;j++)
cin>>a[i][j];
}
for(i=n-1;i>0;i--) // pivoting
{
if(a[i-1][0]<a[i][0])
for(j=0;j<=n;j++)
{
c=a[i][j];
a[i][j]=a[i-1][j];
a[i-1][j]=c;
}
}
// TO DISPLAY MATRIX//
for(i=0;i<n;i++)
{
for(j=0;j<=n;j++)
printf("%6.1f",a[i][j]);
printf("\n");
}
// Converting to upper triangular matrix//
// Forward elimination process//
for(k=0;k<n-1;k++)
for(i=k;i<n-1;i++)
{
c= (a[i+1][k]/a[k][k]) ;
for(j=0;j<=n;j++)
a[i+1][j]-=ca[k][j];
}
// DISPLAYING UPPER TRIANGULAE MATRIX//

printf("\n\n");
for(i=0;i<n;i++)
{
for(j=0;j<=n;j++)
printf("%6.1f",a[i][j]);

printf("\n");
}
// Backward Substitution method//
for(i=n-1;i>=0;i--)
{
c=0;
for(j=i;j<=n-1;j++)
c=c+a[i][j]d[j];

d[i]=(a[i][n]-c)/a[i][i];
}
// RESULT DISPLAY //
for(i=0;i<n;i++)
cout<<d[i]<<endl;

_getch();
return 0;

}
Example:

After Compiling:

You might also like