Wednesday, November 12, 2014

NUmeric : Naive Gauss code

Sample equation for Naïve Gauss

3x1-.01x2-0.2x3=7.85

0.1x1+7x2-0.3x3=-19.3

0.3x1-0.2x2+10x3=71.43

 

 

//perfect for all gauss method
#include<stdio.h>
int main()
{
   // freopen("input.txt","r",stdin);
    float a[10][10],x3,x2,x1;
    float p;
    int c,r,i,j;
 
    printf("Enter row=");
    scanf("%d",&r);
    printf("Enter collom=");
    scanf("%d",&c);
 
//.............................................
    for(i=0;i<r;i++)
    {
        for(j=0;j<c;j++)
        {
            printf("a[%d][%d]=",i,j);
            scanf("%f",&a[i][j]);
        }
    }
    //...........................................
    printf("\nHere is the matrix =\n");
for (i=0;i<r;i++)
{
    for(j=0;j<c;j++)
    {
        printf("%.2f  ",a[i][j]);
    }
    printf("\n");
}
//...............................................
//2nd row
printf(" \n\n\n");
p=a[1][0];
for(i=1;i<2;i++)
{
    for(j=0;j<4;j++)
    {
 
        a[i][j]=(a[i][j]-a[0][j]*(p/a[0][0]));
        //printf("%.2f  ",a[i][j]);
    }
}
//...............................................
printf("\nAFTER r2=r2-r1*(10/00) =\n");
for (i=0;i<r;i++)
{
    for(j=0;j<c;j++)
    {
        printf("%.2f  ",a[i][j]);
    }
    printf("\n");
}
//...............................................
printf("\n");
//3rd row
float q=a[2][0];
for(i=2;i<3;i++)
{
    for(j=0;j<4;j++)
    {
        a[i][j]=(a[i][j]-a[0][j]*(q/a[0][0]));
       // printf("%.2f  ",a[i][j]);
    }
}
//...............................................
printf("\nAFTER r3=r3-r1*(20/00) =\n");
for (i=0;i<r;i++)
{
    for(j=0;j<c;j++)
    {
        printf("%.2f  ",a[i][j]);
    }
    printf("\n");
}
//...............................................
printf("\n");
float z=a[2][1];
for(i=2;i<3;i++)
{
    for(j=0;j<4;j++)
    {
        a[i][j]=(a[i][j]-a[1][j]*(z/a[1][1]));
       // printf("%.2f  ",a[i][j]);
    }
}
//...............................................
printf("\nAFTER r3=r3-r2*(21/11) =\n");
for (i=0;i<r;i++)
{
    for(j=0;j<c;j++)
    {
        printf("%.2f  ",a[i][j]);
    }
    printf("\n");
}
//...............................................
x3=a[2][3]/a[2][2];
printf("\n here x3 is = %f ",x3);
 
x2=((a[1][3]-a[1][2]*x3)/a[1][1]);
printf("\n here x2 is = %f ",x2);
 
x1=(a[0][3]-(a[0][1]*x2)-(a[0][2]*x3))/a[0][0];
 
printf("\n here x1 is = %f ",x1);
return 0;
}

No comments:

Post a Comment