我有这个代码
#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");
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
shoud要求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");
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
501 次 |
| 最近记录: |