Help with writing a program that creates an odd n*n magic square where n is...

This is a discussion on Help with writing a program that creates an odd n*n magic square where n is... within the C Programming forums, part of the General Programming Boards category; I am in a beginning C programming class and I have this assignment: Write a program that creates an N*N ...

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

    Help with writing a program that creates an odd n*n magic square where n is...

    I am in a beginning C programming class and I have this assignment: Write a program that creates an N*N magic square, i.e., a square arrangement of the numbers 1,2,,N^2 in which the sum of rows, columns, and diagonals are the same. The user will specify the size of the square matrix: N. The value N must be an odd number between 1 and 15.


    I am using visual studio 2010. Also I am only allowed to use C89. My program will not compile and I have been trying to fix my problems, but I still have a problem with what the program prints out. My program prints out a vertical column of the magic square. For example if my magic square is size 3, it'll print:
    8
    1
    6


    3
    5
    7


    4
    9
    2


    Help please. Thank you!


    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 [array_size][array_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");
    				}
    
    
    				break;
    	}
    	}
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2012
    Posts
    8
    Sorry I figured it out!

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    Code:
    $ ./a.out 
    Enter size of magic square: 3
       8   1   6
       3   5   7
       4   9   2
    Perhaps remove the \n from the first printf ?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 11-20-2012, 08:08 PM
  2. magic square program
    By Zarakava in forum C Programming
    Replies: 10
    Last Post: 01-27-2009, 06:06 PM
  3. Characters in a Magic Square Program
    By TeamGreen in forum C++ Programming
    Replies: 1
    Last Post: 03-16-2004, 07:08 AM
  4. Help with magic square program
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 07-15-2002, 05:57 AM
  5. Magic Square
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 03-01-2002, 05:34 PM

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