0
我想写一个C程序来打印一个魔方。但是,我在建造广场时遇到了一个错误。任何人都可以帮助弄清楚是什么导致这些错误?以下是我的相关代码,和我的输出:打印二维数组打印垃圾(魔方)
代码:
int main(void) {
int size = 0;
int r, c;
int i = 1;
printf("What is the size of the square: ");
scanf("%d", &size);
int square[size][size];
int range = size * size;
r = 0;
c = size/2;
do {
square[r][c] = i;
i++;
r--;
c++;
if ((r == -1) && (c == size)) {
r = r + 2;
c--;
} else if (r == -1) {
r = size - 1;
} else if (c == size) {
c = 0;
} else if (square[r][c] != 0) {
r = r + 2;
c--;
}
} while (i < range);
for (r = 0; r < size; r++) {
for (c = 0; c < size; c++) {
printf("%d \t", square[r][c]);
}
printf("\n");
}
return 0;
}
输出:
What is the size of the square: 3
-4196312 1 0
3 -4196352 -13339222
4 -13360148 2
谢谢你的回答!但是,在使用'memset'代码后,我现在得到以下错误:'警告:内置函数'memset''的不兼容隐式声明''错误:'int''之前的预期表达式 – user41419
它是'string中的函数.h'。把'#include'放在你的第一行代码中。 –
texasbruce
再次感谢!我已经这样做了,并且删除了第一个错误,但是编译器仍然会在'int''之前说'错误:预期的表达式。我认为它是指'memset'表达式中的'sizeOf(int)'。 – user41419