NUMERICAL METHODS

Implementation of Gauss Elimination Method using C


#include<stdio.h>
int main()
{
    int n,i,j,k;
    float a[i][j],x[10],m,s=0;
    printf("\nEnter the rank of the coefficient of matrix :");
    scanf("%f",&n);
    printf("\nEnter the elements of the augmented matrix rowwise :");
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n;j++)
        {
            scanf("%f",&a[i][j]);
        }
    }
    for(i=1;i<=n;i++)
    {
        for(j=i+1;j<=n;i++)
        {
            m=a[j][i]/a[i][i];
            for(k=1;k<=n+1;k++)
            {
                a[j][k]=a[j][k]-m*a[i][k];
            }
        }
    }
    printf("\nThe upper triangular matrix is :\n");
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n+1;j++)
        {
            printf("4.3%f",&a[i][j]);
            printf("\n");
        }
    }
    x[n]=a[n][n+1]/a[n][n];
    for(i=n-1;i>=1;i--)
    {
        s=0;
        for(j=i+1;j<=n;j++)
        {
            s=s+a[i][j]*x[j];
            x[i]=(a[i][n+1]-s)/a[i][i];
        }
    }
    printf("\nThe required solution is :\n");
    for(i=1;i<=n;i++)
    {
        printf("x[%d]=%4.3f\n",i,x[i]);
    }
}

Output : 
                Enter the rank of the coefficient matrix :4

                Enter the elements of augmented matrix rowwise :
                10        8        -3        1        16
                 2        10        1        -4        9
                 3        -4        10        1        10
                 2        2        -3        10        11

                The upper triangular matrix is :
                10.000      8.000      -3.000      1.000      16.000
                -0.000       8.400      1.600      -4.200      5.800
                -0.000       0.000      12.119     -2.500     9.619
                -0.000       0.000      0.000       9.489      9.489

                The required solution is :
                x[1] = 1.000
                x[2] = 1.000
                x[3] = 1.000
                x[4] = 1.000


/* If you hava any problem, comment below */

Comments