gpt4 book ai didi

c - 矩阵乘法分割错误?

转载 作者:行者123 更新时间:2023-11-30 17:58:41 25 4
gpt4 key购买 nike

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

输入 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com