rootUser

GJM

Jun 13th, 2016
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.17 KB | None | 0 0
  1. #include<stdio.h>
  2. int main()
  3. {
  4.     int i,j,k,n;
  5.     float A[20][20],c,x[10],sum=0.0;
  6.     printf("\nEnter the order of matrix: ");
  7.     scanf("%d",&n);
  8.     printf("\nEnter the elements of augmented matrix row-wise:\n\n");
  9.     for(i=1; i<=n; i++)
  10.     {
  11.         for(j=1; j<=(n+1); j++)
  12.         {
  13.             printf("A[%d][%d] : ", i,j);
  14.             scanf("%f",&A[i][j]);
  15.         }
  16.     }
  17.     for(j=1; j<=n; j++) /* loop for the generation of upper triangular matrix*/
  18.     {
  19.         for(i=1; i<=n; i++)
  20.         {
  21.             if(i>j)
  22.             {
  23.                 c=A[i][j]/A[j][j];
  24.                 for(k=1; k<=n+1; k++)
  25.                 {
  26.                     A[i][k]=A[i][k]-c*A[j][k];
  27.                 }
  28.             }
  29.         }
  30.     }
  31.     x[n]=A[n][n+1]/A[n][n];
  32.     /* this loop is for backward substitution*/
  33.     for(i=n-1; i>=1; i--)
  34.     {
  35.         sum=0;
  36.         for(j=i+1; j<=n; j++)
  37.         {
  38.             sum=sum+A[i][j]*x[j];
  39.         }
  40.         x[i]=(A[i][n+1]-sum)/A[i][i];
  41.     }
  42.     printf("\nThe solution is: \n");
  43.     for(i=1; i<=n; i++)
  44.     {
  45.         printf("\nx%d=%f\t",i,x[i]); /* x1, x2, x3 are the required solutions*/
  46.     }
  47.     return(0);
  48. }
Add Comment
Please, Sign In to add comment