Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.68 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. #define DIM 3
  4.  
  5. void intercambiar_filas(float[DIM][DIM+1], int, int);
  6. void dividir_fila(float[DIM][DIM+1], int, float);
  7. void restar_filas(float[DIM][DIM+1], int, int);
  8. void print_matrix(float [DIM][DIM+1]);
  9.  
  10. int main(int argc, char **argv)
  11. {
  12.     float matrix[DIM][DIM+1]={{1,3,2,-19},{-5,-1,-5,10},{-1,3,1,-20}};
  13.    
  14.     int f, c, i;
  15.    
  16.     printf("Partimos de esta matriz:\n");
  17.     print_matrix(matrix);
  18.     printf("Let's go...\n");
  19.    
  20.     for(c=0; c<DIM; c++)
  21.     {
  22.         /* Si el elemento pivote ([0][0], [1][1], etc...) es cero, buscamos una fila donde no lo sea y las intercambiamos (si no lo encontramos, entonces toda la columna es cero y quiere decir que el sistema está sobredimensionado) */
  23.         f=0;
  24.         while(matrix[c][c]==0 && matrix[f][c]==0 && f<DIM)
  25.             f++;
  26.            
  27.         if(f>0 && f<DIM)
  28.         {
  29.             printf("\n--> Intercambiando filas [%d] y [%d]\n", c+1, f+1);
  30.             intercambiar_filas(matrix, c, f);
  31.             print_matrix(matrix);
  32.         }
  33.  
  34.         /* Convertimos el pivote en 1 */
  35.         printf("\n--> Diviendo fila [%d] entre <%.2f>...\n", c+1, matrix[f][c]);
  36.         dividir_fila(matrix, c, matrix[c][c]);
  37.         print_matrix(matrix);
  38.        
  39.         /* Y ahora nos cargamos los elementos por encima y por debajo de los pivotes */
  40.         for(f=0; f<DIM; f++)
  41.         {          
  42.             if(matrix[f][c]!=0 && matrix[f][c]!=1)
  43.             {  
  44.                 printf("\n--> Diviendo fila [%d] entre <%.2f>...\n", f+1, matrix[f][c]);
  45.                 dividir_fila(matrix, f, matrix[f][c]);
  46.                 print_matrix(matrix);
  47.             }
  48.            
  49.             if(f!=c && matrix[f][c]!=0)
  50.             {
  51.                 printf("\n--> Restando fila [%d] menos [%d]...\n", f+1, c+1);
  52.                 restar_filas(matrix, f, c);
  53.                 print_matrix(matrix);
  54.             }
  55.         }
  56.        
  57.         printf("\n");
  58.     }
  59.    
  60.     printf("Y la matriz escalonada es:\n");
  61.     print_matrix(matrix);
  62.     printf("\n");
  63.    
  64.     printf("X = %.2f\n", matrix[0][DIM]/matrix[0][0]);
  65.     printf("Y = %.2f\n", matrix[1][DIM]/matrix[1][1]);
  66.     printf("Z = %.2f\n", matrix[2][DIM]/matrix[2][2]);
  67.    
  68.     return 0;
  69. }
  70.  
  71. void dividir_fila(float matrix[DIM][DIM+1], int fila, float divisor)
  72. {
  73.     int c;
  74.    
  75.     for(c=0; c<DIM+1; c++)
  76.         matrix[fila][c]/=divisor;
  77. }
  78.  
  79. void intercambiar_filas(float matrix[DIM][DIM+1], int fila1, int fila2)
  80. {
  81.     float aux;
  82.     int c;
  83.    
  84.     for(c=0; c<DIM+1; c++)
  85.     {
  86.         aux=matrix[fila1][c];
  87.         matrix[fila1][c]=matrix[fila2][c];
  88.         matrix[fila2][c]=aux;
  89.     }
  90. }
  91.  
  92. void restar_filas(float matrix[DIM][DIM+1], int f_restando, int f_restador)
  93. {
  94.     int c;
  95.    
  96.     for(c=0; c<DIM+1; c++)
  97.         matrix[f_restando][c]-=matrix[f_restador][c];
  98. }
  99.  
  100. void print_matrix(float matrix[DIM][DIM+1])
  101. {
  102.     int f,c;
  103.    
  104.     printf("***********************************");
  105.    
  106.     for(f=0; f<DIM; f++)
  107.         for(printf("\n"), c=0; c<DIM+1; c++)
  108.             printf("%8.2f ", matrix[f][c]);
  109.    
  110.     printf("\n***********************************\n");
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement