Saturday, March 14, 2020

Programming Exercise for Odd Magic Squares in Java

Programming Exercise for Odd Magic Squares in Java Its unclear who first came up with a magic square. There is a story about a huge flood in China a long time ago. The people were worried they would be washed away and tried to appease the river god by making sacrifices. Nothing seemed to work until a child noticed a turtle sporting a magic square on its back that kept circling the sacrifice. The square told the people how big their sacrifice needed to be in order to save themselves. Since then magic squares have been the height of fashion for any discerning turtle. Level: Beginner Focus: Logic, Arrays, Methods Odd Magic Squares In case youve never come across one before, a magic square is an arrangement of sequential numbers in a square so that the rows, columns, and diagonals all add up to the same number. For instance, a 3x3 magic square is: 8 1 6 3 5 7 4 9 2 Each row, column and diagonal adds up to 15. Odd Magic Squares Question This programming exercise is concerned with creating odd sized magic squares (i.e., the size of the square can only be an odd number, 3x3, 5x5, 7x7, 9x9, and so on). The trick with making such a square is to place the number 1 in the first row and middle column. To find where to place the next number, move diagonally upwards to the right (i.e., one row up, one column across). If such a move means you fall off the square, wrap around to the row or column on the opposite side. Finally, if the move takes you to a square that is already filled, go back to the original square and move downwards by one. Repeat the process until all the squares are filled. For example, a 3x3 magic square would start like so: 0 1 0 0 0 0 0 0 0 A move diagonally upwards means we wrap around to the bottom of the square: 0 1 0 0 0 0 0 0 2 Likewise, the next diagonal move upwards means we wrap around to the first column: 0 1 0 3 0 0 0 0 2 Now the diagonal move upwards results in a square that is already filled, so we go back to where we came from and drop down a row: 0 1 0 3 0 0 4 0 2 and it continues on and on until all the squares are full. Program Requirements a user must be able to enter in the size of the magic square.they must only be allowed to enter in an odd number.use a method to create the magic square.use a method to display the magic square. The question is can your program create a 5x5 magic square like the one below? 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 Hint: Apart from the programming aspects of this exercise its also a test of logic. Take each step of creating the magic square in turn and figure how it can be done with a two-dimensional array. Odd Magic Square Solution Your program should have been capable of creating the 5x5 magic square below: 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 Heres my version: import java.util.Scanner; public class MagicOddSquare {   Ã‚  public static void main(String[] args) {   Ã‚  Ã‚  Ã‚  Scanner input new Scanner(;   Ã‚  Ã‚  Ã‚  int[][] magicSquare;   Ã‚  Ã‚  Ã‚  boolean isAcceptableNumber false;   Ã‚  Ã‚  Ã‚  int size -1;   Ã‚  Ã‚  Ã‚  //only accept odd numbers   Ã‚  Ã‚  Ã‚  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;   Ã‚  Ã‚  Ã‚  Ã‚  Ã‚  }   Ã‚  Ã‚  Ã‚  }   Ã‚  Ã‚  Ã‚  magicSqua re 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 k2;k matrixSize1;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 this position isnt empty then go back to where we   Ã‚  Ã‚  Ã‚  Ã‚  Ã‚  //started and move one row down   Ã‚  Ã‚  Ã‚  Ã‚  Ã‚  if (magicSq[row][column] 0)   Ã‚  Ã‚  Ã‚  Ã‚  Ã‚  {   Ã‚  Ã‚  Ã‚  Ã‚  Ã‚  Ã‚  Ã‚  magicSq[row][column] k;   Ã‚  Ã‚  Ã‚  Ã‚  Ã‚  }   Ã‚  Ã‚  Ã‚  Ã‚  Ã‚  els e   Ã‚  Ã‚  Ã‚  Ã‚  Ã‚  {   Ã‚  Ã‚  Ã‚  Ã‚  Ã‚  Ã‚  Ã‚  row lastRow;   Ã‚  Ã‚  Ã‚  Ã‚  Ã‚  Ã‚  Ã‚  column lastColumn;   Ã‚  Ã‚  Ã‚  Ã‚  Ã‚  Ã‚  Ã‚  if (row 1 size)   Ã‚  Ã‚  Ã‚  Ã‚  Ã‚  Ã‚  Ã‚  {   Ã‚  Ã‚  Ã‚  Ã‚  Ã‚  Ã‚  Ã‚  Ã‚  Ã‚  row0;   Ã‚  Ã‚  Ã‚  Ã‚  Ã‚  Ã‚  Ã‚  }   Ã‚  Ã‚  Ã‚  Ã‚  Ã‚  Ã‚  Ã‚   else   Ã‚  Ã‚  Ã‚  Ã‚  Ã‚  Ã‚  Ã‚  {   Ã‚  Ã‚  Ã‚  Ã‚  Ã‚  Ã‚  Ã‚  Ã‚  Ã‚  row;   Ã‚  Ã‚  Ã‚  Ã‚  Ã‚  Ã‚  Ã‚  }   Ã‚  Ã‚  Ã‚  Ã‚  Ã‚  Ã‚  Ã‚  magicSq[row][column] k;   Ã‚  Ã‚  Ã‚  Ã‚  Ã‚  }   Ã‚  Ã‚  Ã‚  Ã‚  Ã‚  lastRow row;   Ã‚  Ã‚  Ã‚  Ã‚  Ã‚  lastColumn column;   Ã‚  Ã‚  Ã‚  }   Ã‚  Ã‚  Ã‚  return magicSq;   Ã‚  }   Ã‚  private static void displaySquare(int[][] magicSq)   Ã‚  {   Ã‚  Ã‚  Ã‚  int magicConstant 0;   Ã‚  Ã‚  Ã‚  for (int j0;j(magicSq.length);j)   Ã‚  Ã‚  Ã‚  {   Ã‚  Ã‚  Ã‚  Ã‚  Ã‚  for (int k0;k(magicSq[j].length);k)   Ã‚  Ã‚  Ã‚  Ã‚  Ã‚  {   Ã‚  Ã‚  Ã‚  Ã‚  Ã‚  Ã‚  Ã‚  System.out.print(magicSq[j][k] );   Ã‚  Ã‚  Ã‚  Ã‚  Ã‚  }   Ã‚  Ã‚  Ã‚  Ã‚  Ã‚  System.out.print;   Ã‚  Ã‚  Ã‚  Ã‚  Ã‚  magicConstant magicConstant magicSq[j][0];   Ã‚  Ã‚  Ã‚  }   Ã‚  Ã‚  Ã‚   System.out.print(The magic constant is magicConstant);   Ã‚  } }

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.