Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 以从1到n²的螺旋顺序填充nxn矩阵;_C - Fatal编程技术网

C 以从1到n²的螺旋顺序填充nxn矩阵;

C 以从1到n²的螺旋顺序填充nxn矩阵;,c,C,我本来应该用函数按从1到n²的螺旋顺序填充一个nxn矩阵,然后打印结果,但我不知道为什么我的代码无法运行,有人能帮忙吗 其原理是创建不同的函数,每个函数以不同的时间间隔填充矩阵,然后在主程序中调用这些函数,主程序将这些函数打印成螺旋矩阵 #包括 #包括 /*初始化整个程序的数组和变量*/ INTA[5][5],顶部、底部、左侧、右侧; int FillRowForward(int A[5][5],int top,int left,int right,int z) /*从左到右填充矩阵顶部的函数

我本来应该用函数按从1到n²的螺旋顺序填充一个nxn矩阵,然后打印结果,但我不知道为什么我的代码无法运行,有人能帮忙吗

其原理是创建不同的函数,每个函数以不同的时间间隔填充矩阵,然后在主程序中调用这些函数,主程序将这些函数打印成螺旋矩阵


#包括
#包括
/*初始化整个程序的数组和变量*/
INTA[5][5],顶部、底部、左侧、右侧;
int FillRowForward(int A[5][5],int top,int left,int right,int z)
/*从左到右填充矩阵顶部的函数*/
{左=0;
对于(顶部=左侧,右侧=0;右侧=0;右侧--)
{
A[左][右-1]=A[左][右]+z;
}
返回一个[左][右-1];
}
int fillcolumn向下(int A[5][5],int top,int bottom,int left,int z)
/*从上到下填充最后一列*/
{
左=0;
对于(顶部=左侧,底部=4;顶部=1;底部--)
{
A[bottom-1][top]=A[bottom][top]+z
}
返回一个[底部][顶部];
}
int main()
{
int i,j,k=1;

然而(k这里有几个问题:

  • 我认为不需要全局变量,因此可以在
    main()
    函数中定义所有内容

  • 正如前面指出的,“在
    main()
    中,您只提供函数声明,而不调用它们。”

  • main()
    函数中有一个无限循环,因为您从未增加var k

  • 尽量避免在函数中使用数字,而是使用变量或符号常量。这样的小项目没有区别,但在较大的项目中,您很容易混淆它们,而且如果您想更改,您必须更改每个值,等等

  • 你的函数没有完成它们应该做的事情。你可以这样写(我找到了我的旧程序,并对它进行了一些修改):

    #包括
    无效打印材料(内部材料[][5],内部材料m,内部材料n)
    {
    int i,j;
    对于(i=0;i=l;i--)
    {
    mat[m-1][i]=计数器++;
    }
    m--;
    }
    if(l=k;i--)
    {
    mat[i][l]=计数器++;
    }
    l++;
    }
    }
    }
    int main()
    {
    int mat[5][5];
    int n=5;
    螺旋(垫,n,n);
    打印垫(垫,n,n);
    返回0;
    }
    

    由于您需要创建不同的函数,因此可以尝试将此
    spiral()
    函数划分为几个较小的函数,这是一个很好的练习。

    您有许多问题,如:

    int FillRowForward(int A[5][5],int top,int left,int right,int k);
    
    不是一个函数调用,而且你永远不会改变
    k
    ,也就是说,你有一个无止境的循环

    此解决方案使用变量
    direction
    跟踪填充矩阵的当前方向

    #include <stdio.h>
    
    #define ARRSIZE 10
    
    int main()
    {
        int A[ARRSIZE][ARRSIZE] = { 0 };
        int i=0, j=0;
        int direction = 0;
    
        for(int k = 1; k <= (ARRSIZE*ARRSIZE); ++k)
        {
            A[i][j] = k;
            
            switch (direction)
            {
                case 0 :  // Go rigth
                    if (((j + 1) == ARRSIZE) || (A[i][j+1] != 0))
                    {
                        // Switch direction
                        direction = 1;
                        ++i;
                    }
                    else
                    {
                        ++j;
                    }
                    break;
                case 1 :  // Go down
                    if (((i + 1) == ARRSIZE) || (A[i+1][j] != 0))
                    {
                        // Switch direction
                        direction = 2;
                        --j;
                    }
                    else
                    {
                        ++i;
                    }
                    break;
                case 2 :  // Go left
                    if (((j - 1) == -1) || (A[i][j-1] != 0))
                    {
                        // Switch direction
                        direction = 3;
                        --i;
                    }
                    else
                    {
                        --j;
                    }
                    break;
                case 3 :  // Go up
                    if (((i - 1) == -1) || (A[i-1][j] != 0))
                    {
                        // Switch direction
                        direction = 0;
                        ++j;
                    }
                    else
                    {
                        --i;
                    }
                    break;
            }
        }
    
        for(i=0; i<ARRSIZE; i++)
        {
            for(j=0; j<ARRSIZE; j++)
                printf("%4d",A[i][j]);
            printf("\n");
        }
    
        return 0;
    }
    

    您需要调用函数。在
    main()
    中,您只提供函数的声明,而不调用它们。它们实际上应该位于代码的顶部,在
    #include
    下面,因为它们通过将它们放在
    main()中不提供任何功能
    。你应该从一个函数开始,从
    main
    调用一次。然后打印矩阵以查看部分答案。当这项功能正常工作时,你可以编写并测试下一个函数。一旦四个函数都正常工作以绘制外环,你可以再次调用这些函数,然后看看结果如何。
    >而(将全局变量
    A
    top
    bottom
    left
    right
    隐藏在不同函数定义中的同名变量中称为“隐藏”,很容易导致混淆。这些全局变量应在
    main()中定义
    。如果您使用GCC,您可以添加
    -Wshadow
    以获取有关隐藏变量的警告。这也是尽可能避免使用全局变量的另一个原因。
    #include <stdio.h>
    
    #define ARRSIZE 10
    
    int main()
    {
        int A[ARRSIZE][ARRSIZE] = { 0 };
        int i=0, j=0;
        int direction = 0;
    
        for(int k = 1; k <= (ARRSIZE*ARRSIZE); ++k)
        {
            A[i][j] = k;
            
            switch (direction)
            {
                case 0 :  // Go rigth
                    if (((j + 1) == ARRSIZE) || (A[i][j+1] != 0))
                    {
                        // Switch direction
                        direction = 1;
                        ++i;
                    }
                    else
                    {
                        ++j;
                    }
                    break;
                case 1 :  // Go down
                    if (((i + 1) == ARRSIZE) || (A[i+1][j] != 0))
                    {
                        // Switch direction
                        direction = 2;
                        --j;
                    }
                    else
                    {
                        ++i;
                    }
                    break;
                case 2 :  // Go left
                    if (((j - 1) == -1) || (A[i][j-1] != 0))
                    {
                        // Switch direction
                        direction = 3;
                        --i;
                    }
                    else
                    {
                        --j;
                    }
                    break;
                case 3 :  // Go up
                    if (((i - 1) == -1) || (A[i-1][j] != 0))
                    {
                        // Switch direction
                        direction = 0;
                        ++j;
                    }
                    else
                    {
                        --i;
                    }
                    break;
            }
        }
    
        for(i=0; i<ARRSIZE; i++)
        {
            for(j=0; j<ARRSIZE; j++)
                printf("%4d",A[i][j]);
            printf("\n");
        }
    
        return 0;
    }
    
       1   2   3   4   5   6   7   8   9  10
      36  37  38  39  40  41  42  43  44  11
      35  64  65  66  67  68  69  70  45  12
      34  63  84  85  86  87  88  71  46  13
      33  62  83  96  97  98  89  72  47  14
      32  61  82  95 100  99  90  73  48  15
      31  60  81  94  93  92  91  74  49  16
      30  59  80  79  78  77  76  75  50  17
      29  58  57  56  55  54  53  52  51  18
      28  27  26  25  24  23  22  21  20  19