How can I fill a 2D array spirally in C? Example/problem

Viewed 94

so I've been struggling with this example for a good hour now and I can't even begin to process how should I do this.

Write a program that, for given n and m, forms a matrix as described. The matrix should be m x m, and it's filled "spirally" with it's beginning in the upper left corner. The first value in the matrix is the number n. It's repeated until the "edge" of the matrix, at which point the number increments. After the number 9 goes 0. 0 ≤ n ≤ 9, 0 ≤ m ≤ 9

Example for n=3, m=5

2 Answers

Some time ago I had made a function to display the numbers 1 to n on an odd-sized grid. The principle was to start from the center and to shift by ;

  • x = 1
  • x box on the right
  • x box on the bottom
  • x++
  • x box on the left
  • x box at the top
  • x++

With this simple algorithm, you can easily imagine to maybe start from the center of your problem and decrement your value, it seems easier to start from the center.


Here is the code that illustrates the above solution, to be adapted of course for your problem, it's only a lead.

#define WE 5

void    clock(int grid[WE][WE])
{
    int count;
    int i;
    int reach;
    int flag;
    int tab[2] = {WE / 2, WE / 2}; //x , y

    count = 0;
    flag = 0;
    i = 0;
    reach = 1;
    grid[tab[1]][tab[0]] = count;
    for (int j = 0; j < WE - 1 && grid[0][WE - 1] != pow(WE, 2) - 1; j++)
        for (i = 0; i < reach && grid[0][WE - 1] != pow(WE, 2) - 1; i++, reach++)
        {
            if(flag % 2 == 0)
            {
                for(int right = 0 ; right < reach ; right++, tab[0]++, count++, flag = 1)
                    grid[tab[1]][tab[0]] = count;
                if(reach < WE - 1)
                    for(int bottom = 0; bottom < reach; bottom++, count++, tab[1]++)
                        grid[tab[1]][tab[0]] = count;
            }
            else
            {
                for(int left = 0; left < reach; left++, count++, tab[0]--, flag = 0)
                    grid[tab[1]][tab[0]] = count;
                for(int top = 0; top < reach; top++, tab[1]--, count++)
                    grid[tab[1]][tab[0]] = count;
            }
        }
}

I finally solved it. If anybody's interested, here's how I did it:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

//Fills the row number "row" with the number n
int fillRow(int m, int n, int arr[m][m], int row)
{
    int j;

    for(j=0;j<m;j++)
    {
        if(arr[row][j] == -1 || arr[row][j] == n-1) arr[row][j] = n;
    }

}

//Fills the column number "col" with the number n
int fillCol(int m, int n, int arr[m][m], int col)
{
    int i;

    for(i=0;i<m;i++)
    {
        if(arr[i][col] == -1 || arr[i][col] == n-1) arr[i][col] = n;
    }

}

int main()
{

    int n, m, i, j, r=1, c=1, row=-1, col=-1;

    scanf("%d %d",&n, &m);

    int arr[m][m];
    
    
    //Fill array with -1 everywhere
    for(i=0;i<m;i++)
    {
        for(j=0;j<m;j++)
        {
            arr[i][j] = -1;
        }

    }
    
    //Calculate which row/column to fill (variables row/col)
    //Fill row then column then row than column...
    for(i=0;i<2*m;i++)
    {
        if(i%2==0)
        {
            row = (r%2==0) ? m-r/2 : r/2;
            fillRow(m, n, arr, row);
            n++;
            r++;
        }
        else if(i%2==1)
        {
            col = (c%2==0) ? c/2-1 : m-c/2-1;
            fillCol(m, n, arr, col);
            n++;
            c++;
        }
    }
    
    //If an element is larger than 9, decrease it by 10
    //Prints the elements
    for(i=0;i<m;i++)
    {
        for(j=0;j<m;j++)
        {
            if(arr[i][j]>9) arr[i][j] -=10;
            printf("%d ",arr[i][j]);
        }
        printf("\n");
    }

    return 0;
}
Related