gpt4 book ai didi

c - 在c中使用指针进行矩阵乘法

转载 作者:行者123 更新时间:2023-11-30 16:20:44 25 4
gpt4 key购买 nike

#include<stdio.h>

int mul(int *a[3][3], int *b[3][3]);

int i,j,k,*c[3][3],*a[3][3],*b[3][3];

int main()
{
printf("enter the elements of 1st 3*3 matrix:A");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter the elements of 1st 3*3 matrix:B");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&b[i][j]);
}
}
mul(a,b);
printf("result=");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("\t%d\t",*c[i][j]);
}
printf("\n");
}
}

int mul(int *a[3][3], int *b[3][3])
{
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
*c[i][j]=0;
for(k=0;k<3;k++)
{
c[i][j] = *a[i][k] * *b[k][j] + *c[i][j];
}
}
}
}

我正在尝试使用指针进行矩阵乘法,但没有得到任何结果。

我在谷歌上搜索过,但无法理解其中任何一个。

而且他们中的任何一个都与我的截然不同。

请帮助我。

最佳答案

手动将矩阵与指针相乘时,您可能希望将它们表示为一个 array[],而不是 array[] 的 vector 。

这样,移动指针就更容易了。考虑this implementation :

void matmul(double *dest, const double *lhs, const double *rhs,
size_t rows, size_t mid, size_t cols) {
memset(dest, 0, rows * cols * sizeof(double));

for (size_t i = 0; i < rows; ++i) {
const double *rhs_row = rhs;
for (size_t j = 0; j < mid; ++j) {
for (size_t k = 0; k < cols; ++k) {
dest[k] += lhs[j] * rhs_row[k];
}
rhs_row += cols;
}
dest += cols;
lhs += mid;
}
}

关于c - 在c中使用指针进行矩阵乘法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55193411/

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