# Magic square

• 06-01-2011
ardavirus
Magic square
My current exercise, as I am trying to grasp the basics of C is to create a magic square.
Here is the code of what seems to be a successful project:
Code:

```int main(void) {         short size, col, row, i;                 printf("\nThis program creates a \"magic square\" of specified size.");         printf("\nThe size must be an odd number.");         printf("\nBeyond size 99 some numbers will have no space between.");         printf("\nEnter size: ");         scanf(" %d", &size);                 short square[size][size];  /* Declare a two-dimensional array of size x size */                 for (row = 0; row < size; ++row)          /*******************************************/                 for (col = 0; col < size; ++col)  /* Give every element in the array value 0 */                         square[row][col] = 0;    /*******************************************/                                 row = 0;              /***************************************/         col = size / 2;        /* Place the first number in the array */         square[row][col] = 1;  /***************************************/                                 for (i = 2; i <= size * size; ++i)         {                                                                                                if (--row < 0)            /*****************************************************/                         row = (size - 1);  /* Evaluate the indexes to the next position,        */                                           /* wrapping around to the opposite side, everytime  */                 if (++col > (size - 1))    /* an index goes outside the bounds of the array.    */                         col = 0;          /*****************************************************/                                         if (square[row][col] != 0)  /* Check if a number is already placed in the new position */                 {                         if (++row > (size - 1))    /***********************************************/                                 row = 0;          /* Evaluate indexes to the previous position,  */                                                   /* wrapping to the opposite side, everytime an */                         if (--col < 0)            /* index goes outside the bounds of the array  */                                 col = (size - 1);  /***********************************************/                                                         while (square[row][col] != 0)    /*****************************************/                                 if (++row > (size - 1))  /* Evaluate indexes to the first 0 value */                                         row = 0;        /* element, below the last placed number */                 }                                        /*****************************************/                                 square[row][col] = i;  /* Place the next number in the array */         }                         for (row = 0; row < size; ++row)         {                 printf("\n");                 for (col = 0; col < size; ++col)                         printf("%5d", square[row][col]);         }                 return 0; }```
Since I am at a novice level, I tend to complicate my life and thus my code. So I was wondering if there is another, more simple way, to write such project. No need to write me a whole program, just a few pointers of what could have done better.

When I grow up, my priorities when writing something should be, speed, memory usage, and readability of code (not in random order). Having that in mind and assuming that no input value bigger than 99 will be used, is the use of short int, instead of int correct?

• 06-01-2011
anduril462
Quote:

Originally Posted by ardavirus
Since I am at a novice level, I tend to complicate my life and thus my code. So I was wondering if there is another, more simple way, to write such project. No need to write me a whole program, just a few pointers of what could have done better.

I don't know all the algorithms for generating magic squares, but your method is certainly simple. I'm not sure you're going to get any simpler, but you can Google around for some other methods and see.

Quote:

When I grow up, my priorities when writing something should be, speed, memory usage, and readability of code (not in random order). Having that in mind and assuming that no input value bigger than 99 will be used, is the use of short int, instead of int correct?
Your list is backwards. Your first priority should be readability. Worrying about speed and memory usage before they're a problem is called pre-optimization, and usually results in bad, overly complex code, meaning more room for errors. Make your code clean and readable for yourself (you'll be amazed at how much you forget in a few months) and others (this is especially important on any collaborative project). Only worry about speed and size when you know that the run time and memory usage are excessive.

As for your question on short vs int, if the largest number you need is 99, you can fit that in a char. A char contains values up to 127 if signed, 255 if unsigned. But for a 99x99 magic square, you will need to generate numbers from 1 to 9801. For that you definitely need a short.

A point on comments, you should put blocks of comments above the corresponding block of code, not off to the side. Comments at the end of a line with code on it should be brief and pertain only to that line. Besides, this saves you the hassle of aligning your comment boxes. Also, and this may be more a matter of personal taste, but the whole box idea is overkill when you're annotating lines of code. The box should be reserved for file-level comments or comments above a function definition. You should have a good editor with syntax highlighting that makes comments in your code stand out and not need a box to mark it off.

Quote: