#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0

    Can this be done using loops?


    printing out a square like this?


    16 15 14 13

    5 4 3 12

    6 1 2 11

    7 8 9 10
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1045
    A 4x4 square of arbitrary integers? Yes, that can be done with loops.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0
    Originally Posted by Jacques1
    A 4x4 square of arbitrary integers? Yes, that can be done with loops.
    it's the numbers 1-16 in decreasing order and written clockwise
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    33
    I like challenges :-)

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    enum direction {RIGHT, UP, LEFT, DOWN};
    
    int main(int argc, char **argv) {
      int n, k, row, col, ulam[1000][1000] = {0};
      enum direction dir = LEFT;
    
      /* get parameter from command line */
      if (argc == 1) {
        fprintf(stderr, "Specify a size\nProgram draw a square of that size\n");
        exit(EXIT_FAILURE);
      }
      n = atoi(argv[1]);
      n *= n;
    
      /* validate parameter */
      if (n < 4) {
        fprintf(stderr, "Specify a size of 2 or more\n");
        exit(EXIT_FAILURE);
      }
      if (n > 499 * 499) {
        fprintf(stderr, "Size too large\n");
        exit(EXIT_FAILURE);
      }
    
      /* initialize spiral with the first 3 numbers at the right position */
      ulam[500][500] = 1;
      ulam[500][501] = 2;
      ulam[499][501] = 3;
    
      /* position for next number */
      row = 499; col = 500;
    
      /* put numbers in spiral */
      for (k = 4; k <= n; k++) {
        ulam[row][col] = k;
        switch (dir) {
          default: fprintf(stderr, "ERROR: This didn't happen\n");
                   exit(EXIT_FAILURE);
                   break;
          case RIGHT: if (!ulam[row - 1][col]) dir = UP; break;
          case UP: if (!ulam[row][col - 1]) dir = LEFT; break;
          case LEFT: if (!ulam[row + 1][col]) dir = DOWN; break;
          case DOWN: if (!ulam[row][col + 1]) dir = RIGHT; break;
        }
        switch (dir) {
          default: fprintf(stderr, "ERROR: This didn't happen\n");
                   exit(EXIT_FAILURE);
                   break;
          case RIGHT: col++; break;
          case UP: row--; break;
          case LEFT: col--; break;
          case DOWN: row++; break;
        }
      }
    
      /* display elements that are not zero */
      for (row = 0; row < 1000; row++) {
        int line = 0;
        for (col = 0; col < 1000; col++) {
          if (ulam[row][col]) {
            line = 1;
            printf("%4d ", ulam[row][col]);
          }
        }
        if (line) puts("");
      }
    
      /* voila */
      return 0;
    }
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    [code]Code tags[/code] are essential for python code and Makefiles!
  10. #6
  11. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,109
    Rep Power
    1802
    The quality of this answer is directly proportional to the quality of the question. :rolleyes:

    Code:
    #include <stdio.h>
    int main( void )
    {
        int a[] = {16,15,14,13,5,4,3,12,6,1,2,11,7,8,9,10} ;
        for( int i = 0; i < sizeof(a) / sizeof(*a); i++ )
        {
            printf( "%3d", a[i] ) ;
            if( i % 4 == 3)
            {
                putchar{ '\n' ) ;
            }
        }
    
        return 0
    }
    Last edited by clifford; November 29th, 2012 at 04:30 PM.
  12. #7
  13. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,109
    Rep Power
    1802
    More seriously, the following works for the 4x4 square in the example and any even-sided square. It does not work where SQUARE_SIZE is not even; I'll leave that as an exercise for the reader.

    c Code:
     
    #include <stdio.h>
     
    #define SQUARE_SIZE 4  // only works for even value sizes 
    #define SQUARE_MAX (SQUARE_SIZE * SQUARE_SIZE)
     
    int main( void ) 
    {
        int square[SQUARE_SIZE][SQUARE_SIZE] = {{0},{0}};
        int row = SQUARE_SIZE / 2 ;
        int col = SQUARE_SIZE / 2 - 1 ;
        int i = 1 ;
        int w = 1 ;
        int direction = 1 ;
     
        // Fill Square...
        square[row][col] = i++ ;
     
        while( i <= SQUARE_MAX )
        {
            int s ;
            for( s = 0; i <= SQUARE_MAX && s < w; s++ )
            {
                col += direction ;
                square[row][col] = i++ ;
            }
     
            direction = -direction ;
     
            for( s = 0; i <= SQUARE_MAX && s < w; s++ )
            {
                row += direction ;
                square[row][col] = i++ ;
            }
     
            w++ ;
        }
     
        // Draw square...
        for( row = 0; row < SQUARE_SIZE; row++ )
        {
            for( col = 0; col < SQUARE_SIZE; col++ )
            {
                printf( "%3d", square[row][col] ) ;
            }
            putchar( '\n' ) ;
        }
     
        return 0 ;
    }

IMN logo majestic logo threadwatch logo seochat tools logo