c - 矩阵乘法分割错误?

标签 c

需要帮助来确定程序未生成矩阵乘积的原因。这个想法是:

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

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

4

3

2

14 9 3

2 11 15

0 12 17

5 2 3

12 25

9 10

8 5

两个矩阵的输出乘积

273 455

243235

244205

102160

#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;
}

最佳答案

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]);
        }
    }
}

关于c - 矩阵乘法分割错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12041905/

相关文章:

c - Loadrunner 分析 : How can the 90th percentile be higher than the average?

c++ - 混淆确定数组的元素数

c - for循环没有结束

c - 如何以零纳秒实现每秒的计时器释放?

c - OpenPBS 构建错误 - 未知类型名称 'jid_t'

c - 内存泄漏(自由函数不起作用)

c++ - openMP 新手,有什么建议可以将以下代码与 openMP 并行吗?

在for循环中创建线程: all threads passed same "i" value

c - 主要不认识我的功能

在 C 中使用 MPI 进行代码并行化