magic square whoes

This is a discussion on magic square whoes within the C Programming forums, part of the General Programming Boards category; I am trying to figure this out. I wrote the program without functions but need to have it with functions. ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    15

    Unhappy magic square whoes

    I am trying to figure this out. I wrote the program without functions but need to have it with functions. Once I tried to put the code in functions I cant get it to work. I need to have a magic square where the user inputs an odd number between 3-15, then print the squre to the screen. I also need to add the col, row and diag.. but I worry about that later. I have been trying different things for a week now with no solution in site. this si what I have right now..

    Code:
    #include<stdio.h> 
    
            
    void build_square (int magic[15][15])
    
    {
    	int row;
    	int col; 
    	int base=1;
    	int entry;
    
    	printf("Build a Magic Square!\n\n");
    	printf("Enter an odd number between 3 and 15 then hit enter: ");
    	scanf("%d",&entry);
    	printf("\n\n");
      
    	row = 0; col=(entry - 1)/2;   /* establishes the center square of the first row */
      
    	/* algorithm with while/if else to establish where value are in col and row */
    	/* based on rule given for the magic square game */
      
    	while (base <= entry*entry) {
    		magic[row][col] = base;
    		if (base == entry*(base/entry)) row++;
    		else {
    			row--; col++;
    			if (row < 0) row = entry - 1;
    			if (col > entry - 1) col = 0;
    		}
       	 base++;
     	}
     
    }
                
    
    main()
    {
      int row,col,entry=15,magic[15][15], magic_number;
      
        	/* function call */
    
    	
    	build_square (magic[row][col]);
    
    
       printf("Your Magic Square!\n\n");
      
      for(row = 0; row<entry;row++) {
        for (col=0;col<entry;col++) printf("%4d",magic[row][col]);
        printf("\n");
      }
      
    
    }
    no matter what I enter I get a square of 15 with strange intergers!!!

  2. #2
    Rog
    Rog is offline
    Registered User
    Join Date
    Jan 2003
    Posts
    78
    I need a little more information... is the number that the user enters the size of the sides of the square, or is it a seed to populate a 15x15 magic square?

    In other words, is your square always going to be 15x15 or is that only the MAX size of the square?

    Also... what is the algorithm you are using? There are so many different ones for magic squares.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    DELETED! Obviously magic squares != normal squares...
    Last edited by Magos; 03-28-2003 at 05:37 AM.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    Cool

    I give this magic square program frequently in my C classes. There are different variations on similar algorithms. Basically there is one method to calculate odd number magic squares (sounds like what you are doing) and "even number" magic squares. However, you cannot create magic squares for all numbers.

    As for your code- I feel sorry so here is algorithm I show my students

    Code:
    calculateMagic
    	   Set row = 0
    	   Set col = size  2
    	   Set matrix to 1
             Loop (next = 2 to size * size)
    	      Decrement row by 1
    		Increment col by 1
                If (row less than 0 and col greater than size  1)
                  Set row = 1
    		  Set col = size  1
                else if (row less than zero)
    		  Set row = size  1
    		else if (col greater than size  1)
    		  Set col = 0
    		else if (matrix not equal to 0)
    		  Increment row by 2
    		  Decrement col by 1
    		matrix = next
             EndLoop       
    	END
    It is all there- now put it all together.!!!
    Mr. C: Author and Instructor

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    15

    still having problems

    I have tried the suggestion as follows.. and cant get that to work
    2nd code is my code which wont work either...I just cant figure what I am doing wrong.. when I pull it out of functions it works....

    Code:
    #include<stdio.h> 
            
    #define MAX_SIZE 15   /* maximum size of square, must be odd */               
    
    main()
    {
      int row,col,next,entry,magic[MAX_SIZE][MAX_SIZE], all_equal,remain,matrix;
    
      /* collects the size of the magic square */
      
      printf("Build a Magic Square!\n\n");
      printf("Enter an odd number between 3 and 15 then hit enter: ");
      scanf("%d",&entry);
      printf("\n\n");
      
           
      /* tests for odd number */
      
      remain = entry % 2;
           
            if (remain == 0) 	
            {
      		
      		printf ("Your number is even, please enter an odd number: ");
      		scanf ("%d",&entry);
     		printf ("\n\n");
     	}
      	
     
      /* establishes the center square of the first row */
      
      row = 0; 
      col=(entry - 1)/2;
      matrix = 1; 
      next = 1; 
      
      /* algorithm with while/if else to establish where value are in col and row */
      /* nextd on rule given for the magic square game */
      
      while (next != entry*entry) {
        row--;
        col++;
    	if (row < 0){
    	   if (col > entry - 1){
    		row = 1;
    		col = entry - 1;
    	}
    	}
    	else if (row < 0) {
    		
    		row = entry - 1;
    		}
    	else if (col > entry -1){
    		
    		col = 0;
    		}
    	else if (matrix != 0){
    		
    		row = row + 2;
    		col--;
    		}
    	matrix = next;
    }
    
    
      
       printf("Your Magic Square!\n\n");
      
      for(row = 0; row<entry;row++) {
        for (col=0;col<entry;col++) printf("%4d",magic[row][col]);
        printf("\n");
      }
     
      for (col = 0; col < entry; col++) all_equal += magic[0][col];
      printf("\nAll rows, columns and diagonals added equal %d.\n",all_equal); 
    
    }
    my other code

    Code:
    #include<stdio.h> 
    
    #define MAX_SIZE 15   /* maximum size of square, must be odd */   
    
    
    /************************************************************************/
    /*                      Function: build_square                          */
    /*                                                                      */
    /*  Purpose:    Obtains input from user, tests if input is odd          */
    /*              builds magic square based on input value                */
    /* 		and stores the results in an multidimensional array     */
    /*                                                                      */
    /*  Parameters: magic[MAX_SIZE][MAX_SIZE] - multidimensional array      */
    /*                                                                      */
    /*                                                                      */
    /*  Returns:    Nothing, magic[MAX_SIZE][MAX_SIZE] array are passed     */
    /*               by reference.                                          */
    /************************************************************************/
            
    void build_square (int magic[MAX_SIZE][MAX_SIZE])
    {
     
       /* local variables */
       int row, col, count, entry, remain;
    
      /* collects the size of the magic square */
      
      printf("Build a Magic Square!\n\n");
      printf("Enter an odd number between 3 and 15 then hit enter: ");
      scanf("%d", &entry);
      printf("\n\n");
      
           
      /* tests for odd number */
      
      remain = entry % 2;
           
      if (remain == 0) 	
       {
      	printf ("Your number is even, please enter an odd number: ");
      	scanf ("%d",&entry);
     	printf ("\n\n");
       }
      	
     
      count=0; /* sets the count for the loop */
      
      row = 0; col=(entry - 1)/2;   /* establishes the center square of the first row */
      magic[row][col] = count; 
      
      /* algorithm with while/if else to establish where value are in col and row */
      /* countd on rule given for the magic square game */
      
     do 
     { 
      		if (count == entry * (count/entry))
      			row++;
      		else {
      			row--;
      			col++;
      			if (row < 0)
      				row = entry - 1;
      			if (col > entry - 1)
      				col = 0;
      		}
    	count++;
      }
      while (count != entry*entry);
    }
    
    
    
    /************************************************************************/
    /*                      Function: print_square                          */
    /*                                                                      */
    /*  Purpose:    Prints the magic square                                 */
    /*                                                                      */
    /*  Parameters: row   = row of magic square                             */
    /*		col   = columns of magic square                         */
    /*		entry = number user entered                             */
    /*		magic[MAX_SIZE][MAX_SIZE] = multidimensional array      */
    /*                                                                      */
    /*  Returns:    Nothing                                                 */
    /************************************************************************/
    
    
    void print_square (int row, int col, int entry, int magic[MAX_SIZE][MAX_SIZE])
    {
    
       printf("Your Magic Square!\n\n");
      
        for (row = 0; row < entry; row++) {
        for (col = 0; col < entry; col++)
        printf("%4d", magic[row][col]);
        printf("\n");
      }
    }         
    
    
    
    /************************************************************************/
    /*                      Function: value                                */
    /*                                                                      */
    /*  Purpose:    Finds the value of rows,  col and diagnol               */
    /*                                                                      */
    /*  Parameters: row   = row of magic square                             */
    /*		col   = columns of magic square                         */
    /*		entry = number user entered                             */
    /*		magic[MAX_SIZE][MAX_SIZE] = multidimensional array      */
    /*                                                                      */
    /*  Returns:    Nothing                                                 */
    /************************************************************************/
    
    
    void value (int col, int entry, int all_equal, int magic[MAX_SIZE][MAX_SIZE])
    {
    	for (col = 0; col < entry; col++) all_equal += magic[0][col];
    	printf("\nAll rows, columns and diagonals added equal %d.\n",all_equal);
    } 
    
            
    
    main()
    {
    	int row, col, entry, magic[MAX_SIZE][MAX_SIZE];
     
     
    	/* function call */
    	
    	build_square (magic);
    
    	print_square(row, col, entry, magic);
    	
    	value (row, col, entry, magic);
    
    }

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Here's a clue on the second prog:

    >>print_square(row, col, entry, magic);
    What value are row, col, and entry holding when you issue this call in main() ?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User
    Join Date
    Mar 2003
    Posts
    15

    tried to use hin t to fix values passed and no luck still

    I am just not seeing something obvious I am sure.. but I just cant get it... to me it all seems right and I know its not because it doesnt work.

    Code:
    #include<stdio.h> 
    
    #define MAX_SIZE 15   /* maximum size of square, must be odd */   
    
    
    /************************************************************************/
    /*                      Function: build_square                          */
    /*                                                                      */
    /*  Purpose:    Obtains input from user, tests if input is odd          */
    /*              builds magic square based on input value                */
    /* 		and stores the results in an multidimensional array     */
    /*                                                                      */
    /*  Parameters: magic[MAX_SIZE][MAX_SIZE] - multidimensional array      */
    /*                                                                      */
    /*                                                                      */
    /*  Returns:    Nothing, magic[MAX_SIZE][MAX_SIZE] array are passed     */
    /*               by reference.                                          */
    /************************************************************************/
            
    void build_square (int entry, int magic[MAX_SIZE][MAX_SIZE])
    {
     
       /* local variables */
       int row, col, count;
    
      
      	
     
      count=0; /* sets the count for the loop */
      
      row = 0; col=(entry - 1)/2;   /* establishes the center square of the first row */
    
      
      /* algorithm with while/if else to establish where value are in col and row */
      /* countd on rule given for the magic square game */
      
     while (count <= entry*entry) 
      	{
        	magic[row][col] = count;
        	if (count == entry * (count/entry))
        		row++;
        		else {
        		row--;
        		col++;
         		if (row < 0)
         			row = entry - 1;
          		if (col > entry - 1)
          			col = 0;
        		}
        	count++;
      	}
    }
    
    
    /************************************************************************/
    /*                      Function: print_square                          */
    /*                                                                      */
    /*  Purpose:    Prints the magic square                                 */
    /*                                                                      */
    /*  Parameters: row   = row of magic square                             */
    /*		col   = columns of magic square                         */
    /*		entry = number user entered                             */
    /*		magic[MAX_SIZE][MAX_SIZE] = multidimensional array      */
    /*                                                                      */
    /*  Returns:    Nothing                                                 */
    /************************************************************************/
    
    
    void print_square (int row, int col, int entry, int magic[MAX_SIZE][MAX_SIZE])
    {
    
       printf("Your Magic Square!\n\n");
      
        for (row = 0; row < entry; row++) {
        for (col = 0; col < entry; col++)
        printf("%i", magic[row][col]);
        printf("\n");
      }
    }         
    
    
    
    /************************************************************************/
    /*                      Function: value                                */
    /*                                                                      */
    /*  Purpose:    Finds the value of rows,  col and diagnol               */
    /*                                                                      */
    /*  Parameters: row   = row of magic square                             */
    /*		col   = columns of magic square                         */
    /*		entry = number user entered                             */
    /*		magic[MAX_SIZE][MAX_SIZE] = multidimensional array      */
    /*                                                                      */
    /*  Returns:    Nothing                                                 */
    /************************************************************************/
    
    
    void value (int entry, int magic[MAX_SIZE][MAX_SIZE])
    {
    	int all_equal=0;
    	int i=0;
    	
    	for (i = 0; i < entry; i++) 
    		all_equal = all_equal + magic[0][i];
    	printf("\nAll rows, columns and diagonals added equal %i.\n", all_equal);
    } 
    
            
    
    main()
    {
    	int row=0, col=0, entry, remain, magic[MAX_SIZE][MAX_SIZE];
    	
    	/* collects the size of the magic square */
      
      	printf("Build a Magic Square!\n\n");
    	printf("Enter an odd number between 3 and 15 then hit enter: ");
      	scanf("%i", &entry);
     	 printf("\n\n");
      
           
     	 /* tests for odd number */
      
     	 remain = entry % 2;
           
      	 if (remain == 0) 	
      		{
      		printf ("Your number is even, please enter an odd number: ");
      		scanf ("%i",&entry);
     		printf ("\n\n");
      		}
     
     
    	/* function call */
    	
    	build_square (entry, magic);
    
    	print_square (row, col, entry, magic);
    	
    	value (entry, magic);
    
    }

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>to me it all seems right and I know its not because it doesnt work.
    Can you be more specific than "it doesnt work" ?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Registered User
    Join Date
    Mar 2003
    Posts
    15

    sure thing

    When I input the number to make the magic square, for instance in this case I use 3 as an example. The magic square does not build, instead I get an output of

    Code:
    897
    897
    897
    The frustrating thing is I did the entire program without the use of functions and it works. When I divide it up into the functions. first one being "build the square", second "print the square" and third "calculate the magic number" it does not work.

    I am sure I am just missing something that is not being passed corrrectly but I just cant seem to find it at this point. When I go through it, it seems that I am passing everything correctly.

  10. #10
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    Red face

    Ok what is your problem now?

    More clues;

    Enter size: size

    function prototype for magic square

    void magic (int square[][MAX], int size);

    now if you can read in write out a 2D array with the code provide earlier then you should have. it.
    Mr. C: Author and Instructor

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Forced moves trouble!!
    By Zishaan in forum Game Programming
    Replies: 0
    Last Post: 03-27-2007, 06:57 PM
  2. Windows crashing Magic Square
    By KoshiB in forum C++ Programming
    Replies: 9
    Last Post: 04-19-2006, 09:02 PM
  3. help on magic square
    By katway in forum C Programming
    Replies: 2
    Last Post: 03-07-2005, 05:44 PM
  4. help with magic square
    By dragon_heart in forum C++ Programming
    Replies: 3
    Last Post: 11-26-2004, 04:56 PM
  5. Help with magic square program
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 07-15-2002, 05:57 AM

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