Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
November 29th, 2012, 10:37 AM
 nawsx
Registered User

Join Date: Nov 2012
Posts: 5
Time spent in forums: 1 h 34 m 53 sec
Reputation 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
November 29th, 2012, 11:30 AM
 Jacques1
You have been warned

Join Date: Jul 2012
Posts: 3,084
Time spent in forums: 2 Months 3 Weeks 3 Days 23 h 33 m 42 sec
Reputation Power: 1063
A 4x4 square of arbitrary integers? Yes, that can be done with loops.

#3
November 29th, 2012, 12:22 PM
 nawsx
Registered User

Join Date: Nov 2012
Posts: 5
Time spent in forums: 1 h 34 m 53 sec
Reputation Power: 0
Quote:
 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

#4
November 29th, 2012, 03:12 PM
 bdb
Contributing User

Join Date: Aug 2012
Posts: 156
Time spent in forums: 1 Week 15 h 48 m 11 sec
Reputation 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;
}```

#5
November 29th, 2012, 04:11 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,124
Time spent in forums: 1 Month 3 Weeks 2 Days 4 h 49 m 23 sec
Reputation Power: 455
__________________
[code]Code tags[/code] are essential for python code!

#6
November 29th, 2012, 04:39 PM
 clifford
Contributing User

Join Date: Aug 2003
Location: UK
Posts: 4,961
Time spent in forums: 1 Month 4 Days 2 h 35 m 15 sec
Reputation Power: 1801
The quality of this answer is directly proportional to the quality of the question.

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 05:30 PM.

#7
November 30th, 2012, 04:21 PM
 clifford
Contributing User

Join Date: Aug 2003
Location: UK
Posts: 4,961
Time spent in forums: 1 Month 4 Days 2 h 35 m 15 sec
Reputation Power: 1801
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:
 Original - 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 ;}
```

 Viewing: Dev Shed Forums > Programming Languages > C Programming > Can this be done using loops?