Answer to Monday's Odd Magic Square Question
On Monday the programming question was about odd magic squares (e.g., 3x3, 5x5, 7x7). The Java program had to position sequential numbers in a square so that the rows, columns and diagonals all add up to the same number.
More specifically it needed to take an input from the user for an odd number and use two methods – one to create the magic square and one to display it. All being well your program should be able to display the 5x5 magic square:
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
Here's my version:
import java.util.Scanner;
public class MagicOddSquare {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int[][] magicSquare;
boolean isAcceptableNumber = false;
int size = -1;
while (isAcceptableNumber == false)
{
System.out.println("Enter in size of square: ");
String sizeText = input.nextLine();
size = Integer.parseInt(sizeText);
if (size % 2 == 0)
{
System.out.println("The size must be an odd number");
isAcceptableNumber = false;
}
else
{
isAcceptableNumber = true;
}
}
magicSquare = createOddSquare(size);
displaySquare(magicSquare);
}
private static int[][] createOddSquare(int size)
{
int[][] magicSq = new int[size][size];
int row = 0;
int column = size/2;
int lastRow = row;
int lastColumn = column;
int matrixSize = size*size;
magicSq[row][column]= 1;
for (int k=2;k < matrixSize+1;k++)
{
//check if we need to wrap to
//opposite row
if (row - 1 < 0)
{
row = size-1;
}
else
{
row--;
}
//check if we need to wrap
//to opposite column
if (column + 1 == size)
{
column = 0;
}
else
{
column++;
}
if (magicSq[row][column] == 0)
{
magicSq[row][column] = k;
}
else
{
row = lastRow;
column = lastColumn;
if (row + 1 == size)
{
row=0;
}
else
{
row++;
}
magicSq[row][column] = k;
}
lastRow = row;
lastColumn= column;
}
return magicSq;
}
private static void displaySquare(int[][] magicSq)
{
int magicConstant = 0;
for (int j=0;j<(magicSq.length);j++)
{
for (int k=0;k<(magicSq[j].length);k++)
{
System.out.print(magicSq[j][k] + " ");
}
System.out.print("\n");
magicConstant = magicConstant + magicSq[j][0];
}
System.out.print("The magic constant is " + magicConstant);
}
}


Comments
No comments yet. Leave a Comment