Like Tree3Likes
  • 3 Post By Matticus

Odd Magic Square One problem left!

This is a discussion on Odd Magic Square One problem left! within the C Programming forums, part of the General Programming Boards category; I posted earlier tonight. You can view my original post here: http://cboard.cprogramming.com/c-pro...ml#post1135266 . So, now everything works fine, but the ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    8

    Odd Magic Square One problem left!

    I posted earlier tonight. You can view my original post here: Help with writing a program that creates an odd n*n magic square where n is....

    So, now everything works fine, but the problem left is that my code will compile and run, but instead of printing my output as a square it prints it in a vertical column: For example if I wanted my magic square to be 3 it would print:
    8
    1
    6
    3
    5
    7
    4
    9
    2
    **Also I have tried placing my 'break;' in my program, but it doesn't seem to be working.

    Thank you!
    Here is my updated code:

    Code:
    #include <stdio.h>#define array_size 15
    
    
    int main (void) 
    {
    	int row, column, size, n = 2, magic_square [array_size][array_size];
    	
    
    
    	for ( ; ; ) {
    
    
    	printf ("Enter size of magic square: ");
    
    
    	scanf ("%d", &size);
    
    
    	if (size % 2 == 0)
    		printf ("Enter an odd number!\n");
    
    
    	else if (size <= 0 || size > 15)
    		printf ("Enter a number between 1 and 15!\n");
    
    
    	else {
    
    
    		magic_square [size][size];
    
    
    		for (row = 0; row < size; row++)
    			for (column = 0; column < size; column++)
    				magic_square [row][column] = 0;
    
    
    		row = 0;
    		column = (size/2);
    		magic_square [row][column] = 1;
    
    
    		for (n; n<= size*size; n++) {
    			if (--row < 0)
    				row = (size -1);
    			if (++column > size -1)
    				column = 0;
    			if (magic_square [row][column] != 0) {
    				if (++row > (size -1))
    					row = 0;
    				if (--column < 0)
    					column = size - 1;
    				while (magic_square [row][column] != 0)
    					if (++row > (size -1))
    						row = 0;
    			}
    
    
    			magic_square [row][column] = n;
    		}
    
    
    				for (row = 0; row < size; row++) 
    				for (column = 0; column < size; column++) 
    				printf ("%4d\n", magic_square [row][column]);
    				printf ("\n");
    
    
    
    
    	}
    	}
    
    
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    2,241
    I don't know anything about magic squares, but maybe this will help.

    Your code without good indentation:

    Code:
                    for (row = 0; row < size; row++) 
                    for (column = 0; column < size; column++) 
                    printf ("%4d\n", magic_square [row][column]);
                    printf ("\n");
    Your code with good indentation:

    Code:
        for (row = 0; row < size; row++) 
            for (column = 0; column < size; column++) 
                printf ("%4d\n", magic_square [row][column]);
        printf ("\n");
    So, let's see:

    Line 3: You are printing a newline out every time you print a value. That is why it's all appearing in one column.

    Line 4: You are only printing a newline once here, when the row/column printing is done. Which means, instead of printing only in a vertical column, it will only print in a horizontal row. I think you meant to add some braces for the first "for()" loop, to encompass the last newline print.
    iMalc, qny and AndiPersti like this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Magic square
    By ardavirus in forum C Programming
    Replies: 2
    Last Post: 06-01-2011, 10:53 AM
  2. magic square problem
    By ranjan in forum C++ Programming
    Replies: 1
    Last Post: 04-28-2009, 12:16 PM
  3. Magic Square Problem
    By GCNDoug in forum C++ Programming
    Replies: 23
    Last Post: 02-18-2008, 12:13 AM
  4. Magic Square
    By ilanco in forum C Programming
    Replies: 1
    Last Post: 12-31-2003, 07:07 AM
  5. magic square
    By dizzyhippy in forum C Programming
    Replies: 1
    Last Post: 03-11-2002, 11:36 AM

Tags for this Thread


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