0

## 這C語言的程式碼那些地方出錯？

```c

```
vuj8104 iT邦新手 5 級 ‧ 2020-10-04 11:19:51 檢舉
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int size;
int recursion(int [][size],int,int,int,int);
int main()
{
int i,j,row,col,count;
printf("Enter the size of the square:");
scanf("%d",&size);
if(size == 5 || size == 7 || size == 9)
{
int arr[size][size];
for(i = 0 ; i < size ; i ++)
{
for(j = 0 ; j < size ; j++)
{
arr[i][j] = 0;
}
}
srand(time(NULL));
i = rand() % size;
j = rand() % size;
arr[i][j] = 1;
recursion(arr[size][size],size*size,i,j,2);
for(i = 0 ; i < size ; i ++)
{
for(j = 0 ; j < size ; j++)
{
printf("%2d ",arr[i][j]);
}
printf("\n");
}
}
return 0;
}
int recursion(int arr[][size],int num,int i,int j,int count)
{
int row,col;
if(count <= num)
{
row = (i - 1 < 0) ? (size - 1) : (i - 1);
col = (j - 1 < 0) ? (size - 1) : (j - 1);
if(arr[row][col] != 0)
{
i = (++i) % size;
}
else
{
i = row;
j = col;
}
arr[i][j] = count;
return recursion(arr[size][size],num-1,i,j,count++);
}
}
vuj8104 iT邦新手 5 級 ‧ 2020-10-04 11:36:07 檢舉
```
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int size;
int recursion(int [][size],int,int,int,int);
int main()
{
int i,j,row,col,count;
printf("Enter the size of the square:");
scanf("%d",&size);
if(size == 5 || size == 7 || size == 9)
{
int arr[size][size];
for(i = 0 ; i < size ; i ++)
{
for(j = 0 ; j < size ; j++)
{
arr[i][j] = 0;
}
}
srand(time(NULL));
i = rand() % size;
j = rand() % size;
arr[i][j] = 1;
recursion(arr[size][size],size*size,i,j,2);
for(i = 0 ; i < size ; i ++)
{
for(j = 0 ; j < size ; j++)
{
printf("%2d ",arr[i][j]);
}
printf("\n");
}
}
return 0;
}
int recursion(int arr[][size],int num,int i,int j,int count)
{
int row,col;
if(count <= num)
{
row = (i - 1 < 0) ? (size - 1) : (i - 1);
col = (j - 1 < 0) ? (size - 1) : (j - 1);
if(arr[row][col] != 0)
{
i = (++i) % size;
}
else
{
i = row;
j = col;
}
arr[i][j] = count;
return recursion(arr[size][size],num-1,i,j,count++);
}
}
```
vuj8104 iT邦新手 5 級 ‧ 2020-10-04 11:39:48 檢舉

vuj8104 iT邦新手 5 級 ‧ 2020-10-04 14:16:07 檢舉

https://ithelp.ithome.com.tw/m/questions/10200536