Advertisement
Guest User

Untitled

a guest
May 27th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main(void){
  5.     int r1, c1, r2, c2, r3, c3;
  6.     int r, c, aux;
  7.     float **m1, **m2, **m3;
  8.     float sum;
  9.  
  10.     do{
  11.         do{
  12.           printf("Rows for Matrix M: ");
  13.           scanf("%d", &r1);
  14.           printf("Columns for Matrix M:");
  15.           scanf("%d", &c1);
  16.             if(r1 < 1 || c1 < 1)
  17.            printf("Invalid dimensions for Matrix M. Type again.\n");
  18.        }while(r1 < 1 || c1 < 1);
  19.  
  20.        do{
  21.          printf("Rows for Matrix m: ");
  22.          scanf("%d", &r2);
  23.          printf("Columns for Matrix m:");
  24.          scanf("%d", &c2);
  25.            if(r2 < 1 || c2 < 1)
  26.            printf("Invalid dimensions for Matrix m. Type again.\n");
  27.        }while(r2 < 1 || c2 < 1);
  28.  
  29.        if(c1!=r2)
  30.        printf("\nError! Rows for M1 and Columns for M2 must match.\n");
  31.  
  32.    }while(c1!=r2);
  33.  
  34.    r3 = r1;
  35.    c3 = c2;
  36.  
  37.    if((m1 = (float **) malloc(r1*sizeof(float *)))==NULL){
  38.        printf("\nInsufficient Memory.");
  39.        exit(1);
  40.    }
  41.  
  42.    for(r = 0; r < r1; r++){
  43.     if((*(m1+r) = (float *) malloc(c1*sizeof(float)))==NULL){
  44.     printf("\nInsufficient Memory.");
  45.        exit(1);
  46.    }
  47.  
  48.    if((m2 = (float **) malloc(r2*sizeof(float *)))==NULL){
  49.        printf("\nInsufficient Memory.");
  50.        exit(1);
  51.    }
  52.  
  53.    for(r = 0; r < r2; r++){
  54.     if((*(m2+r) = (float *) malloc(c2*sizeof(float)))==NULL){
  55.     printf("\nInsufficient Memory.");
  56.        exit(1);
  57.    }
  58.  
  59.    if((m3 = (float **) calloc(r3,sizeof(float *)))==NULL){
  60.        printf("\nInsufficient Memory.");
  61.        exit(1);
  62.    }
  63.  
  64.    for(r = 0; r < r3; r++){
  65.     if((*(m2+r) = (float *) calloc(c3,sizeof(float)))==NULL){
  66.     printf("\nInsufficient Memory.");
  67.        exit(1);
  68.    }
  69.  
  70.    for(r = 0; r < r1; r++){
  71.        for(c = 0; c < c1; c++){
  72.            printf("M[%d][%d]: ", r, c);
  73.            scanf("%f", *(m1+r)+c);
  74.     }
  75.     printf("\n");
  76.    }
  77.  
  78.    for(r = 0; r < r2; r++){
  79.        for(c = 0; c < c2; c++){
  80.            printf("m[%d][%d]: ", r, c);
  81.            scanf("%f", *(m2+r)+c);
  82.     }
  83.     printf("\n");
  84.    }
  85.  
  86.    for(r = 0; r < r3; r++){
  87.        for(c = 0; c < c3; c++){
  88.     *(m3+(c3.r+c)) += (*m1+(c1.r+c))*(*m2+(c2.r+c));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement