Sunday 15 March 2015

Simultaneous equation using gauss elimination method Using C

# include <stdio.h>
# include <conio.h>

void main()
{
int i, j, k, n ;
float a[20][20], x[20] ;
double s, p ;
clrscr() ;
printf("Enter the number of equations : ") ;
scanf("%d", &n) ;
printf("\nEnter the co-efficients of the equations :\n\n") ;
for(i = 0 ; i < n ; i++)
{
for(j = 0 ; j < n ; j++)
{
printf("a[%d][%d] = ", i + 1, j + 1) ;
scanf("%f", &a[i][j]) ;
}
printf("b[%d] = ", i + 1) ;
scanf("%f", &a[i][n]) ;
}
for(k = 0 ; k < n - 1 ; k++)
{
for(i = k + 1 ; i < n ; i++)
{
p = a[i][k] / a[k][k] ;
for(j = k ; j < n + 1 ; j++)
a[i][j] = a[i][j] - p * a[k][j] ;
}
}
x[n-1] = a[n-1][n] / a[n-1][n-1] ;
for(i = n - 2 ; i >= 0 ; i--)
{
s = 0 ;
for(j = i + 1 ; j < n ; j++)
{
s += (a[i][j] * x[j]) ;
x[i] = (a[i][n] - s) / a[i][i] ;
}
}
printf("\nThe result is :\n") ;
for(i = 0 ; i < n ; i++)
printf("\nx[%d] = %.2f", i + 1, x[i]) ;
getch() ;
}

RUN 1 :
~~~~~~~
Enter the number of equations : 3
Enter the co-efficients of the equations :

a[1][1] = 10
a[1][2] = 1
a[1][3] = 1
b[1] = 12
a[2][1] = 2
a[2][2] = 10
a[2][3] = 1
b[2] = 13
a[3][1] = 1
a[3][2] = 1
a[3][3] = 5
b[3] = 7
The result is :
x[1] = 1.00
x[2] = 1.00
x[3] = 1.00

No comments:

Post a Comment