ariestamirra

gausss

Apr 20th, 2014
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.73 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #define MAKS 50
  4.  
  5. int i, j, k, n, m;
  6. float A[MAKS][MAKS], B[MAKS];
  7.  
  8. void title(){
  9.         printf("======================\n");
  10.         printf("Metode Eliminasi Gauss\nOleh : Mirra Ariesta AA\n");
  11.         printf("======================\n");
  12. }
  13.  
  14. void inputMatriks(){
  15.     printf("Input ordo baris : ");
  16.     scanf("%d",&n);
  17.     printf("Input ordo kolom : ");
  18.     scanf("%d",&m);
  19.     printf("\n");
  20.         for(i=0;i<n;i++){
  21.         for(j=0;j<m;j++){
  22.             printf("A[%d][%d] = ",i,j);
  23.             scanf("%f",&A[i][j]);
  24.         }
  25.         printf("B[%d] = ",i);
  26.         scanf("%f",&B[i]);
  27.         printf("\n");
  28.     }
  29. }
  30. void pivot(int l){
  31.     float c,temp;
  32.     int y;
  33. awal:  
  34.     if(A[l][l]==1);
  35.     else if(A[l][l]!=0&&A[l][l]!=1){
  36.         c=1/A[l][l];
  37.         for(j=0;j<m;j++){
  38.             A[l][j]=c*A[l][j];
  39.         }
  40.         B[l]=c*B[l];
  41.     }
  42.     else{
  43.         for(y=l+1;y<n;y++){
  44.             if(A[y][l]!=0){
  45.                 for(j=0;j<m;j++){
  46.                     temp=A[y][j];
  47.                     A[y][j]=A[l][j];
  48.                     A[l][j]=temp;
  49.                 }
  50.                 temp=B[l];
  51.                 B[l]=B[y];
  52.                 B[y]=temp;
  53.                 outputMatriks();
  54.                 goto awal;
  55.             }
  56.             else
  57.                 continue;
  58.         }
  59.         printf("Sorry, Error..!!\n");
  60.         exit(0);
  61.     }
  62. }
  63. void obe1(int l){
  64.     float h;
  65.     for(i=l+1;i<n;i++){
  66.         h=A[i][l]/A[l][l];
  67.         for(j=l;j<m;j++){
  68.             A[i][j]=A[i][j]-h*A[l][j];
  69.         }
  70.         B[i]=B[i]-h*B[l];
  71.     }
  72. }
  73. void obe2(int l){
  74.     float h;
  75.     for(i=l-1;i>=0;i--){
  76.         h=A[i][l]/A[l][l];
  77.         for(j=l;j>=0;j--){
  78.             A[i][j]=A[i][j]-h*A[l][j];
  79.         }
  80.         B[i]=B[i]-h*B[l];
  81.     }
  82. }
  83. void outputMatriks(){
  84.     for(i=0;i<n;i++){
  85.         for(j=0;j<m;j++){
  86.             printf("%5.0f\t",A[i][j]);
  87.         }
  88.         printf("|%5.0f \n",B[i]);
  89.     }
  90.     printf("\n");
  91. }
  92.  
  93. main(){
  94.     int l;
  95.     title();
  96.     inputMatriks();
  97.     outputMatriks();
  98.     for(l=0;l<n;l++){
  99.         pivot(l);
  100.         obe1(l);
  101.     }
  102.     outputMatriks();
  103.     for(l=n-1;l>=0;l--) {
  104.         obe2(l);
  105.     }
  106.     outputMatriks();
  107. }
Advertisement
Add Comment
Please, Sign In to add comment