To create a magic square of Odd Order The best known algori

To create a magic square of Odd Order -

The best known algorithm for generating magic squares of odd order is de La Loubere\'s method. Simon de La Loubere was the French ambassador to Siam in the late 17th century. I sometimes refer to his method as the \"nor\'easter algorithm\", after the winter storms that move northeasterly up the coast of New England. You can see why if you follow the integers sequentially through magic(9).


   47    58    69    80     1    12    23    34    45
   57    68    79     9    11    22    33    44    46
   67    78     8    10    21    32    43    54    56
   77     7    18    20    31    42    53    55    66
    6    17    19    30    41    52    63    65    76
   16    27    29    40    51    62    64    75     5
   26    28    39    50    61    72    74     4    15
   36    38    49    60    71    73     3    14    25
   37    48    59    70    81     2    13    24    35


The integers from 1 to n*n   are inserted along diagonals, starting in the middle of first row and heading in a northeasterly direction. When you go off an edge of the array, which you do at the very first step, continue from the opposite edge. When you bump into a cell that is already occupied, drop down one row and continue.

Write a Java program that will prompt the user to enter an odd integer between 3 and 25. Based on the input, display a magic square as described above. Submit multiple runs, high-lighting various features of the program. Print the sum of the two diagonals. Verify that each row and each column and the two diagonals add up to the same sum.

need it for JAVA

Solution

// MagicSquare.java

import java.util.*;
public class MagicSquare
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
  
int[][] magicSquare;
  
System.out.println(\"Enter an odd integer between 3 and 25: \");
int magicNumber = scan.nextInt();

magicSquare = new int[magicNumber+1][magicNumber+1];

int ROW = 1;
int COLUMN = (magicNumber+1)/2;

// assign all values to 0
for(int rowIndex = 0; rowIndex<magicNumber+1;rowIndex++)
for(int columnIndex = 0; columnIndex<magicNumber+1;columnIndex++)
magicSquare[rowIndex][columnIndex] =0;

// iterate over the matrix and update the required elements
for (int tempCounter =1 ; tempCounter <=magicNumber*magicNumber; tempCounter++)
{
magicSquare[ROW][COLUMN] = tempCounter;

ROW = ROW - 1;
COLUMN = COLUMN + 1;

if(ROW <= 0)
ROW = magicNumber;

if(COLUMN>magicNumber)
COLUMN = 1;
  
if(magicSquare[ROW][COLUMN] != 0)
{
if ( ROW == magicNumber )
ROW = 0;   
if ( COLUMN == 1)
COLUMN = magicNumber+1;
ROW = ROW + 2;
COLUMN = COLUMN - 1;
}

if(ROW <= 0)
ROW = magicNumber;
if(COLUMN > magicNumber)
COLUMN = 1;
}

// print the matrix
System.out.println(\"\ MAGIC SQUARE:\");
for(int rowIndex = 1; rowIndex <= magicNumber;rowIndex++)
{
for(int columnIndex = 1; columnIndex<=magicNumber;columnIndex++)
{
String temp =\" \";
if(magicSquare[rowIndex][columnIndex]<10)
{
temp = temp+\" \";
temp +=magicSquare[rowIndex][columnIndex];
System.out.print(\" \"+temp);
}
else if (10 <= magicSquare[rowIndex][columnIndex] && magicSquare[rowIndex][columnIndex] <=99)
{
temp +=magicSquare[rowIndex][columnIndex];
System.out.print(\" \"+temp);
}
else
System.out.print(\" \"+magicSquare[rowIndex][columnIndex]);
}
System.out.println();
}

int diagonal1SUm = 0;
for(int rowIndex = 1; rowIndex <= magicNumber;rowIndex++)
{
diagonal1SUm = diagonal1SUm + magicSquare[rowIndex][rowIndex];
}
  
int diagonal2SUm = 0;
for(int rowIndex = 1; rowIndex <= magicNumber;rowIndex++)
{
diagonal2SUm = diagonal2SUm + magicSquare[rowIndex][magicNumber - rowIndex + 1];
}

System.out.println(\"\ Diagonal 1 sum: \" + diagonal1SUm);
System.out.println(\"Diagonal 2 sum: \" + diagonal2SUm);
}
}   

/*
output:

Enter an odd integer between 3 and 25:
9

MAGIC SQUARE:
47 58 69 80 1 12 23 34 45
57 68 79 9 11 22 33 44 46
67 78 8 10 21 32 43 54 56
77 7 18 20 31 42 53 55 66
6 17 19 30 41 52 63 65 76
16 27 29 40 51 62 64 75 5
26 28 39 50 61 72 74 4 15
36 38 49 60 71 73 3 14 25
37 48 59 70 81 2 13 24 35

Diagonal 1 sum: 369
Diagonal 2 sum: 369

*/

To create a magic square of Odd Order - The best known algorithm for generating magic squares of odd order is de La Loubere\'s method. Simon de La Loubere was t
To create a magic square of Odd Order - The best known algorithm for generating magic squares of odd order is de La Loubere\'s method. Simon de La Loubere was t
To create a magic square of Odd Order - The best known algorithm for generating magic squares of odd order is de La Loubere\'s method. Simon de La Loubere was t

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site