Advertisement
CSnathan

Summer Assignment

Jun 24th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.15 KB | None | 0 0
  1. //************************************************
  2. // Nathan Schnitzer
  3. // Magic Square / Summer Assignment
  4. // 6/30/17
  5. // Will determine if matrix is an magic square supports up to 4x4
  6. //************************************************
  7.  
  8. import java.util.Scanner;
  9. import java.util.ArrayList;
  10. import java.util.Arrays;
  11.  
  12. public class magicSquare
  13. {
  14.  
  15.     public static void main(String[] args)
  16.     {
  17.         Scanner scan = new Scanner(System.in);
  18.         ArrayList<Integer> input = new ArrayList<Integer>();
  19.         int rowTotal1 = 0, rowTotal2 = 0, rowTotal3=0, rowTotal4 = 0;
  20.         int colTotal1=0, colTotal2=0, colTotal3=0, colTotal4 = 0;
  21.         int diagTotal1=0, diagTotal2 = 0;
  22.        
  23.         System.out.println("Enter input");
  24.         int input1 = scan.nextInt();
  25.         int size = input1 * input1; //Square = L*H and L=H
  26.        
  27.         //Scan input1^2 or size elements
  28.         for (int i = 0; i < size; i++)
  29.             input.add(scan.nextInt());
  30.        
  31.         //Create array
  32.         int[][] square = new int[input1][input1];
  33.        
  34.         for (int row = 0; row  < input1; row++)
  35.         {
  36.             for (int col = 0; col < input1; col++)
  37.             {
  38.                 square[row][col] = input.get((input1*row)+col);
  39.             }
  40.         }
  41.        
  42.         //Print out 2d array
  43.         for (int i = 0; i < 3; i++)
  44.         {
  45.             for (int k = 0; k < 3; k++)
  46.                 System.out.print(square[i][k]);
  47.             System.out.println();
  48.         }
  49.        
  50.         //Calculate 3 row totals
  51.         //Row 1:
  52.         for (int i = 0; i < square.length; i++)
  53.         {
  54.             rowTotal1 += square[0][i];
  55.         }
  56.        
  57.         //Row 2:
  58.         for (int i = 0; i < square.length; i++)
  59.         {
  60.             rowTotal2 += square[1][i];
  61.         }
  62.        
  63.         //Row 3:
  64.         if (size > 2)
  65.         for (int i = 0; i < square.length; i++)
  66.         {
  67.             rowTotal3 += square[2][i];
  68.         }
  69.        
  70.         //If it is 4x4
  71.         if (input1 > 3)
  72.         {
  73.             for (int i = 0; i < square.length; i++)
  74.                 rowTotal4 += square[3][i];
  75.                
  76.         }
  77.        
  78.        
  79.         //Calculate Column totals
  80.         //Column 1:
  81.         for (int i = 0; i < square.length; i++)
  82.         {
  83.             colTotal1 += square[i][0];
  84.         }
  85.        
  86.         //Column 2:
  87.         for (int i = 0; i < square.length; i++)
  88.         {
  89.             colTotal2 += square[i][1];
  90.         }
  91.        
  92.         //Column 3:
  93.         for (int i = 0; i < square.length; i++)
  94.         {
  95.             colTotal3 += square[i][2];
  96.         }
  97.        
  98.         //If it is 4x4
  99.         if (input1 > 3)
  100.         {
  101.             for (int i = 0; i < square.length; i++)
  102.                 colTotal4 += square[i][3];
  103.                
  104.         }
  105.        
  106.         //Calculate Diagonals
  107.         //Diag 1: (Bottom Left to Top Right
  108.         int cols = 0;
  109.         for (int i = input1 - 1; i >= 0; i--)
  110.         {
  111.             System.out.println(square[i][cols]);
  112.             diagTotal1 += square[i][cols];
  113.             cols++;
  114.         }
  115.        
  116.         //Diag 2: (Top Left to Bottom Right)
  117.         for (int i = 0; i < square.length; i++)
  118.         {
  119.             System.out.println(square[i][i]);
  120.             diagTotal2 += square[i][i];
  121.         }
  122.        
  123.         System.out.println("Row 1 total: " + rowTotal1);
  124.         System.out.println("Row 2 total: " + rowTotal2);
  125.         System.out.println("Row 3 total: " + rowTotal3);
  126.         System.out.println("Row 4 total: " + rowTotal4);
  127.         System.out.println("Column 1 total: " + colTotal1);
  128.         System.out.println("Column 2 total: " + colTotal2);
  129.         System.out.println("Column 3 total: " + colTotal3);
  130.         System.out.println("Column 4 total: " + colTotal4);
  131.         System.out.println("Diagonal 1 total: " + diagTotal1);
  132.         System.out.println("Diagonal 2 total: " + diagTotal2);
  133.        
  134.        
  135.         //Check if it is a magic square
  136.         boolean magic = false;
  137.         if (input1 == 2)
  138.             if (rowTotal1 == rowTotal2 && rowTotal1 == colTotal1 && rowTotal1 == colTotal2
  139.                 && rowTotal1 == diagTotal1 && rowTotal1 == diagTotal2)
  140.                     magic = true;
  141.             else
  142.                 magic = false;
  143.                
  144.                        
  145.  
  146.         if (input1 == 3)
  147.             if (rowTotal1 == rowTotal2 && rowTotal1 == rowTotal3 && rowTotal1 == colTotal1
  148.             && rowTotal1 == colTotal2 && rowTotal1 == colTotal3 && rowTotal1 == diagTotal1 && rowTotal1 == diagTotal2)
  149.                 magic = true;
  150.             else
  151.                 magic = false;
  152.        
  153.         if (input1 == 4)
  154.             if (rowTotal1 == rowTotal2 && rowTotal1 == rowTotal3 && rowTotal1 == rowTotal4 && rowTotal1 == colTotal1
  155.             && rowTotal1 == colTotal2 && rowTotal1 == colTotal3 && rowTotal1 == colTotal4 && rowTotal1 == diagTotal1 && rowTotal1 == diagTotal2)
  156.                 magic = true;
  157.             else
  158.                 magic = false;
  159.        
  160.        
  161.         //Print if it is magic
  162.         if (magic)
  163.             System.out.println("The Matrix is a magic square.");
  164.         else
  165.             System.out.println("The Matrix is not a magic square.");
  166.  
  167.     }
  168.  
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement