Saturday, 25 August 2012

C Implemntation of Gauss Elimination Method

#include<stdio.h>
#include<stdlib.h>
                     
#define max 100      
      
void read_matrix(double arr[max][max],int n)
{
    int i,j;
    
    for(i=0;i<n;i++)
    {
        for(j=0;j<=n;j++)
        {
            printf("Enter element [%d,%d]",i,j);
            scanf("%lf",&arr[i][j]);
        }
        printf("\n");
    }
}

void write_matrix(double arr[max][max],int n)
{
    int i,j;
    
    for(i=0;i<n;i++)
    {
        for(j=0;j<=n;j++)
        {
            printf(" %lf ",arr[i][j]);
        }
        printf("\n");
    }
}

void triangularize_matrix(double arr[max][max],int dim)
{
    int i,j,k;
    double u;
    for(i=0;i<dim;i++)
    {
        
        for(j=i+1;j<dim;j++)
        {
            
            u=(arr[j][i]/arr[i][i]);
            
            for(k=i;k<=dim;k++)
            {
                arr[j][k]-=(u*arr[i][k]);
            }
        }
    }
}

void back_substitution(double arr[max][max],int dim)
{
    int i,j,k;
    double u=0.0;
    for(i=dim-1;i>=0;i--)
    {
        u=arr[i][dim];
        for(j=dim-1;j>i;j--)
        {
            u-=arr[i][j]*arr[j][dim];
        }
        arr[i][dim]=(u/arr[i][i]);
        
    }
}
int main()
{
    double arr[max][max];int dim;
    printf("Enter the Dimension\n");
    scanf("%d",&dim);

    read_matrix(arr,dim);
    write_matrix(arr,dim);
    
    triangularize_matrix(arr,dim);
    write_matrix(arr,dim);

    back_substitution(arr,dim);
    write_matrix(arr,dim);
    system("PAUSE");
    return 0;
}

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
@Gnosioware Solutions