我一直在尝试在C中实现一个程序,其中我有两个2D数组(使用指针创建),我必须将它们相乘(如乘法矩阵)并将结果存储到第三个数组。数组的尺寸由用户给出。我已经完成了代码,但结果却是错误的。我的配方有问题吗?我想用功能进行阅读和打印。 例如,当我输入n = 2时,m = 2且k = 2。对于输入的矩阵的每个元素
A(0)(0) = 1, A(0)(1) = 2, A(1)(0) = 3, A(1)(1) = 4
and
B(0)(0) = 1,> B(0)(1) = 2, B(1)(0) = 3, B(1)(1) = 4.
输出应该是
C(0)(0) = 7, C(0)(1) = 10, C(1)(0) = 15 and C(1)(1) = 22.
而是输出
C(0)(0) = 7, C(0)(1) = 10, C(1)(0) = 33, C(1)(1) = 46.
我希望不难理解,我不允许发布图片:(
#include <stdio.h>
#include <stdlib.h>
int **read(int **x, int i, int j);
int **prod(int **x, int **y, int n, int m, int k);
void print(int **x, int r, int c);
int main()
{
int n, m, k, i, j;
printf("Give n, m and k: ");
scanf("%d%d%d", &n, &m, &k);
int **A, **B, **C;
A = (int **)malloc(n*sizeof(int *));
for (i = 0; i < n; i++)
*(A+i) = (int *)malloc(k*sizeof(int));
B = (int **)malloc(k*sizeof(int *));
for (i = 0; i < k; i++)
*(B+i) = (int *)malloc(m*sizeof(int));
C = (int **)malloc(n*sizeof(int *));
for (i = 0; i < n; i++)
*(C+i) = (int *)malloc(m*sizeof(int)
for (i = 0; i < n; i++)
for (j = 0; j < k; j++)
A = read(A, i, j);
for (i = 0; i < k; i++)
for (j = 0; j < m; j++)
B = read(B, i, j);
C = prod(A, B, n, m, k);
print(C, n, m);
}
int **read(int **x, int i, int j)
{
printf("Give value to store in cell [%d][%d]: ", i, j);
scanf("%d", &x[i][j]);
return x;
}
int **prod(int **x, int **y, int n, int m, int k)
{
int i, j, l, sum;
int **res;
for (i = 0; i < n; i++)
for (j = 0; j < k; j++)
{
sum = 0;
for (l = 0; l < m; l++)
sum = sum + (x[i][l]*y[l][j]);
res[i][j] = sum;
}
return res;
}
void print(int **x, int r, int c)
{
int i, j;
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j ++)
printf("C[%d][%d]: %d\t", i, j, x[i][j]);
printf("\n");
}
}
答案 0 :(得分:0)
首先理解没有指针的矩阵
没有指针的矩阵乘法
int r1,r2,c1,c2;
int i,j,sum,k;
void matrixmul()
{
printf("Getting values for matrices");
printf("\n---------------------------");
printf("\n Enter the row for Matrix A: ");
scanf("%d",&r1);
printf("\n Enter the column for Matrix A: ");
scanf("%d",&c1);
printf("\n Enter the row for Matrix B: ");
scanf("%d",&r2);
printf("\n Enter the column for Matrix A: ");
scanf("%d",&c2);
int a[r1][c1];
int b[r2][c2];
int c[r1][c2];
if (!((c1==r2)&&(r1==c2)))
{
printf("Row column miss matching so cannot do matrix multiplication");
}
else
{
printf("\n A Matrix");
printf("\n ---------");
for (i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf("\n Enter %d and %d value: ",i,j);
scanf("%d",&a[i][j]);
}
}
printf("\n B Matrix");
printf("\n ---------");
for (i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
printf("\n Enter %d and %d value: ",i,j);
scanf("%d",&b[i][j]);
}
}
}
for(i=0;i<c1;i++)
{
for(j=0;j<r2;j++)
{
for (k=0;k<r2;k++)
{
sum = sum + a[i][k]*b[k][j];
}
c[i][j] = sum;
sum = 0;
}
}
printf("\nResultant Matrix");
printf("\n----------------\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
void main()
{
matrixmul();
}
<强>输出强>
输入Matrix A:2
的行输入Matrix A:3
列输入Matrix B的行:3
输入Matrix B:2
列矩阵
输入0和0值:1
输入0和1值:2
输入0和2值:3
输入1和0值:3
输入1和1值:2
输入1和2值:11
B矩阵
输入0和0值:4
输入0和1值:5
输入1和0值:7
输入1和1值:8
输入1和0值:9
输入1和1值:10
结果矩阵
45 51
125 141
使用指针的矩阵 - 在此程序中使用指针变量声明2D并获取2D数组的值并显示值
void main()
{
int r = 2, c = 3, i, j, count;
int *a[r];//while declaring the array as pointer its number of rows are specified
for (i=0; i<r; i++)
{
a[i] = (int *)malloc(c * sizeof(int));//Allocate memory space for each row for 'c' columns
}
count = 0;
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
scanf("%d",&a[i][j] );
}
}
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
printf("%d\t ", a[i][j]);
}
printf("\n");
}
}
<强>输出强>
3
2
1
1
2
3
3 2 1
1 2 3
以下是使用指针
的最终完整矩阵乘法void input()
{
int r1, c1,r2,c2, i, j,k,sum;
printf("\n Enter the row for Matrix A: ");
scanf("%d",&r1);
printf("\n Enter the column for Matrix A: ");
scanf("%d",&c1);
printf("\n Enter the row for Matrix B: ");
scanf("%d",&r2);
printf("\n Enter the column for Matrix A: ");
scanf("%d",&c2);
if (!((c1==r2)&&(r1==c2)))
{
printf("Row column miss matching so cannot do matrix multiplication");
}
else
{
int *a[r1];
int *b[r2];
int *c[r1];
for (i=0; i<r1; i++)
{
a[i] = (int *)malloc(c1 * sizeof(int));
}
for (i=0; i<r2; i++)
{
b[i] = (int *)malloc(c2 * sizeof(int));
}
for (i=0; i<r1; i++)
{
c[i] = (int *)malloc(c2 * sizeof(int));
}
printf("\n A Matrix");
printf("\n ----------");
for (i = 0; i < r1; i++)
{
for (j = 0; j < c1; j++)
{
printf("\n Enter the %d and %d value:",i,j);
scanf("%d",&a[i][j] );
}
}
printf("\n B Matrix");
printf("\n ----------");
for (i = 0; i < r2; i++)
{
for (j = 0; j < c2; j++)
{
printf("\n Enter the %d and %d value:",i,j);
scanf("%d",&b[i][j]);
}
}
for(i=0;i<c1;i++)
{
for(j=0;j<r2;j++)
{
for (k=0;k<r2;k++)
{
sum = sum + a[i][k]*b[k][j];
}
c[i][j] = sum;
sum = 0;
}
}
printf("\n Resultant Matrix");
printf("\n------------------");
for (i = 0; i < r1; i++)
{
for (j = 0; j < c2; j++)
{
printf("%d\t ", c[i][j]);
}
printf("\n");
}
}
}
void main() {
input();
}
<强>输出强>
输入Matrix A:2
的行输入Matrix A:3
列输入Matrix B的行:3
输入Matrix B:2
列矩阵
输入0和0值:1
输入0和1值:2
输入0和2值:3
输入1和0值:3
输入1和1值:2
输入1和2值:11
B矩阵
输入0和0值:4
输入0和1值:5
输入1和0值:7
输入1和1值:8
输入2和0值:9
输入2和1值:10
结果矩阵
45 51
125 141
逐一运行上面的程序了解它然后按照你的要求将最终程序转换成函数,谢谢