C program to solve system of equations using Gauss-Jordan method

Implement Gauss-Jordan method. Input:number of Equations, number of unknowns, Right hand side Constants. Output solutions to the unknowns.

#include stdio .h

#include<stdio.h>
#include<conio.h>
#include<process.h>
void main()
{
    int k,i,j,n,n1;
    float temp,given[10][10];
    char dvar[10];
    clrscr();
    printf("\nEnter the no: of unknowns :");
    scanf("%d",&n);
    printf("\nEnter the no: of equations :");
    scanf("%d",&n1);
    if(n!=n1)
    {
printf("\n\nNo: of equations and unknowns are not equal..");
getch();
exit(1);
    }
    printf("\nEnter the unknowns :");
    for(i=0;i<n;i++)
    {
dvar[i]=getche();
getch();
printf(" ");
    }
    printf("\nEnter the augmented matrix of equations :\n\n");
    for(i=0;i<n;i++)
    for(j=0;j<n+1;j++)
scanf("%f",&given[i][j]);
    for(k=0;k<n-1;k++)
    {
for(i=k+1;i<n;i++)
{
    temp=given[i][k]/given[k][k];
    for(j=0;j<n+1;j++)
given[i][j]=given[i][j]-temp*given[k][j];
}
    }
    for(k=n-1;k>0;k--)
    {
for(i=k-1;i>=0;i--)
{
    temp=given[i][k]/given[k][k];
    for(j=n;j>=0;j--)
 given[i][j]=given[i][j]-temp*given[k][j];
}
    }
    printf("\n");
    for(i=0;i<n;i++)
    {
for(j=0;j<n+1;j++)
printf("%f\t",given[i][j]);
printf("\n");
    }
    printf("\n\n");
    for(i=0;i<n;i++)
printf("%c=%f\n",dvar[i],given[i][n]/given[i][i]);
    getch();
}

0 comments:

Post a Comment