Advertisement
Edu004

Ceros por debajo-Fila0 constante- Bonito XD

May 24th, 2012
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <math.h>
  4.  
  5. /*********** VARIABLES GLOBALES **********************/
  6. int N = 5; //N contiene el tama¤o de la matriz cuadrada
  7. double matriz[5][5];
  8. double identidad[5][5];
  9.  
  10. /*********** PROTOTIPOS DE FUNCIONES *****************/
  11.  
  12. void hallar_matriz(void);
  13. void escalonar_matriz(void);
  14. void permutar_filas(int fila1, int fila2);
  15. void multip_fila(int fila,double factor);
  16. void sumar_fila_multip(int fila1,int fila2, double factor);
  17. void ceros_abajo(int fila_pivote, int columna_pivote);
  18. /*****************************************************/
  19.  
  20.  
  21. int main()
  22. {
  23. srand (time(NULL));
  24. int fi, co;
  25.  
  26. for(fi=0;fi<N;fi++)
  27. {
  28. for(co=0;co<N;co++)
  29. {
  30.  
  31. int numero= rand()%99; //numeros entre 0 y 99
  32. matriz[fi][co] = numero;
  33. printf("%d ", numero);
  34. }
  35. printf("\n");
  36. }
  37. printf("Calculando la matriz .... \n");
  38. hallar_matriz();
  39.  
  40. return 0;
  41. }
  42.  
  43. /*-------------------------------------------------------------------------*/
  44.  
  45. void hallar_matriz(void)
  46. {
  47. int cont,cont2, flag=0;
  48.  
  49. escalonar_matriz();
  50.  
  51.  
  52. for(cont=1;cont<N;cont++) //recorre filas, como queremos que la primera fila no se cambie empezamos en la 1
  53. {
  54. for(cont2=1;cont2<N;cont2++) //recorre columnas, como queremos que la primera columna no se cambie empezamos en la 1
  55. {
  56. if(matriz[cont][cont2]!=0) //busca pivote (elemento ditinto de 0)
  57. {
  58. if(matriz[cont][cont2]!=1) //si pivote no es 1, se lo multiplica
  59. {
  60. multip_fila(cont,pow(matriz[cont][cont2],-1));
  61. }
  62.  
  63. ceros_abajo(cont,cont2); // 0's por debajo del pivote
  64.  
  65. break;
  66. }
  67. }
  68. }
  69.  
  70. for(cont=0;cont<N;cont++)
  71. {printf("|");
  72. for(cont2=0;cont2<N;cont2++)
  73. {
  74.  
  75. printf("%7.3g ",matriz[cont][cont2]); //forzar a 7 caracteres, precision 3 decimales y usar el minimo de caracteres
  76. }
  77. printf("|\n");
  78. }
  79.  
  80.  
  81. printf("\n Presione una tecla para continuar...");
  82. getch();
  83.  
  84.  
  85. }
  86.  
  87. /*-----------------------------------------------------------------------*/
  88. /* */
  89. /* Ordena la matriz de forma que quede en su forma escalonada por */
  90. /* renglones */
  91. /* */
  92. /*-----------------------------------------------------------------------*/
  93.  
  94. void escalonar_matriz(void)
  95. {
  96. int cont, col, ceros, vec[10];
  97. int flag, aux;
  98.  
  99. for(cont=0;cont<N;cont++)
  100. {
  101. col=0,ceros=0;
  102.  
  103. if(matriz[cont][col]==0)
  104. {
  105. do{
  106. ceros++;
  107. col++;
  108. }while(matriz[cont][col]==0);
  109. }
  110. vec[cont]=ceros;
  111. }
  112.  
  113.  
  114. do
  115. {
  116. flag=0;
  117. for(cont=0;cont<N-1;cont++)
  118. {
  119. if(vec[cont]>vec[cont+1])
  120. {
  121. aux=vec[cont];
  122. vec[cont]=vec[cont+1];
  123. vec[cont+1]=aux;
  124.  
  125. permutar_filas(cont,cont+1);
  126.  
  127. flag=1;
  128. }
  129. }
  130. }while(flag==1);
  131.  
  132. }
  133.  
  134. /*----------------------------------------------------------------------*/
  135. /* SE DEFINEN LAS 3 OPERACIONES ELEMENTALES DE FILA */
  136. /* */
  137. /* Las operaciones que se le realizen a la matriz para reducirla */
  138. /* tambien deberan realizarsele a la matriz identidad para obtener */
  139. /* la matriz inversa */
  140. /*----------------------------------------------------------------------*/
  141.  
  142.  
  143. void permutar_filas(int fila1,int fila2)
  144. {
  145. float auxval;
  146. int cont;
  147.  
  148. for(cont=0;cont<N;cont++)
  149. {
  150. auxval=matriz[fila1][cont];
  151. matriz[fila1][cont]=matriz[fila2][cont];
  152. matriz[fila2][cont]=auxval;
  153.  
  154. auxval=identidad[fila1][cont];
  155. identidad[fila1][cont]=identidad[fila2][cont];
  156. identidad[fila2][cont]=auxval;
  157. }
  158. }
  159.  
  160. /*----------------------------------------------------------------------*/
  161.  
  162. void multip_fila(int fila,double factor)
  163. {
  164. int cont;
  165.  
  166. for(cont=0;cont<N;cont++)
  167. {
  168. matriz[fila][cont]=(matriz[fila][cont])*factor;
  169. identidad[fila][cont]=(identidad[fila][cont])*factor;
  170. }
  171. }
  172.  
  173. /*----------------------------------------------------------------------*/
  174.  
  175. void sumar_fila_multip(int fila1,int fila2, double factor)
  176. {
  177. int cont;
  178. for(cont=0;cont<N;cont++)
  179. {
  180. matriz[fila1][cont]=(matriz[fila1][cont])+((matriz[fila2][cont])*factor);
  181. identidad[fila1][cont]=(identidad[fila1][cont])+((identidad[fila2][cont])*factor);
  182. }
  183. }
  184.  
  185. /*-------------------------------------------------------------------------*/
  186. void ceros_abajo(int fila_pivote, int columna_pivote)
  187. {
  188. int cont;
  189.  
  190. for(cont=columna_pivote+1;cont<N;cont++)
  191. {
  192. sumar_fila_multip(cont,fila_pivote,((matriz[cont][columna_pivote])*(-1)));
  193. }
  194.  
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement