arrayscmatrixspiral

Fill matrix with two numbers in a spiral


In the middle of matrix is number x. Matrix is filled with x in a spiral, while the rest is filled with y.

Example:

int x=4, y=6, v=7, s=9;

OUTPUT:

6 6 6 6 6 6 6 6 4
4 4 4 4 4 4 4 6 4
4 6 6 6 6 6 4 6 4
4 6 4 4 x 6 4 6 4
4 6 4 6 6 6 4 6 4
4 6 4 4 4 4 4 6 4
4 6 6 6 6 6 6 6 4

Spiral filling looks like --> this

#include <stdio.h>
int main() {
  int v = 7, s = 9, x = 4, y = 6, mat[7][9], i, j, l, k;
  for (i = 0; i < v; i++)
    for (j = 0; j < s; j++)
      mat[i][j] = y;
  for (i = 0; i < v; i++) {
    for (j = 0; j < s; j++) {
      // spiral goes here
      // mat[i][j] = x;
    }
  }
  for (i = 0; i < v; i++) {
    for (j = 0; j < s; j++) 
      printf("%4d", mat[i][j]);
    printf("\n");
  }
  return 0;
}

EDIT: After applying suggestions, now the whole matrix is filled with y. How to make it fill spiral with x?


Solution

  • Below is a program for constructing a spiral from the center in an s by v matrix.

    Here's the algorithm:

    1. The matrix is ​​filled with y values
    2. Calculating the center, the cursor cx, cy is set there, 0 is written
    3. The spiral drawing direction is set, dir, 1 - left, 2 - down, 3 - right, 4 - up, first 1
    4. Draw 1 step by setting x value to matrix
    5. If the value in the matrix for dir+1 (if dir+1=5 then dir=1) = y (empty) then dir is incremented by +1. If not (center (0) or already drawn (x)) then dir remains the same. If beyond the edge of the matrix, then dir is incremented +1 as if empty, but nothing is drawn.
    6. Step 4 is repeated s*v/2 times

    The program is compiled with the gcc compiler.

    Output:

    cx - 4, xy - 3
    6 6 6 6 6 6 6 6 4
    4 4 4 4 4 4 4 6 4
    4 6 6 6 6 6 4 6 4
    4 6 4 4 0 6 4 6 4
    4 6 4 6 6 6 4 6 4
    4 6 4 4 4 4 4 6 4
    4 6 6 6 6 6 6 6 4

    Required:

    6 6 6 6 6 6 6 6 4
    4 4 4 4 4 4 4 6 4
    4 6 6 6 6 6 4 6 4
    4 6 4 4 x 6 4 6 4
    4 6 4 6 6 6 4 6 4
    4 6 4 4 4 4 4 6 4
    4 6 6 6 6 6 6 6 4

    #include <stdio.h>
    
    
    /*
     Increase dir
    */
    int incdir (int dir) {
    
        // direction, 1 - left, 2 - down, 3 - right, 4 - up
        dir++;
        if (dir==5) dir=1;
        return dir;
        
    }
    
    
    /*
     Is coord in matrix
    */
    int inmat (int y, int x, int v, int s) {
    
        // direction, 1 - left, 2 - down, 3 - right, 4 - up
        if (x<0 || x>=s) return 0;
        if (y<0 || y>=v) return 0;
        return 1;
    
    }
    
    
    /*
     Value in some direction
    */
    int dirval(int cx, int cy, int v, int s, int dir, int mat[v][s]) {
    
        // direction, 1 - left, 2 - down, 3 - right, 4 - up
        
        if (dir==1) {
            if (!inmat(cy, cx-2, v, s)) return -1;
            return mat[cy][cx-2];
            
        }
        if (dir==2) {
            if (!inmat(cy+2, cx, v, s)) return -1;
            return mat[cy+2][cx];
        }
        if (dir==3) {
            if (!inmat(cy, cx+2, v, s)) return -1;
            return mat[cy][cx+2];
        }
        if (dir==4) {
            if (!inmat(cy-2, cx, v, s)) return -1;
            return mat[cy-2][cx];
        }
    
    }
    
    
    int main() {
      int v = 7, s = 9, x = 4, y = 6, mat[7][9], i, j, l, k;
      
      // cursor
      int cx, cy;
      cx=((int)s/2);
      cy=((int)v/2);
      printf("cx - %4d, xy - %4d\n", cx, cy);
      
      
      
      int dir=1; // direction, 1 - left, 2 - down, 3 - right, 4 - up
      
      int dv;
      
      for (i = 0; i < v; i++)
        for (j = 0; j < s; j++)
          mat[i][j] = y;
      
      mat[cy][cx]=0; // start
      // spiral goes here
      for (i = 0; i < s*v/2; i++) {
    
    
        if (dir==1) {
            if (inmat(cy, cx-1, v, s)) mat[cy][cx-1]=x;
            if (inmat(cy, cx-2, v, s)) mat[cy][cx-2]=x;
            cx=cx-2;
        }
        if (dir==2) {
            if (inmat(cy+1, cx, v, s)) mat[cy+1][cx]=x;
            if (inmat(cy+2, cx, v, s)) mat[cy+2][cx]=x;
            cy=cy+2;
        }
        if (dir==3) {
            if (inmat(cy, cx+1, v, s)) mat[cy][cx+1]=x;
            if (inmat(cy, cx+2, v, s)) mat[cy][cx+2]=x;
            cx=cx+2;
        }
        if (dir==4) {
            if (inmat(cy-1, cx, v, s)) mat[cy-1][cx]=x;
            if (inmat(cy-2, cx, v, s)) mat[cy-2][cx]=x;
            cy=cy-2;
        }
    
        dv=dirval(cx, cy, v, s, incdir(dir), mat);
        if (dv==y || dv==-1) dir=incdir(dir);
    
      }
    
      for (i = 0; i < v; i++) {
        for (j = 0; j < s; j++) 
          printf("%4d", mat[i][j]);
        printf("\n");
      }
      return 0;
    }