Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- int main(void){
- int r1, c1, r2, c2, r3, c3;
- int r, c, aux;
- float **m1, **m2, **m3;
- float sum;
- do{
- do{
- printf("Rows for Matrix M: ");
- scanf("%d", &r1);
- printf("Columns for Matrix M:");
- scanf("%d", &c1);
- if(r1 < 1 || c1 < 1)
- printf("Invalid dimensions for Matrix M. Type again.\n");
- }while(r1 < 1 || c1 < 1);
- do{
- printf("Rows for Matrix m: ");
- scanf("%d", &r2);
- printf("Columns for Matrix m:");
- scanf("%d", &c2);
- if(r2 < 1 || c2 < 1)
- printf("Invalid dimensions for Matrix m. Type again.\n");
- }while(r2 < 1 || c2 < 1);
- if(c1!=r2)
- printf("\nError! Rows for M1 and Columns for M2 must match.\n");
- }while(c1!=r2);
- r3 = r1;
- c3 = c2;
- if((m1 = (float **) malloc(r1*sizeof(float *)))==NULL){
- printf("\nInsufficient Memory.");
- exit(1);
- }
- for(r = 0; r < r1; r++){
- if((*(m1+r) = (float *) malloc(c1*sizeof(float)))==NULL){
- printf("\nInsufficient Memory.");
- exit(1);
- }
- if((m2 = (float **) malloc(r2*sizeof(float *)))==NULL){
- printf("\nInsufficient Memory.");
- exit(1);
- }
- for(r = 0; r < r2; r++){
- if((*(m2+r) = (float *) malloc(c2*sizeof(float)))==NULL){
- printf("\nInsufficient Memory.");
- exit(1);
- }
- if((m3 = (float **) calloc(r3,sizeof(float *)))==NULL){
- printf("\nInsufficient Memory.");
- exit(1);
- }
- for(r = 0; r < r3; r++){
- if((*(m2+r) = (float *) calloc(c3,sizeof(float)))==NULL){
- printf("\nInsufficient Memory.");
- exit(1);
- }
- for(r = 0; r < r1; r++){
- for(c = 0; c < c1; c++){
- printf("M[%d][%d]: ", r, c);
- scanf("%f", *(m1+r)+c);
- }
- printf("\n");
- }
- for(r = 0; r < r2; r++){
- for(c = 0; c < c2; c++){
- printf("m[%d][%d]: ", r, c);
- scanf("%f", *(m2+r)+c);
- }
- printf("\n");
- }
- for(r = 0; r < r3; r++){
- for(c = 0; c < c3; c++){
- *(m3+(c3.r+c)) += (*m1+(c1.r+c))*(*m2+(c2.r+c));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement