Advertisement
link186fr

Multiplicacion de Matrices en C.

Nov 9th, 2013
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.56 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <time.h>
  3.  
  4. int main()
  5. {
  6.  
  7.    int a[8][8],b[8][8],t[8][8],r,c,d,m,n,p,q; char choice;
  8.  
  9.       srand(time(NULL)); // aleatorios del microprocesador
  10.  
  11.       for(r=0;r<8;r++)
  12.          for(c=0;c<8;c++)                  //limpia matrices
  13.             a[r][c]=b[r][c]=t[r][c]=0;
  14.            
  15.   puts("Dame m(renglones) y n(columnas) de la primera matriz separados por un espacio:");
  16.      scanf("%d%d",&m,&n);
  17.  
  18.   puts("Dame p(renglones) y q(columnas) de la segunda matriz separados por un espacio:");
  19.      scanf("%d%d",&p,&q);
  20.    
  21.       getchar(); // se come el enter que se queda esperando en el bufer y no interfiera procesos
  22.  
  23.   if(m>8||n>8||p>8||q>8)
  24.    puts("Numero invalido de renglones o columnas, recuerda que tu matriz debe ser de maximo 8x8");          
  25.  
  26.    else
  27. {
  28.   if(n!=p)
  29.       printf("No se pueden multiplicar las matrices debido a que n y p son diferentes.\n");
  30.    
  31.    else
  32.    {
  33.        puts("¿Deseas llenar las matrices manualmente o aleatorio?[m/a]");
  34.            scanf("%c",&choice);
  35.      
  36.       if(choice == 'm')
  37.      {    
  38.              getchar();
  39.     printf("Dame los %d datos de la primera matriz en el mismo renglon, separados por un espacio.\n",m*n);
  40.         for(r=0;r<m;r++)
  41.            for(c=0;c<n;c++)
  42.               scanf("%d",&a[r][c]);
  43.  
  44.   printf("Dame los %d datos de la segunda matriz en el mismo renglon, separados por un espacio.\n",p*q);
  45.         for(r=0;r<p;r++)
  46.            for(c=0;c<q;c++)
  47.               scanf("%d",&b[r][c]);  
  48.      }
  49.  
  50.       else{          //matriz a
  51.                      for(r=0;r<m;r++)
  52.                         for(c=0;c<n;c++)
  53.                            a[r][c]=rand()%51;
  54.  
  55.  
  56.                      //matriz b
  57.                      for(r=0;r<p;r++)
  58.                         for(c=0;c<q;c++)
  59.                            b[r][c]=rand()%51;
  60.  
  61.          
  62.            }  
  63.    
  64.   //producto de matrices
  65.         for(r=0;r<m;r++)
  66.        for(c=0;c<q;c++)
  67.               for(d=0;d<n;d++)                  
  68.                  t[r][c]=t[r][c]+a[r][d]*b[d][c];
  69.      
  70.  
  71.     puts("Primera matriz\n");
  72.  
  73.     for(r=0;r<m;r++){
  74.            for(c=0;c<n;c++)
  75.                printf("%d\t",a[r][c]);
  76.            printf("\n");
  77.                         }
  78.  
  79.      puts("Segunda matriz\n");
  80.  
  81.         for(r=0;r<p;r++){
  82.            for(c=0;c<q;c++)
  83.                printf("%d\t",b[r][c]);
  84.            printf("\n");
  85.                         }
  86.  
  87.      puts("Producto de matrices\n");
  88.  
  89.         for(r=0;r<m;r++){
  90.            for(c=0;c<q;c++)      
  91.           printf("%d\t",t[r][c]);
  92.           printf("\n");
  93.                     }
  94.    }
  95.  } 
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement