Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
Below is a 3x3 magic square C code for odd numbers 3 to 15 and displaying the magic sum total number 15 for rows, columns, and diagonals for odd number 3 in Example1 Output. If we scroll down to the comment //Sum up and down diagonals - display at the end on diagonals and the C code below it which display the diagonal magic sum total number 15 for 8, 5, and 2 for odd number 3 in the lower right corner of Example1 Output.

Question: How will I display diagonal magic sum total number 15 for 6, 5, and 4 for odd number 3 in the lower left corner or upper right corner in Example1 Output to look like Example2 or Example3 Outputs?

*Note the C code under the comment //Sum up and down diagonals - display at the end on diagonals works for all odd numbers 3 to 15.

C++
#include <stdio.h>
#define Mag 15

int main(void)
{
   int n, col, row, i, j, k, r, c, diag=0;
   int magic[Mag][Mag];  /* Declare a two-dimensional array of size x size */

   printf("\nThis program creates a \"magic square\" of specified odd numbers.");
   printf("\nThe size must be an odd number.");
   printf("\nEnter odd number from 3 to 15: ");
   scanf(" %d", &n);
	 
   for (j = 0; j < n+1; j++)          
        for (k = 0; k < n+1; k++)  
	     magic[j][k] = 0;     

   row = 0;               
   col = n / 2;        
   magic[row][col] = 1;  

   for (i=2; i <= n * n; i++)
     {
     if (--row < 0)             
	 row = (n-1);

     if (++col > (n-1))
	 col = 0;           

      if (magic[row][col] != 0)  
       {
	if (++row > (n-1))    
	    row = 0;
           
	 if (--col < 0)
             col = (n-1);  

	  while (magic[row][col] != 0)    
		 if (++row > (n-1))  
		      row = 0;         
          }                                        

	  magic[row][col] = i;  
	 }


         //End of the logic loop to fill in the magic square 

         //Sum the row - display at right side
         for(r=0; r<n; r++){
	     for(c=0; c<n; c++){
	         magic[r][n]+=magic[r][c];}
        }

        //Sum the columns - display at the bottom 
        for(c=0; c<n; c++){
	    for(r=0; r<n; r++){
	        magic[n][c]+=magic[r][c];}
       }

       // Sum up and down diagonals - display at the end on diagonals 
       for(r=0; r<n; r++){
           magic[n][n]+=magic[r][r];}

       for(r=1; r<(n-1); r++){
           c = n - r + 1;
              diag+=magic[r][c];}


      //Print out matrix with row, column, and diagonal sums 
      printf("\n\n");
      for(r=0; r<(n+1) ; r++){
          printf("\n");
      for(c=0; c<(n+1) ; c++)
          printf("%4d", magic[r][c]);
     printf("\n");
   }
    return 0;
}


           Example1 Output

           8    1    6   15
           3    5    7   15
   4    9    2   15
           15   15  15   15


             Example2 Output

          8    1    6    15
  3    5    7    15
  4    9    2    15
       15 15   15   15   15


             Example3 Output

                        15
        8    1    6     15
3    5    7     15
4    9    2     15
        15   15   15    15
Posted
Updated 11-Sep-13 13:48pm
v4
Comments
Richard MacCutchan 12-Sep-13 3:50am    
This is a question of mathematics not programming.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900