gpt4 book ai didi

在 C 中使用 for 循环创建矩阵

转载 作者:太空宇宙 更新时间:2023-11-04 08:16:59 25 4
gpt4 key购买 nike

我正在尝试使用 C 中的 for 循环创建 2 个矩阵。

矩阵 A 是 [18x16],其中每个元素 A[i,j] = i + j,对于 (i=1,...,18; j=1,...,16)
矩阵B是[16x18],其中每个元素B[i,j] = i + 2j, for (i=1,...,16; j=1,...,18)

根据我的 Ubuntu(虚拟机)终端输出,我缺少矩阵 A 的整行,并且矩阵 A 和 B 在两个特定位置都有不正确的元素:A[17,16] 和 B[15, 18],恭敬地(请见下图)。

代码如下,如有帮助,将不胜感激。

#include <stdio.h>

int A[18][16];
int B[16][18];
int i, j;

int main(void)
{
// Create the A matrix
for(i = 1; i < 18; i++){

for(j = 1; j < 16; j++){
A[i][j] = i + j;
printf("%d ", A[i][j]);
} // End inner loop

printf("\n");
} // End outer loop

printf("\n");

// Create the B matrix
for(i = 1; i < 16; i++){

for(j = 1; j < 18; j++){
B[i][j] = i + (2*j);
printf("%d ", B[i][j]);
} // End inner loop

printf("\n");
} // End outer loop
}

终端输出:

enter image description here

最佳答案

您的 for 循环应该分别从 i = 0j = 0 开始。

关于在 C 中使用 for 循环创建矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35263850/

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