fpontesmorales

Multiplicação de Matrizes

Aug 10th, 2011
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.78 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct {
  5.   int **ptr;
  6.   int l;
  7.   int c;
  8. } IntMat;
  9.  
  10. void readInfo(IntMat *m)
  11.     {
  12.         // função para ler a quantidade de linhas e colunas da matriz
  13.  
  14.          printf("\nMatriz \n");
  15.          printf("Linhas: ");
  16.          scanf("%d", &m->l);
  17.          printf("Colunas: ");
  18.          scanf("%d", &m->c);
  19.  
  20.          //aqui testamos se as informações são válidas (positivas)
  21.  
  22.              if(m->l < 1 || m->c < 1)
  23.               {
  24.                printf("Erro: Parametro invalido\n");
  25.                exit(1);
  26.               }
  27.      }
  28.  
  29. int testDim(IntMat *m1, IntMat *m2, IntMat *m3)
  30.     {
  31.         //função para testar se com as dimensões informadas, podemos multiplicar a matriz
  32.  
  33.         if(m1->c == m2->l)
  34.         {
  35.             m3->l = m1->l;
  36.             m3->c = m2->c;
  37.             printf("\n\nMatriz 3 = [%d,%d]\n", m3->l , m3->c);
  38.             return 1;
  39.         }
  40.         else{return 0;}
  41.     }
  42.  
  43. int allocMat(IntMat *m)
  44.     {
  45.  
  46.         int i;
  47.  
  48.         //alocando as linhas
  49.       if((m->ptr = (int **) calloc(m->l, sizeof(int *))) == NULL) {
  50.         printf("** Erro: Memoria Insuficiente **\n");
  51.         return 0;
  52.       }
  53.  
  54.         //alocando as colunas
  55.       for(i = 0; i < m->l; i++) {
  56.         if((*(m->ptr + i) = (int *) calloc(m->c, sizeof(int))) == NULL) {
  57.           printf("** Erro: Memoria Insuficiente **\n");
  58.           return 0;
  59.         }
  60.       }
  61.  
  62. }
  63. void readMat(IntMat *m)
  64.     {
  65.  
  66.         // aqui lemos os elementos da matriz
  67.  
  68.     int i, j;
  69.  
  70.     for (i = 0; i < m->l; i++){
  71.         for (j = 0; j < m->c; j++){
  72.             printf("Digite o elemento (%d,%d) da Matriz : ", i , j);
  73.             scanf("%d", (*(m->ptr + i) + j));
  74.             printf("\n");
  75.  
  76.         }
  77.       }
  78.        printf("Elementos preenchidos!\n\n");
  79.  
  80.  
  81. }
  82. int multiMat(IntMat *m1, IntMat *m2, IntMat *m3)
  83.     {
  84.         int i, j,k;
  85.  
  86.         for(i = 0; i < m1->l; i++) {
  87.         for(j = 0; j < m2->c; j++) {
  88.         for(k = 0; k < m1->c; k++) {
  89.         *(*((m3->ptr) + i) + j) = *(*((m3->ptr) + i) + j) + (*(*((m1->ptr) + i) + k)) * (*(*((m2->ptr) + k) + j));
  90.         }
  91.         }
  92.         }
  93.         return 0;
  94.     }
  95. void showMat(IntMat *m)
  96.     {
  97.         int i, j;
  98.  
  99.         printf("\nImprimindo a matriz: \n\n");
  100.  
  101.         for(i = 0; i < m->l; i++){
  102.             for(j = 0; j < m->c; j++)
  103.             printf(" %2d ", *(*((m->ptr) + i) + j));
  104.             printf("\n");
  105.         }
  106.         printf("\n");
  107.  
  108.     }
  109. void freeMat(IntMat *m)
  110.     {
  111.         int i;
  112.         for(i = 0; i < m->l; i++)
  113.         free(*((m->ptr) + i));
  114.         free(m);
  115.     }
  116.  
  117. int main() {
  118.   IntMat a, b, c;
  119.   /* Ler as dimensões das matrizes a e b */
  120.   readInfo(&a);
  121.   readInfo(&b);
  122.   /* Testar se as matrizes podem ser multiplicadas */
  123.   /* Se não for possível, a função retorna FALSO e o porgrama acaba */
  124.   /* Se for possível, a função calcula as dimensões da matriz c, e retorna VERDADEIRO */
  125.   if(!testDim(&a, &b, &c)) {
  126.     printf("\nAs matrizes nao podem ser multiplicadas.\n");
  127.     exit(1);
  128.   }
  129.   /* Se VERDADEIRO, alocar matrizes a, b e c */
  130.   /* Se não for possível alocar qualquer uma das matrizes o programa acaba */
  131.   if(!allocMat(&a)) {
  132.     printf("Erro na alocação de memória.\n");
  133.     exit(1);
  134.   }
  135.   if(!allocMat(&b)) {
  136.     printf("Erro na alocação de memória.\n");
  137.     exit(1);
  138.   }
  139.   if(!allocMat(&c)) {
  140.     printf("Erro na alocação de memória.\n");
  141.     exit(1);
  142.   }
  143.  
  144.  
  145.   /* Ler os elementos das matrizes a e b */
  146.   readMat(&a);
  147.   readMat(&b);
  148.   /* Multiplicar as matrizes a e b */
  149.   multiMat(&a, &b, &c);
  150.   /* Mostrar as três matrizes em forma tabular */
  151.   showMat(&a);
  152.   showMat(&b);
  153.   showMat(&c);
  154.   /* Liberar a memória a locada */
  155.   freeMat(&a);
  156.   freeMat(&b);
  157.   freeMat(&c);
  158.  
  159.   return 0;
  160. }
Advertisement
Add Comment
Please, Sign In to add comment