Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //************************************************
- // Nathan Schnitzer
- // Magic Square / Summer Assignment
- // 6/30/17
- // Will determine if matrix is an magic square supports up to 4x4
- //************************************************
- import java.util.Scanner;
- import java.util.ArrayList;
- import java.util.Arrays;
- public class magicSquare
- {
- public static void main(String[] args)
- {
- Scanner scan = new Scanner(System.in);
- ArrayList<Integer> input = new ArrayList<Integer>();
- int rowTotal1 = 0, rowTotal2 = 0, rowTotal3=0, rowTotal4 = 0;
- int colTotal1=0, colTotal2=0, colTotal3=0, colTotal4 = 0;
- int diagTotal1=0, diagTotal2 = 0;
- System.out.println("Enter input");
- int input1 = scan.nextInt();
- int size = input1 * input1; //Square = L*H and L=H
- //Scan input1^2 or size elements
- for (int i = 0; i < size; i++)
- input.add(scan.nextInt());
- //Create array
- int[][] square = new int[input1][input1];
- for (int row = 0; row < input1; row++)
- {
- for (int col = 0; col < input1; col++)
- {
- square[row][col] = input.get((input1*row)+col);
- }
- }
- //Print out 2d array
- for (int i = 0; i < 3; i++)
- {
- for (int k = 0; k < 3; k++)
- System.out.print(square[i][k]);
- System.out.println();
- }
- //Calculate 3 row totals
- //Row 1:
- for (int i = 0; i < square.length; i++)
- {
- rowTotal1 += square[0][i];
- }
- //Row 2:
- for (int i = 0; i < square.length; i++)
- {
- rowTotal2 += square[1][i];
- }
- //Row 3:
- if (size > 2)
- for (int i = 0; i < square.length; i++)
- {
- rowTotal3 += square[2][i];
- }
- //If it is 4x4
- if (input1 > 3)
- {
- for (int i = 0; i < square.length; i++)
- rowTotal4 += square[3][i];
- }
- //Calculate Column totals
- //Column 1:
- for (int i = 0; i < square.length; i++)
- {
- colTotal1 += square[i][0];
- }
- //Column 2:
- for (int i = 0; i < square.length; i++)
- {
- colTotal2 += square[i][1];
- }
- //Column 3:
- for (int i = 0; i < square.length; i++)
- {
- colTotal3 += square[i][2];
- }
- //If it is 4x4
- if (input1 > 3)
- {
- for (int i = 0; i < square.length; i++)
- colTotal4 += square[i][3];
- }
- //Calculate Diagonals
- //Diag 1: (Bottom Left to Top Right
- int cols = 0;
- for (int i = input1 - 1; i >= 0; i--)
- {
- System.out.println(square[i][cols]);
- diagTotal1 += square[i][cols];
- cols++;
- }
- //Diag 2: (Top Left to Bottom Right)
- for (int i = 0; i < square.length; i++)
- {
- System.out.println(square[i][i]);
- diagTotal2 += square[i][i];
- }
- System.out.println("Row 1 total: " + rowTotal1);
- System.out.println("Row 2 total: " + rowTotal2);
- System.out.println("Row 3 total: " + rowTotal3);
- System.out.println("Row 4 total: " + rowTotal4);
- System.out.println("Column 1 total: " + colTotal1);
- System.out.println("Column 2 total: " + colTotal2);
- System.out.println("Column 3 total: " + colTotal3);
- System.out.println("Column 4 total: " + colTotal4);
- System.out.println("Diagonal 1 total: " + diagTotal1);
- System.out.println("Diagonal 2 total: " + diagTotal2);
- //Check if it is a magic square
- boolean magic = false;
- if (input1 == 2)
- if (rowTotal1 == rowTotal2 && rowTotal1 == colTotal1 && rowTotal1 == colTotal2
- && rowTotal1 == diagTotal1 && rowTotal1 == diagTotal2)
- magic = true;
- else
- magic = false;
- if (input1 == 3)
- if (rowTotal1 == rowTotal2 && rowTotal1 == rowTotal3 && rowTotal1 == colTotal1
- && rowTotal1 == colTotal2 && rowTotal1 == colTotal3 && rowTotal1 == diagTotal1 && rowTotal1 == diagTotal2)
- magic = true;
- else
- magic = false;
- if (input1 == 4)
- if (rowTotal1 == rowTotal2 && rowTotal1 == rowTotal3 && rowTotal1 == rowTotal4 && rowTotal1 == colTotal1
- && rowTotal1 == colTotal2 && rowTotal1 == colTotal3 && rowTotal1 == colTotal4 && rowTotal1 == diagTotal1 && rowTotal1 == diagTotal2)
- magic = true;
- else
- magic = false;
- //Print if it is magic
- if (magic)
- System.out.println("The Matrix is a magic square.");
- else
- System.out.println("The Matrix is not a magic square.");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement