C and C++ Programs - Gauss Elimination Method

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

0

More

Next Blog

Create Blog

Sign In

C and C++ programs


Friday, 7 September 2012

Gauss Elimination Method


# include<stdio.h>
# include<conio.h>
void main()
{
int i,j,k,n;
float a[10][10],x[10];
float s,p;
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("d[%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]=%f",i+1,x[i]);
getch();
}

hit counter
hit counter

Blog Archive
2014 (6)
2012 (6)
December (1)
September (2)
Gauss Elimination Method
gauss seidel method
August (2)
July (1)

About Me
Gautam Naik
Follow

217

View my complete profile

Posted by Gautam Naik at 07:36


Recommend this on Google

2 comments:
vaibhav 24 February 2014 at 02:19
nice work thnx
Reply

Say Real 2 March 2014 at 09:11


good day sir, can you show the matrix form after you zero all element on the lower triangle? I
have a code here that show a matrix form with ) lower triangle but the problem is it gets a
wrong determinants, can you help me?
here is the code:
# include
# include
# include
# define MAX 10
void main()
{
int i,j,n,k;
float mat[MAX][MAX],x[MAX],temp,pivot,sum=0;
printf("\t\t\t GAUSS ELIMINITION METHOD\n");
printf("-------------------------------------------------------------------\n");
printf("Enter No of Equations : ");
scanf("%d",&n);
printf("\nEnter the Elements of Matrix \n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%f",&mat[i][j]);
printf("\nEnter Constant value\n");
for(i=1;i<=n;i++)
{
scanf("%f",&mat[i][n+1]);
x[i]=mat[i][n+1];
}
for(i=2;i<=n;i++)
{
for(j=i;j<=n;j++)
{
pivot=mat[j][i-1]/mat[i-1][i-1];
for(k=i-1;k<=n+1;k++)
mat[j][k]=mat[j][k]-pivot*mat[i-1][k];
}
}
printf("\n\nEliminated matrix as :- \n\n");
for(i=1;i<=n;i++)
{

for(j=1;j<=n+1;j++)
printf("\t%.2f",mat[i][j]);
printf("\n");
}
for(i=1;i<=n;i++)
{
if(mat[i][i]==0)
{
printf("Since diagonal element become zero\n Hence solution is not possible\n");
}
}
printf("Solution : \n");
for(i=0;in-i;j--)
sum=sum+mat[n-i][j];
x[n-i]=(mat[n-i][n+1]-sum*x[n])/mat[n-i][n-i];
printf("X%d = %4.2f\n",n-i,x[n-i]);
}
getch();
}

thanks in advance sir!:)


Reply

Enter your comment...

Comment as:

Publish

Newer Post

Google Account

Preview

Home

Older Post

Subscribe to: Post Comments (Atom)

Fish

Awesome Inc. template. Powered by Blogger.

You might also like