Fill 2d array in spiral order in c

淺唱寂寞╮ 提交于 2021-02-17 07:07:30

问题


I'm doing program where I enter the number from keyboard. Then 2d array is created, and it's filled in spiral order till this number. All elements after the number will be equal to 0; The function fills the array in spiral order (to right -> down -> left -> up).

Code is:

#include <stdio.h>
#include <time.h>

void spiral(int array[100][100], int m, int n, int s)
{
int size, b, x = 0, y = 1, num = 1;
 size = m*n;
for (num=1;num<=size+1;num++)
{  
    for (b = x; b < n; b++)
    {
        if (num <=s) {
            array[x][b] = num;
            num++;
        }
        else array[x][b] = 0;
    }

    if (num == size + 1)
    {
        break;
    }

    for (b = y; b < m; b++)
    {
        if (num <=s) {
            array[b][n - 1] = num;
            num++;
        }
        else array[b][n - 1] = 0;
    }

    if (num == size + 1)
    {
        break;
    }

    y++;
    n--;

    for (b = n - 1; b > x; b--)
    {
        if (num <= s) {
            array[m - 1][b] = num;
            num++;
        }
        else array[m - 1][b] = 0;
    }

    if (num == size + 1)
    {
        break;
    }

    for (b = m - 1; b > x; b--)
    {
        if (num <= s) {
            array[b][x] = num;
            num++;
        }
        else array[b][x] = 0;
    }
    x++;
    m--;
}
}

int main()
    {   
  int m, n, s, array[100][100];
  srand(time(NULL));
  //m=3;
  // n=4;
  m = 2 + rand() % 5;
  n = 2 + rand() % 5;
  //memset(array, 0, sizeof(array[0][0]) * 10 * 10);
  printf("enter the number \n");
  scanf("%i", &s);
  spiral(array, m, n, s);

 for (int i = 0; i < m; i++)
{                                                                          
 for (int j = 0; j < n; j++)
{
    printf("%i\t", array[i][j]);
}
   printf("\n");
}

return (0); }

However it doesn't work always correctly.

For example when i enter 15 and the program generates 4*3 array the output is`

   1       2       3
   10      12      4
   9       -858993460      5
   8       7       6`

However expected output is 1 2 3 10 11 4 9 12 5 8 7 6

Or when i enter 15 and the program generates 4*5 array the output is 1 2 3 4 5 14 0 0 0 6 13 0 0 0 7 12 11 10 9 8

And the expected output is 1 2 3 4 5 14 15 0 0 6 13 0 0 0 7 12 11 10 9 8

I can't find whats wrong with this code.


回答1:


Try this:

void printArray(int array[10][10], int m, int n) {

    for (int i = 0; i < m; i++)
    {
        for (int j = 0; j < n; j++)
        {
            printf("%i\t", array[i][j]);
        }
        printf("\n");
    }
    printf("\n");
}

void spiral(int array[10][10], int m, int n, int s)
{
    int size, b, x = 0, y = 1, num = 1;
    size = m*n;
    while ( num <= s )
    {

        for (b = x; b < n; b++)
        {
            if (num <= s) {
                array[x][b] = num;
                num++;
            } else array[x][b] = 0;
        }

        if (num == size + 1)
        {
            break;
        }

        for (b = y; b < m; b++)
        {
            if (num <= s) {
                array[b][n - 1] = num;
                num++;
            } else array[b][n - 1] = 0;
        }

        if (num == size + 1)
        {
            break;
        }

        y++;
        n--;

        for (b = n - 1; b > x; b--)
        {
            if (num <= s) {
                array[m - 1][b] = num;
                num++;
            } else array[m - 1][b] = 0;
        }

        if (num == size + 1)
        {
            break;
        }


        for (b = m - 1; b > x; b--)
        {
            if (num <= s) {
                array[b][x] = num;
                num++;
            } else array[b][x] = 0;
        }
        x++; 
        m--;
    }
}



int main()
{
    int m, n, s, array[10][10];
    srand(time(NULL));
    m = 2 + rand() % 5;
    n = 2 + rand() % 5;
    memset(array, 0, sizeof(array[0][0]) * 10* 10);
    printf("enter the number \n");
    scanf("%i", &s);
    spiral(array, m, n, s);

    printArray(array, m, n);


    return (0);
}

Before you had some weird for loop on top of your spiral function. The num++ in it interfered with the fact that you already increased num by one and made it skip the number the next time it would write in the uppermost line. I changed it to a while loop that runs until num>s and it seems to work for me now.

Note that I just added printArray for easier debugging.



来源:https://stackoverflow.com/questions/43663745/fill-2d-array-in-spiral-order-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!