0

需要帮助来确定程序不产生矩阵产品的原因。这个想法是:

输入 n,m,p 后跟 2 个矩阵;

第一个是“m”列矩阵的“n”行。第二个是“p”列矩阵的“m”行。

4

3

2

14 9 3

2 11 15

0 12 17

5 2 3

12 25

9 10

8 5

2个矩阵的输出产品

273 455

243 235

244 205

102 160

#include<stdio.h>;
#include<malloc.h>;

int main(void) {
    int r1, r2, c1, c2, i, j, e;
    int temp, **mat1, **mat2, **ansMat;

    /*Accepting row and column values of 1st matrix*/
    //Enter no. of rows for 1st matrix
    scanf("%d", &r1);
    //Enter no. of cols for 1st matrix
    scanf("%d", &c1);

    temp = c1;
    r2=temp;
    /*Accepting row and column values of 2nd matrix*/
    //Enter no. of cols for 2nd matrix
    scanf("%d", &c2);

    if (c1 != r2) {
        printf("\nIncorrect combination!\n");
        return 1;
    }

    //Allocate memory for matrix1(# of rows)
    mat1 = (int**) malloc(r1 * sizeof(int*));

    printf("Enter element of 1st matrix:\n");
    for (i = 0; i < r1; i++) {
        mat1[i] = (int*) malloc(c1 * sizeof (int));
        for (j = 0; j < c1; j++) {
            printf("\nEnter element matrix 1[%d][%d]:", i, j);
            scanf("%d", &mat1[i][j]);
        }
    }

    //Allocate memory for matrix2(# of rows)
    mat2 = (int**) malloc(r2 * sizeof (int*));

    printf("\nEnter elements of 2nd matrix:\n");
    for (i = 0; i < r2; i++) {
        mat2[i] = (int*) malloc(c2 * sizeof (int));
        for (j = 0; j < c2; j++) {
            printf("\n\tEnter element matrix 2[%d][%d]:", i, j);
            scanf("%d", &mat2[i][j]);
        }
    }

    //Print the first matrix
    printf("\nFirst Matrix:\n");
    for (i = 0; i < r1; i++) {
        for (j = 0; j < c1; j++) {
            printf("%d ", mat1[i][j]);
        }
        printf("\n");
    }

    //Print the second matrix
    printf("\nSecond Matrix:\n");
    for (i = 0; i < r2; i++) {
        for (j = 0; j < c2; j++) {
            printf("%d ", mat2[i][j]);
        }
        printf("\n");
    }

    //Allocate memory for solution matrix(# of rows)
    ansMat = (int**) malloc(r1 * sizeof (int*));

    //Allocate memory for solution matrix(# of columns)
    for (i = 0; i < c2; i++) {
        ansMat[i] = (int*) malloc(c2 * sizeof (int));
    }

    //Matrix multiplication
    for (i = 0; i < r1; i++) {
        for (j = 0; j < c2; j++) {
            ansMat[i][j] = 0;
            for (e = 0; e < r2; e++) {
                ansMat[i][j] += mat1[i][e] * mat2[e][j]);
            }
        }
    }
    //Print matrix solution
    for (i = 0; i < r1; i++) {
        for (j = 0; j < c2; j++) {
            printf("%d ", ansMat[i][j]);
        }
    }
    return 0;
}
4

1 回答 1

0
int temp, *mat1, *mat2, *ansMat;

...

//Allocate memory for matrix1(# of rows)
mat1 = (int*) malloc(r1 * c1 * sizeof(int));

printf("Enter element of 1st matrix:\n");
for (i = 0; i < r1; i++) {
    for (j = 0; j < c1; j++) {
        printf("\nEnter element matrix 1[%d][%d]:", i, j);
        scanf("%d", &mat1[i*j]);
    }
}

...

//Allocate memory for matrix2(# of rows)
mat2 = (int*) malloc(r2 * c2 * sizeof (int));

printf("\nEnter elements of 2nd matrix:\n");
for (i = 0; i < r2; i++) {
    for (j = 0; j < c2; j++) {
        printf("\n\tEnter element matrix 2[%d][%d]:", i, j);
        scanf("%d", &mat2[i*j]);
    }
}

...

//Allocate memory for solution matrix(# of rows)
ansMat = (int*) malloc(r1 * c2 * sizeof (int));

//Matrix multiplication
for (i = 0; i < r1; i++) {
    for (j = 0; j < c2; j++) {
        ansMat[i*j] = 0;
        for (e = 0; e < r2; e++) {
            ansMat[i*j] += mat1[i*e] * mat2[e*j]);
        }
    }
}
于 2012-08-20T17:44:21.100 回答