gpt4 book ai didi

c - C 中的 5x5 矩阵乘法

转载 作者:太空宇宙 更新时间:2023-11-04 00:33:35 24 4
gpt4 key购买 nike

我的作业一直卡在这个问题上。我已经做到了这一点,并且确定问题出在我的三个 for 循环中。问题直接说要使用 3 个 for 循环,所以我知道这可能只是一个逻辑错误。

#include<stdio.h>

void matMult(int A[][5],int B[][5],int C[][5]);
int printMat_5x5(int A[5][5]);

int main() {

int A[5][5] = {{1,2,3,4,6},
{6,1,5,3,8},
{2,6,4,9,9},
{1,3,8,3,4},
{5,7,8,2,5}};

int B[5][5] = {{3,5,0,8,7},
{2,2,4,8,3},
{0,2,5,1,2},
{1,4,0,5,1},
{3,4,8,2,3}};

int C[5][5] = {0};

matMult(A,B,C);

printMat_5x5(A);
printf("\n");

printMat_5x5(B);
printf("\n");

printMat_5x5(C);

return 0;

}

void matMult(int A[][5], int B[][5], int C[][5])
{
int i;
int j;
int k;

for(i = 0; i <= 2; i++) {
for(j = 0; j <= 4; j++) {
for(k = 0; k <= 3; k++) {
C[i][j] += A[i][k] * B[k][j];
}
}
}

}

int printMat_5x5(int A[5][5]){

int i;
int j;

for (i = 0;i < 5;i++) {
for(j = 0;j < 5;j++) {

printf("%2d",A[i][j]);
}

printf("\n");
}

}

编辑:这是问题,很抱歉没有第一时间发布。

Write a C function to multiply two five by five matrices. The prototype should read

void matMult(int a[][5],int b[][5],int c[][5]);

The resulting matrix product (a times b) is returned in the two dimensional array c (the third parameter of the function). Program your solution using three nested for loops (each generating the counter values 0, 1, 2, 3, 4) That is, DO NOT code specific formulas for the 5 by 5 case in the problem, but make your code general so it can be easily changed to compute the product of larger square matrices. Write a main program to test your function using the arrays

a:
1 2 3 4 6
6 1 5 3 8
2 6 4 9 9
1 3 8 3 4
5 7 8 2 5
b:
3 5 0 8 7
2 2 4 8 3
0 2 5 1 2
1 4 0 5 1
3 4 8 2 3

Print your matrices in a neat format using a C function created for printing five by five matrices. Print all three matrices. Generate your test arrays in your main program using the C array initialization feature.

最佳答案

你的printMat_5x5怎么来的?循环有条件 i < 5j < 5 ,但是你的 matMult循环有条件 i <= 2 , j <= 4 , 和 k <= 3

关于c - C 中的 5x5 矩阵乘法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2444621/

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