Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct {
- int **ptr;
- int l;
- int c;
- } IntMat;
- void readInfo(IntMat *m)
- {
- // função para ler a quantidade de linhas e colunas da matriz
- printf("\nMatriz \n");
- printf("Linhas: ");
- scanf("%d", &m->l);
- printf("Colunas: ");
- scanf("%d", &m->c);
- //aqui testamos se as informações são válidas (positivas)
- if(m->l < 1 || m->c < 1)
- {
- printf("Erro: Parametro invalido\n");
- exit(1);
- }
- }
- int testDim(IntMat *m1, IntMat *m2, IntMat *m3)
- {
- //função para testar se com as dimensões informadas, podemos multiplicar a matriz
- if(m1->c == m2->l)
- {
- m3->l = m1->l;
- m3->c = m2->c;
- printf("\n\nMatriz 3 = [%d,%d]\n", m3->l , m3->c);
- return 1;
- }
- else{return 0;}
- }
- int allocMat(IntMat *m)
- {
- int i;
- //alocando as linhas
- if((m->ptr = (int **) calloc(m->l, sizeof(int *))) == NULL) {
- printf("** Erro: Memoria Insuficiente **\n");
- return 0;
- }
- //alocando as colunas
- for(i = 0; i < m->l; i++) {
- if((*(m->ptr + i) = (int *) calloc(m->c, sizeof(int))) == NULL) {
- printf("** Erro: Memoria Insuficiente **\n");
- return 0;
- }
- }
- }
- void readMat(IntMat *m)
- {
- // aqui lemos os elementos da matriz
- int i, j;
- for (i = 0; i < m->l; i++){
- for (j = 0; j < m->c; j++){
- printf("Digite o elemento (%d,%d) da Matriz : ", i , j);
- scanf("%d", (*(m->ptr + i) + j));
- printf("\n");
- }
- }
- printf("Elementos preenchidos!\n\n");
- }
- int multiMat(IntMat *m1, IntMat *m2, IntMat *m3)
- {
- int i, j,k;
- for(i = 0; i < m1->l; i++) {
- for(j = 0; j < m2->c; j++) {
- for(k = 0; k < m1->c; k++) {
- *(*((m3->ptr) + i) + j) = *(*((m3->ptr) + i) + j) + (*(*((m1->ptr) + i) + k)) * (*(*((m2->ptr) + k) + j));
- }
- }
- }
- return 0;
- }
- void showMat(IntMat *m)
- {
- int i, j;
- printf("\nImprimindo a matriz: \n\n");
- for(i = 0; i < m->l; i++){
- for(j = 0; j < m->c; j++)
- printf(" %2d ", *(*((m->ptr) + i) + j));
- printf("\n");
- }
- printf("\n");
- }
- void freeMat(IntMat *m)
- {
- int i;
- for(i = 0; i < m->l; i++)
- free(*((m->ptr) + i));
- free(m);
- }
- int main() {
- IntMat a, b, c;
- /* Ler as dimensões das matrizes a e b */
- readInfo(&a);
- readInfo(&b);
- /* Testar se as matrizes podem ser multiplicadas */
- /* Se não for possível, a função retorna FALSO e o porgrama acaba */
- /* Se for possível, a função calcula as dimensões da matriz c, e retorna VERDADEIRO */
- if(!testDim(&a, &b, &c)) {
- printf("\nAs matrizes nao podem ser multiplicadas.\n");
- exit(1);
- }
- /* Se VERDADEIRO, alocar matrizes a, b e c */
- /* Se não for possível alocar qualquer uma das matrizes o programa acaba */
- if(!allocMat(&a)) {
- printf("Erro na alocação de memória.\n");
- exit(1);
- }
- if(!allocMat(&b)) {
- printf("Erro na alocação de memória.\n");
- exit(1);
- }
- if(!allocMat(&c)) {
- printf("Erro na alocação de memória.\n");
- exit(1);
- }
- /* Ler os elementos das matrizes a e b */
- readMat(&a);
- readMat(&b);
- /* Multiplicar as matrizes a e b */
- multiMat(&a, &b, &c);
- /* Mostrar as três matrizes em forma tabular */
- showMat(&a);
- showMat(&b);
- showMat(&c);
- /* Liberar a memória a locada */
- freeMat(&a);
- freeMat(&b);
- freeMat(&c);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment