gpt4 book ai didi

char() 数组的表示

转载 作者:行者123 更新时间:2023-11-30 16:56:14 25 4
gpt4 key购买 nike

我必须创建一个程序,它需要用户输入 1 到 99 之间的奇数,并创建一个幻方,我已经成功完成了。

#include <stdio.h>

int main()
{
int n;

printf("\nThis programs creates a magic squares of a specified size.\n");
printf("The size must be an odd number between 1 and 99.\n");
printf("Enter the size of magic square: ");
scanf("%d", &n);


int magicsq[99][99];

int row = 0;
int col = (n - 1) / 2;
magicsq[row][col] = 1;

int i;
for(i = 2; i <= n * n; i++)
{
row = (row + n - 1) % n;

/* printf("i = %d\n", i);
printf("row %d\n", row);

col = (col % n); */
col = (col + 1) % n;

/* printf("col %d\n\n", col); */

if(magicsq[row][col] != 0)
{
row = (n + row + 2) % n;
col = (n + col - 1) % n;
/* printf("n = %d ; row = %d ; col = %d\n", n, row, col); */
}

magicsq[row][col] = i;
}




printf("\n");
int j;
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
printf("%5d", magicsq[i][j]);
}
printf("\n");
}
return 0;
}

我遇到了另一个问题,要求我引入两个函数,即 void create_magic_square(int n, char magic_square[99][99])void print_magic_square(int n, char magic_square[99][99])

#include <stdio.h>


void create_magic_square(int n, char magic_square[99][99]);
void print_magic_square(int n, char magic_square[99][99]);

int main()
{
int n;
char **magic_square;

printf("\nThis programs creates a magic squares of a specified size.\n");
printf("The size must be an odd number between 1 and 99.\n");
printf("Enter the size of magic square: ");
scanf("%d", &n);

create_magic_square(n, magic_square[99][99]);
print_magic_square(n, magic_square[99][99]);

return 0;
}

void create_magic_square(int n, char magic_square[99][99])
{

int *magicsq[][];
magic_square[99][99] = magicsq[][];
int row = 0;
int col = (n - 1) / 2;
magicsq[row][col] = 1;

int i;
for(i = 2; i <= n * n; i++)
{
row = (row + n - 1) % n;

printf("i = %d\n", i);
printf("row %d\n", row);

/* col = (col % n); */
col = (col + 1) % n;

printf("col %d\n\n", col);

if(magicsq[row][col] != 0)
{
row = (n + row + 2) % n;
col = (n + col - 1) % n;
printf("n = %d ; row = %d ; col = %d\n", n, row, col);
}

magicsq[row][col] = i;
}
}



void print_magic_square(int n, char magic_square[99][99])
{
printf("\n");
int j;
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
printf("%5d", magicsq[i][j]);
}
printf("\n");
}
}

编译后,我在 char 类型数组的声明和参数的使用方面充满了大量错误。我在 google 上搜索了 char() 参数和类型,但无法合并到我的程序中。

我是 c 语言的新手,所以建设性的批评值得赞赏,如果我做错了什么,可以帮助我更好地学习。

语言:c99;编译器:gcc

最佳答案

当然,您的代码中有很多与指针和数组相关的错误,并且这些错误无法在此处的单个答案中完全解释。我建议您正确学习指针。在尝试做你想做的事情之前,需要彻底理解指针和数组之间的关系,多维数组。

关于char() 数组的表示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39984701/

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