[英]C program to fill an n x n matrix in a spiral form
我正在实现一个 C 程序,该程序使用函数以螺旋形式填充方阵。 这是我已经做过的:
#include <stdio.h>
#include <conio.h>
const N = 5;
int top = 0;
int bottom = N - 1;
int right = 0;
int left = N -1;
int main(){
int z = 1 /*N = 5*/;
int Array[100][100];
while (z <= (N*N))
{
FillRowForward(Array, z);
FillColumnDownward(Array, z);
FillRowBackward(Array, z);
FillColumnUpward(Array, z);
}
printf("Two dimensional array elements: \n");
for (int i = 0; i < N; i++)
{
// printf("\t");
for (int j = 0; j < N; j++)
{
printf("%d \t", Array[i][j]);
}
printf("\n");
}
return 0;
}
/*Definition of functions*/
int FillRowForward(int A[][N], /*int top, int left, int right,*/ int Z)
{
for (int i = right; i <= left; i++)
{
A[top][i] = Z++;
}
}
int FillColumnDownward(int A[][N], /*int top, int bottom, int right,*/ int Z)
{
for (int j = top + 1; j <= bottom; j++)
{
A[j][bottom] = Z++;
}
}
int FillRowBackward(int A[][N], /*int bottom, int left, int right,*/ int Z)
{
for (int i = left - 1; i >= top; i--)
{
A[bottom][i] = Z++;
}
}
int FillColumnUpward(int A[][N], /*int top, int bottom, int right,*/ int Z)
{
for (int j = bottom - 1; j >= top + 1; j--)
{
A[j][left] = Z++;
}
}
第一个 function 应该填充第一行( FillRowForward ),下一个应该向下填充第一列,依此类推,直到填充所有矩阵。 但是当我运行它时,它只显示一个黑色和空白的屏幕。 没有 output。 请在这方面需要一些帮助!
目前尚不清楚应该以哪种方式填充该矩阵。 我想Z应该一直在增加。 如果这是真的,那么你应该传递/接受 z 的指针。 或者,由于您已经拥有多个全局变量,因此 z 可能是全局变量。
主要原因是您在主 function ( int [100][100] ) 中创建的数组类型与各个函数的参数类型 ( int[][N] ) 不匹配。 但是,您的代码中还有更多问题。
我建议不要在 C 中使用原生 2D arrays,这很麻烦。 您可以很容易地使用一维数组,只需将坐标从 x、y 转换为 i,反之亦然。
这是您重写为一维方法的代码:
#include <stdio.h>
int Get(int a[], int n, int y, int x)
{
return a[y * n + x];
}
void Set(int a[], int n, int y, int x, int value)
{
a[y * n + x] = value;
}
int FillRowForward(int a[], int n, int z, int value)
{
for (int x = z; x < n - z - 1; x++)
Set(a, n, z, x, value++);
return value;
}
int FillColumnDownward(int a[], int n, int z, int value)
{
int x = n - z - 1;
for (int y = z; y < n - z - 1; y++)
Set(a, n, y, x, value++);
return value;
}
int FillRowBackward(int a[], int n, int z, int value)
{
int y = n - z - 1;
for (int x = n - z - 1; x > z; x--)
Set(a, n, y, x, value++);
return value;
}
int FillColumnUpward(int a[], int n, int z, int value)
{
int x = z;
for (int y = n - z - 1; y > z; y--)
Set(a, n, y, x, value++);
return value;
}
int Spiral(int a[], int n, int i)
{
for (int z = 0; z < n / 2; z++)
{
i = FillRowForward(a, n, z, i);
i = FillColumnDownward(a, n, z, i);
i = FillRowBackward(a, n, z, i);
i = FillColumnUpward(a, n, z, i);
}
if (n % 2 == 1)
Set(a, n, n / 2, n / 2, i++);
return i;
}
void Print(int a[], int n)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
printf("%10d", Get(a, n, i, j));
}
printf("\n");
}
}
int main()
{
int N = 5;
int Array[N * N];
Spiral(Array, N, 0);
printf("Two dimensional array elements: \n");
Print(Array, N);
return 0;
}
在您开始考虑实际程序在做什么之前,需要修复此代码中的许多基本问题。 我怀疑您使用的是 Turbo C 或类似的一些过时的废话,因为此代码无法在现代标准 C 中编译。
const N = 5;
不要使用隐式 int,这应该是const int N = 5;
int bottom = N - 1;
不是 integer 常量表达式,所以这会导致问题。 绝对没有理由在文件 scope 中声明所有这些变量,但如果这样做,则需要更改为#define N 5
。int Array[100][100];
在堆栈上。 这样做可能会导致堆栈溢出,这很难追踪而且很烦人。i <= left;
这样的循环条件是腥的。 与其将left
初始化为某个N-1
,不如将其设置为 N ,然后将循环条件写为i < left
。z
以便调用者可以知道它已被修改。int [][5]
的 function 不能将int [100][100]
作为参数,当这些 arrays “衰减”为指向其第一个元素的指针时,它会导致指针类型不兼容。 通过使用指向 VLA 的指针并传递数组大小来解决此问题: void function (size_t x, size_t y, int array[x][y])
。一旦你解决了所有这些基本的 C 问题,你就可以专注于实际的算法。 如果在修复上述所有问题后仍然无法正常工作,请使用修改后的代码发布一个单独的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.