XeBuZer0

Matrix Product in C W/Dinamic Memory

Nov 30th, 2019 (edited)
615
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.33 KB | None | 0 0
  1. /* ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** */
  2. /* ** // ** // ** // Código fuente de programa que realiza producto matricial // ** // ** // ** */
  3. /* ** // ** // ** // * Licenciado bajo GNU General Public License (GPL) 3.0 * // ** // ** // ** */
  4. /* ** // ** // ** // ** // ** // ** // Created by: XeBuZer0 // ** // ** // ** // ** // ** // ** */
  5. /* ** // ** // ** // ** // ** // Windows beta tester: Rafael Mora // ** // ** // ** // ** // ** */
  6. /* ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** */
  7. /* ** // ** // EDITED V2.0: Improved compatibility with windows. For better results // ** // ** */
  8. /* ** // ** // * this was tested with windows, compiled with Geany as IDE and TCC * // ** // ** */
  9. /* ** // ** // ** Under Linux there's no trouble, tested with GCC, Clang and TCC ** // ** // ** */
  10. /* ** // ** // ** // ** // ** V2.0 finished on 5th - December - 2021 ** // ** // ** // ** // ** */
  11. /* ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** */
  12. /* ** // ** // ** // ** // ** //  * F v q _ U k r a N a z i s ! * // ** // ** // ** // ** // ** */
  13. /* ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** */
  14.  
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <errno.h>
  18. #include <string.h>
  19. #include <time.h>
  20.  
  21. typedef long double** matriz;
  22.  
  23. matriz  InitMat     (const int f, const int c);
  24. void    LlenaMat    (const int f, const int c, matriz M);
  25. void    LeerMat     (const int f, const int c, matriz M);
  26. void    LlenaMatRand(const int f, const int c, matriz M);
  27. void    ProdMat     (const int i, const int j, const int k, const matriz M1, const matriz M2, matriz M3);
  28. void    clean_stdin (void);
  29.  
  30. int main (void){
  31.     matriz M1, M2, M3;
  32.     int i=0, j=0, k=0,a=0;
  33.     printf("Ingrese # renglones de M1: ");
  34.     scanf("%i", &i);
  35.     printf("Ingrese # columnas de M1: ");
  36.     scanf("%i", &j);
  37.     printf("Ingrese # columnas de M2: ");
  38.     scanf("%i", &k);
  39.     printf("¿Desea ingresar el contenido de las matrices\nO desea que el contenido sea aleatorio?\n1) Si, ingresar manualmente\n0) No, contenido aleatorio\n Ingrese opción: ");
  40.     scanf("%i", &a);
  41.     M1 = InitMat(i,j);
  42.     M2 = InitMat(j,k);
  43.     M3 = InitMat(i,k);
  44.     if (a==1){
  45.         printf("Ingresando datos de Matriz 1...\n");
  46.         LlenaMat(i,j,M1);
  47.         printf("Ingresando datos de Matriz 2...\n");
  48.         LlenaMat(j,k,M2);
  49.     }
  50.     if (!a) {
  51.         printf("Ingresando datos de Matriz 1...\n");
  52.         LlenaMatRand(i,j,M1);
  53.         printf("Ingresando datos de Matriz 2...\n");
  54.         LlenaMatRand(j,k,M2);
  55.     }
  56.     if (!(!a || a==1)) {
  57.         printf("¿Usted se hace pendejo o neta mastica agua?\n");
  58.         return a;
  59.     }
  60.     printf("Multiplicando Matrices...\n");
  61.     ProdMat(i,j,k,M1,M2,M3);
  62.     LeerMat(i,k,M3);
  63.     free(M1); free(M2); free(M3);
  64.     return 0;
  65. }
  66.  
  67. matriz InitMat(const int f, const int c){
  68.     extern int errno;
  69.     register int t = 0;
  70.     matriz dummy = (matriz)calloc(f,sizeof(long double*));
  71.     if (dummy != NULL)
  72.         for (t = 0; t<f;t++){
  73.             *(dummy+t) = (long double*)calloc(c,sizeof(long double));
  74.             if ( *(dummy+t) == NULL ){
  75.                     fprintf(stderr, "Error asignando memoria:\nCode error:%d\n%s\n", errno, strerror(errno));
  76.                     exit(EXIT_FAILURE);
  77.                     }
  78.         }
  79.     else {
  80.         fprintf(stderr, "Error asignando memoria:\nCode error:%d\n%s\n", errno, strerror(errno));
  81.         exit(EXIT_FAILURE);
  82.     }
  83.     return dummy;
  84. }
  85.  
  86. void LlenaMat(const int f, const int c, matriz M){
  87.     register int x = 0, y = 0;
  88.     for(x=0;x < f;x++)
  89.         for(y=0;y < c;y++){
  90.             printf("Ingrese valor %d,%d : ",x+1,y+1);
  91.             scanf("%Lf",&(M[x][y]));
  92.         }
  93.     LeerMat(f,c,M);
  94. }
  95.  
  96. void LlenaMatRand(const int f, const int c, matriz M){
  97.     register int x = 0, y = 0;
  98.     time_t t = 0;
  99.     srand((unsigned) time(&t));
  100.     for(x=0;x < f;x++)
  101.         for(y=0;y < c;y++)
  102.             M[x][y] = (rand()%99);
  103.     LeerMat(f,c,M);
  104. }
  105.  
  106. void LeerMat(const int f, const int c, matriz M){
  107.     printf("Imprimiendo Matriz... \n");
  108.     register int x = 0, y = 0;
  109.     for(x=0;x < f;x++)
  110.         for(y=0;y < c;y++){
  111.             printf("%4Lf%c",M[x][y],(y+1==c)?'\n':'\t');
  112.         }
  113. }
  114.  
  115. void clean_stdin(void){
  116.     register int c;
  117.     do c = getchar();
  118.     while (c != '\n' && c != EOF);
  119. }
  120.  
  121. void ProdMat(const int i, const int j, const int k, const matriz M1, const matriz M2, matriz M3){
  122.     register int x = 0, y = 0, z=0;
  123.     for (x = 0;x<i;x++)
  124.         for (y = 0;y<k;y++)
  125.             for (z = 0;z<j;z++)
  126.                 M3[x][y] = (M1[x][z] * M2[z][y]) + M3[x][y];
  127. }
  128.  
Add Comment
Please, Sign In to add comment