document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <conio.h>
  4. #include <math.h>
  5. using namespace std;
  6. /*********** VARIABLES GLOBALES **********************/
  7.  
  8. double matriz[50][50];
  9. double identidad[50][50];
  10. int N; //N contiene el tama¤o de la matriz cuadrada
  11.  
  12.  
  13. /*********** PROTOTIPOS DE FUNCIONES *****************/
  14.  
  15. void hallar_inversa(void);
  16. void escalonar_matriz(void);
  17. void permutar_filas(int fila1, int fila2);
  18. void multip_fila(int fila,double factor);
  19. void sumar_fila_multip(int fila1,int fila2, double factor);
  20. void ceros_abajo(int fila_pivote, int columna_pivote);
  21. void ceros_arriba(int fila_pivote, int columna_pivote);
  22. void generar_matriz_identidad(void);
  23.  
  24. /*****************************************************/
  25.  
  26.  
  27. int main()
  28. {
  29. int fi, co;
  30.  
  31. //clrscr();
  32.  
  33. do{
  34. printf("Ingrese el tama¤o de la matriz cuadrada: ");
  35. scanf("%i",&N);
  36. if(N>50 || N<2) { printf("El numero debe estar entre 2 y 50n");}
  37. }while(N>50 || N<2);
  38.  
  39. for(fi=0;fi<N;fi++)
  40. {
  41. for(co=0;co<N;co++)
  42. {
  43. printf("Ingrese el valor de matriz[%i][%i]",fi+1,co+1);
  44. scanf("%lf",&matriz[fi][co]);
  45. }
  46. }
  47.  
  48. hallar_inversa();
  49.  
  50. return 0;
  51. }
  52.  
  53. /*-------------------------------------------------------------------------*/
  54.  
  55. void hallar_inversa(void)
  56. {
  57. int cont,cont2, flag=0;
  58.  
  59. escalonar_matriz();
  60. generar_matriz_identidad(); //rellena la matriz identidad
  61.  
  62.  
  63. for(cont=0;cont<N;cont++) //recorre filas
  64. {
  65. for(cont2=0;cont2<N;cont2++) //recorre columnas
  66. {
  67. if(matriz[cont][cont2]!=0) //busca pivote (elemento ditinto de 0)
  68. {
  69. if(matriz[cont][cont2]!=1) //si pivote no es 1, se lo multiplica
  70. {
  71. multip_fila(cont,pow(matriz[cont][cont2],-1));
  72. }
  73.  
  74. ceros_arriba(cont,cont2); // se hacen 0\'s por arriba
  75. ceros_abajo(cont,cont2); // y por debajo del pivote
  76.  
  77. break;
  78. }
  79. }
  80. }
  81.  
  82. /*--------------------------------------------------------------*/
  83. /* Una vez terminada esta operacion, la matriz identidad estara */
  84. /* transformada en la inversa */
  85. /* */
  86. /* Ahora se comprueba que la matriz original este transformada */
  87. /* en la matriz identidad, de no ser asi la inversa obtenida */
  88. /* no es valida y la matriz no tiena inversa */
  89. /*--------------------------------------------------------------*/
  90.  
  91.  
  92. for(cont=0;cont<N;cont++)
  93. {
  94. for(cont2=0;cont2<N;cont2++)
  95. {
  96. if(cont==cont2)
  97. {
  98. if(matriz[cont][cont2]!=1) flag=1;
  99. }
  100. else
  101. {
  102. if(matriz[cont][cont2]!=0) flag=1;
  103. }
  104. }
  105. }
  106.  
  107.  
  108.  
  109. if(flag==1)
  110. {
  111. cout<<"La matriz no tiene inversa"<<endl;
  112. }
  113. else
  114. {
  115. cout<<"La Matriz Inversa es :"<<endl;
  116.  
  117. for(cont=0;cont<N;cont++)
  118. {
  119.     for(cont2=0;cont2<N;cont2++)
  120.     {
  121.         cout<<identidad[cont][cont2]<<" ";
  122.  
  123.     }
  124.     cout<<endl;
  125.  
  126. }
  127. }
  128.  
  129.  
  130. printf("nPresione una tecla para continuar...");
  131. getch();
  132.  
  133.  
  134. }
  135.  
  136. /*-----------------------------------------------------------------------*/
  137. /* */
  138. /* Ordena la matriz de forma que quede en su forma escalonada por */
  139. /* renglones */
  140. /* */
  141. /*-----------------------------------------------------------------------*/
  142.  
  143. void escalonar_matriz(void)
  144. {
  145. int cont, col, ceros, vec[10];
  146. int flag, aux;
  147.  
  148. for(cont=0;cont<N;cont++)
  149. {
  150. col=0,ceros=0;
  151.  
  152. if(matriz[cont][col]==0)
  153. {
  154. do{
  155. ceros++;
  156. col++;
  157. }while(matriz[cont][col]==0);
  158. }
  159. vec[cont]=ceros;
  160. }
  161.  
  162.  
  163. do
  164. {
  165. flag=0;
  166. for(cont=0;cont<N-1;cont++)
  167. {
  168. if(vec[cont]>vec[cont+1])
  169. {
  170. aux=vec[cont];
  171. vec[cont]=vec[cont+1];
  172. vec[cont+1]=aux;
  173.  
  174. permutar_filas(cont,cont+1);
  175.  
  176. flag=1;
  177. }
  178. }
  179. }while(flag==1);
  180.  
  181. }
  182.  
  183. /*----------------------------------------------------------------------*/
  184. /* SE DEFINEN LAS 3 OPERACIONES ELEMENTALES DE FILA */
  185. /* */
  186. /* Las operaciones que se le realizen a la matriz para reducirla */
  187. /* tambien deberan realizarsele a la matriz identidad para obtener */
  188. /* la matriz inversa */
  189. /*----------------------------------------------------------------------*/
  190.  
  191.  
  192. void permutar_filas(int fila1,int fila2)
  193. {
  194. float auxval;
  195. int cont;
  196.  
  197. for(cont=0;cont<N;cont++)
  198. {
  199. auxval=matriz[fila1][cont];
  200. matriz[fila1][cont]=matriz[fila2][cont];
  201. matriz[fila2][cont]=auxval;
  202.  
  203. auxval=identidad[fila1][cont];
  204. identidad[fila1][cont]=identidad[fila2][cont];
  205. identidad[fila2][cont]=auxval;
  206. }
  207. }
  208.  
  209. /*----------------------------------------------------------------------*/
  210.  
  211. void multip_fila(int fila,double factor)
  212. {
  213. int cont;
  214.  
  215. for(cont=0;cont<N;cont++)
  216. {
  217. matriz[fila][cont]=(matriz[fila][cont])*factor;
  218. identidad[fila][cont]=(identidad[fila][cont])*factor;
  219. }
  220. }
  221.  
  222. /*----------------------------------------------------------------------*/
  223.  
  224. void sumar_fila_multip(int fila1,int fila2, double factor)
  225. {
  226. int cont;
  227. for(cont=0;cont<N;cont++)
  228. {
  229. matriz[fila1][cont]=(matriz[fila1][cont])+((matriz[fila2][cont])*factor);
  230. identidad[fila1][cont]=(identidad[fila1][cont])+((identidad[fila2][cont])*factor);
  231. }
  232. }
  233.  
  234.  
  235. void ceros_arriba(int fila_pivote, int columna_pivote)
  236. {
  237. int cont;
  238.  
  239. for(cont=0;cont<fila_pivote;cont++)
  240. {
  241. sumar_fila_multip(cont,fila_pivote,((matriz[cont][columna_pivote])*(-1)));
  242.  
  243. }
  244. }
  245.  
  246. /*-------------------------------------------------------------------------*/
  247. void ceros_abajo(int fila_pivote, int columna_pivote)
  248. {
  249. int cont;
  250.  
  251. for(cont=columna_pivote+1;cont<N;cont++)
  252. {
  253. sumar_fila_multip(cont,fila_pivote,((matriz[cont][columna_pivote])*(-1)));
  254. }
  255.  
  256. }
  257. /*-------------------------------------------------------------------------*/
  258. void generar_matriz_identidad(void)
  259. {
  260. int i,j;
  261. for(i=0;i<50;i++)
  262. {
  263. for(j=0;j<50;j++)
  264. {
  265. if(i==j) identidad[i][j]=1;
  266. else identidad[i][j]=0;
  267. }
  268. }
  269. }
');