Linear Algebraic Equation(Gauss Elimination & Gauss Jordan)

Gauss Elimination

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

void main()
{
	clrscr();
	int n,i,j,k;
	float a[10][10],b[10],x[10],p,f,sum;
	printf("Enter the no. of equations");
	scanf("%d",&n);
	for(i=0;i<n ;i++)
	{
		printf("Enter the coefficients of equation %d:n",i+1);
		for(j=0;j<n;j++)
			scanf("%f",&a[i][j]);
		printf("Enter the constant");
		scanf("%f",&b[i]);
	}
	for(i=0;i<n;i++)
	{
		p=a[i][i];
		for(j=i+1;j<n;j++)
		{
			f=a[j][i]/p;
			for(k=i;k<n;k++)
				a[j][k]-=f*a[i][k];
			b[j]-=f*b[i];
		}
	}
	for(i=n-1;i>=0;i--)
	{
		sum=0;
		for(j=i+1;j</n><n ;j++)
			sum+=a[i][j]*x[j];
		x[i]=(b[i]-sum)/a[i][i];
		printf("x[%d]=%fn",i,x[i]);
	}
	getch();
}

Gauss Jordan

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

void main()
{
	clrscr();
	int n,i,j,k;
	float a[10][10],b[10],x[10],p,f,sum;
	printf("Enter the no. of equations");
	scanf("%d",&n);
	for(i=0;i<n ;i++)
	{
		printf("Enter the coefficients of equation %d:n",i+1);
		for(j=0;j<=n;j++)
			scanf("%f",&a[i][j]);
	}
	for(i=0;i<n;i++)
	{
		p=a[i][i];
		for(k=0;k<=n;k++)
			a[i][k]/=p;
		for(j=0;j<n;j++)
		{
			if(j!=i)
			{
				f=a[j][i]/a[i][i];
				for(k=0;k<=n;k++)
					a[j][k]-=f*a[i][k];
			}
		}
		printf("x[%d]=%fn",i,a[i][n]);
	}
	getch();
}

Leave a Reply

Your email address will not be published. Required fields are marked *