Stats

Friday, 7 September 2012

Gauss Elimination Method


# include<stdio.h>
# include<conio.h>
void main()
{
 int i,j,k,n;
 float a[10][10],x[10];
 float s,p; 
 printf("Enter the number of equations : ");
 scanf("%d",&n) ;
 printf("\nEnter the co-efficients of the equations :\n\n");
 for(i=0;i<n;i++)
 {
  for(j=0;j<n;j++)
  {
   printf("a[%d][%d]= ",i+1,j+1);
   scanf("%f",&a[i][j]);
  }
  printf("d[%d]= ",i+1);
  scanf("%f",&a[i][n]);
 }
 for(k=0;k<n-1;k++)
 {
  for(i=k+1;i<n;i++)
  {
   p = a[i][k] / a[k][k] ;
   for(j=k;j<n+1;j++)
    a[i][j]=a[i][j]-p*a[k][j];
  }
 }
 x[n-1]=a[n-1][n]/a[n-1][n-1];
 for(i=n-2;i>=0;i--)
 {
  s=0;
  for(j=i+1;j<n;j++)
  {
   s +=(a[i][j]*x[j]);
   x[i]=(a[i][n]-s)/a[i][i];
  }
 }
 printf("\nThe result is:\n");
 for(i=0;i<n;i++)
  printf("\nx[%d]=%f",i+1,x[i]);
 getch();

}

9 comments:

  1. good day sir, can you show the matrix form after you zero all element on the lower triangle? I have a code here that show a matrix form with ) lower triangle but the problem is it gets a wrong determinants, can you help me?

    here is the code:

    # include
    # include
    # include
    # define MAX 10
    void main()
    {
    int i,j,n,k;
    float mat[MAX][MAX],x[MAX],temp,pivot,sum=0;

    printf("\t\t\t GAUSS ELIMINITION METHOD\n");
    printf("-------------------------------------------------------------------\n");
    printf("Enter No of Equations : ");
    scanf("%d",&n);
    printf("\nEnter the Elements of Matrix \n");
    for(i=1;i<=n;i++)
    for(j=1;j<=n;j++)
    scanf("%f",&mat[i][j]);
    printf("\nEnter Constant value\n");
    for(i=1;i<=n;i++)
    {
    scanf("%f",&mat[i][n+1]);
    x[i]=mat[i][n+1];
    }
    for(i=2;i<=n;i++)
    {
    for(j=i;j<=n;j++)
    {
    pivot=mat[j][i-1]/mat[i-1][i-1];
    for(k=i-1;k<=n+1;k++)
    mat[j][k]=mat[j][k]-pivot*mat[i-1][k];
    }
    }
    printf("\n\nEliminated matrix as :- \n\n");
    for(i=1;i<=n;i++)
    {
    for(j=1;j<=n+1;j++)
    printf("\t%.2f",mat[i][j]);
    printf("\n");
    }
    for(i=1;i<=n;i++)
    {
    if(mat[i][i]==0)
    {
    printf("Since diagonal element become zero\n Hence solution is not possible\n");
    }
    }
    printf("Solution : \n");
    for(i=0;in-i;j--)
    sum=sum+mat[n-i][j];

    x[n-i]=(mat[n-i][n+1]-sum*x[n])/mat[n-i][n-i];
    printf("X%d = %4.2f\n",n-i,x[n-i]);
    }

    getch();
    }


    thanks in advance sir!:)

    ReplyDelete
  2. How about data validation? User can enter string and get bad result.

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. sorry in this part :
      for(i=n-2;i>=0;i--)
      {
      s=0;
      for(j=i+1;j<n;j++)
      {
      s +=(a[i][j]*x[j]);
      x[i]=(a[i][n]-s)/a[i][i];
      }
      }
      this should be above one

      Delete
  4. Can you convert this code with c++ Sir?

    ReplyDelete
  5. THANK you for this , beautiful work :)

    ReplyDelete
  6. c++ version ,but array elements and dimension is static


    #include
    #include
    #include
    #include
    #include
    #define m 4

    using namespace std;
    int main()
    {
    int i,j,k;
    float A[10][10],x[10];
    float s,p;


    // for(i=0;i<m;i++)
    // {
    // for(j=0;j<m;j++)
    // {
    // cout<<endl<<" A["<<i+1<<","<<j+1<<"]=";
    // cin>>A[i][j];
    // }
    // cout<<endl<<" b["<<i+1<<"]=";
    // cin>>A[i][m];
    // }
    A[0][0]=0.408542;
    A[0][1]=-0.583313;
    A[0][2]=0;
    A[0][3]=0;
    A[0][4]=-0.0528507;

    A[1][0]=-0.0460201;
    A[1][1]=0.497771;
    A[1][2]=0;
    A[1][3]=0;
    A[1][4]=-1.6025;

    A[2][0]=0;
    A[2][1]=0;
    A[2][2]=-0.937109;
    A[2][3]=-1.68992;
    A[2][4]=-1.98207;

    A[3][0]=0;
    A[3][1]=0;
    A[3][2]=-0.504407;
    A[3][3]=-0.0897299;
    A[3][4]=-2.07107;


    for(k=0;k<m-1;k++)
    {
    for(i=k+1;i<m;i++)
    {
    p = A[i][k] / A[k][k] ;
    for(j=k;j<m+1;j++)
    A[i][j]=A[i][j]-p*A[k][j];
    }
    }
    x[m-1]=A[m-1][m]/A[m-1][m-1];
    for(i=m-2;i>=0;i--)
    {
    s=0;
    for(j=i+1;j<m;j++)
    {
    s +=(A[i][j]*x[j]);
    x[i]=(A[i][m]-s)/A[i][i];
    }
    }
    cout<<("\nThe result is:\n");
    for(i=0;i<m;i++)
    cout<<"x["<<i+1<<"]="<<x[i];

    return 0;

    }

    ReplyDelete