gpt4 book ai didi

c - 3x3 数组 = 10 个数字

转载 作者:太空宇宙 更新时间:2023-11-04 05:07:38 27 4
gpt4 key购买 nike

我有这个代码

#include <math.h>
#include <stdio.h>

const int n = 3;
const int s = 3;
int getm(int mat[n][s]);
int printm(int mat[n][s]);

int main()
{
int m[n][s];
getm(m);
printm(m);
return 0;
}

int getm(int mat[n][s])
{
for(int x = 0;x < n;x++)
{
for (int y = 0;y<s;y++)
{
scanf("%i ", &mat[x][y]);
}
}
return 0;
}
int printm(int mat[n][s])
{
for(int x = 0;x<n;x++)
{
for(int y = 0;y<s;y++)
{
printf("%i ", mat[x][y]);
if(y==(s-1))
{
printf("\n");
}
}
}
}

应该要求 9 个数字来制作 3x3 矩阵数组,但它实际上要求 10 个数字,printm 运行良好 - 仅打印 9 个数字。错误在哪里?

最佳答案

我认为问题在于 %i 之后的空格:您不需要第十个数字,但您的代码无论如何都在请求它,因为它等待在第九个数字之后获得一个空格。

另外,您可以通过删除 if 来稍微优化您的打印​​代码:

for(int x = 0;x<n;x++)
{
for(int y = 0;y<s;y++)
{
printf("%i ", mat[x][y]);
}
printf("\n");
}

关于c - 3x3 数组 = 10 个数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12654029/

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