Home > Mobile >  C program to fill an n x n matrix in a spiral form
C program to fill an n x n matrix in a spiral form

Time:01-05

I am implementing a C program using functions to fill a square matrix in a spiral form. Here is what I already did:

#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  ;
    }

}

The first function is supposed to fill the first row (FillRowForward), the next is supposed to fill the first column downward and so on until all the matrix is filled. But when I run it only shows a black and blank screen. No output. Need some help on this please!

CodePudding user response:

It is not clear in which manner that matrix should be populated. I suppose the Z should be increasing all the way. If that is true, then you should pass/ accept pointer of z. Or, since you already have number of global variables, z could be global.

CodePudding user response:

The main reason is that the type of the array that you create in the main function ( int [100][100] ) does not match the argument type of individual functions ( int[][N] ). However, there are more issues in your code.

I recommend not using native 2D arrays in C, it's a pain in the ass. You can pretty easily use 1D array and just transform coordinates from x, y to i and vice versa.

Here is your code rewritten to 1D approach:

#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("d", 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;
}
  •  Tags:  
  • Related