Like Tree4Likes
  • 4 Post By anduril462

Magic square

This is a discussion on Magic square within the C Programming forums, part of the General Programming Boards category; My current exercise, as I am trying to grasp the basics of C is to create a magic square. Here ...

  1. #1
    Charming Registered User
    Join Date
    May 2011
    Posts
    49

    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?

    Thank you very much for your time and your valuable advices
    Last edited by ardavirus; 06-01-2011 at 08:40 AM.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,443
    Quote Originally Posted by ardavirus View Post
    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.

    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.

    Thank you very much for your time and your valuable advices
    You're welcome. You're off to a great start, so good luck.

  3. #3
    Charming Registered User
    Join Date
    May 2011
    Posts
    49
    @anduril462 Thank you very much for your valuable advices. I admit that the comments were an overkill but i made them this way in purpose just to show what I was making step by step (usually I draw on a paper what I want to do before I start a project and the comments were just those drawings), in case my way of thinking was faulty or too complex, so someone more expert (the 9.999% in this board) could correct me. Next time comment will go over

    I also agree with you that speed and memory are not a factor for programs like that, but I was just asking so I can understand the basics about how the machine really works. Using short seemed a fine choice from memory usage point of view, but maybe the speed could suffer because of some internal conversion type that I ignored.

    I understand my questions seem stupid (and probably they are), but so far you were very patient and helpful in this board and I tend to exploit such kindness, also a mild OCD disorder could justify some of my posts

    Thank you again for your time to help me !!!!
    Last edited by ardavirus; 06-01-2011 at 07:22 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 4 x 4 Magic Square
    By amorvisa in forum C Programming
    Replies: 2
    Last Post: 10-17-2007, 11:27 PM
  2. Magic Square and Tic Tac Toe
    By curlious in forum Game Programming
    Replies: 3
    Last Post: 07-28-2003, 05:50 PM
  3. magic square
    By miki in forum C++ Programming
    Replies: 3
    Last Post: 05-09-2002, 09:26 AM
  4. magic square
    By dizzyhippy in forum C Programming
    Replies: 1
    Last Post: 03-11-2002, 11:36 AM
  5. Magic Square
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 03-01-2002, 05:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21