2D数组的乘法,使用指向指针的指针 [英] Multiplication of 2D arrays, using pointers to pointers
问题描述
我一直在尝试用C语言实现一个程序,其中我有两个2D数组(使用指针创建),并且我必须将它们相乘(如相乘矩阵)并将结果存储到第三个数组中.阵列的尺寸由用户指定.我已经完成了代码,但是结果却是错误的.我的配方有问题吗?我想使用功能进行读取和打印. 例如,当我输入n = 2时,m = 2且k = 2.对于矩阵的每个元素,我输入
I've been trying to implement a program in C, where i have two 2D arrays (created with pointers) and i have to multiply them (like multiplying matrices) and store the results to a third array. The dimensions of the arrays are given by the user. I've done the code but i'm getting wrong results. Is there something wrong with my formula? I wanted to do the reading and printing with functions. For example, when i input n = 2, m = 2 and k =2. For each element of the matrix i input
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.
我希望这不难理解,还不允许发布图像:(
I hope it's not hard to understand, i'm not allowed to post an image yet :(
#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");
}
}
推荐答案
首先了解没有指针的矩阵
first understand the matrix without pointers
没有指针的矩阵乘法
int r1,r2,c1,c2;
int r1,r2,c1,c2;
int i,j,sum,k;
int i,j,sum,k;
void matrixmul()
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()
void main()
{
matrixmul();
}
输出
输入矩阵A的行:2
输入矩阵A的列:3
输入矩阵B的行:3
输入矩阵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
Enter the 1 and 1 value:10
结果矩阵
45 51
125141
45 51
125 141
带有指针的矩阵-在此程序中,使用指针变量声明2D并获取2D数组的值并显示值
Matrix with pointers-in this program 2D is declared using pointer variable and get the values for the 2D array and display the values
void main()
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
以下是使用指针的最终完整矩阵乘法
The following is the final complete matrix multiplication using pointers
void input()
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() {
void main() {
input();
}
输出
输入矩阵A的行:2
输入矩阵A的列:3
输入矩阵B的行:3
输入矩阵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
Enter the 2 and 1 value:10
结果矩阵
45 51
125141
45 51
125 141
一个个地运行上面的程序,了解它,然后将最终程序转换为所需的功能,谢谢
run the above program one by one understand it then convert the final program into function as per your required,Thank you
这篇关于2D数组的乘法,使用指向指针的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!