In: Computer Science
You will put a possible magic square into a two dimensional array and determine if it is a magic square or not. Some of the code is completed. You can assume that it is a square array and that all of the integers are unique (no repeats)
CODE:
package TwoDimensionalArrays;
public class MagicSquare {
   public static boolean
checkMagicSquare(int[][]array)
   {   //Pre: Assume that the array is a square
two dimensional array
       //Pre: Assume that there are no
repeat integers
       int sum=0;
       for(int
col=0;col<array[0].length;col++)
       {
          
sum=sum+array[0][col];
       }
       //sum will represent the sum of
every row, every col and the diagonals
      
if(checkRows(array,sum)&&checkCols(array,sum)&&checkDiagonals(array,sum))
           return
true;
       else
           return
false;
   }
   public static boolean checkRows(int[][]a,int
sum)
   {
      
       for(int
row=0;row<a.length;row++)
       {
           int
rowSum=0;
           for(int
col=0;col<a[0].length;col++) {
          
    rowSum+=a[row][col];
           }//end column
loop
          
if(rowSum!=sum)
          
    return false;   //This will jump out
of the method and return false if not magic square
       }//end row loop
      
       return true;    
        //The rows all add up to the
same value
   }
   public static boolean checkCols(int[][]a,int
sum)
   {
      
      
       return true;    
        //The rows all add up to the
same value
   }
   public static boolean checkDiagonals(int[][]a,int
sum)
   {   int leftDiagonalSum=0;
       int rightDiagonalSum=0;
      
      
   }
   public static void main(String[] args) {
       // TODO Auto-generated method
stub
       int[][]array1=
{{6,1,8},{7,5,3},{2,9,4}};
       int[][]array2=
{{9,1,5},{3,4,8},{6,7,2}};
      
System.out.println(checkMagicSquare(array1));
      
System.out.println(checkMagicSquare(array2));
   }
}
/*
true
false
*/
CODE:
public class MagicSquare{
public static boolean checkMagicSquare(int[][]array)
{ //Pre: Assume that the array is a square two dimensional
array
//Pre: Assume that there are no repeat integers
int sum=0;
for(int col=0;col<array[0].length;col++)
{
sum=sum+array[0][col];
}
//sum will represent the sum of every row, every col and the
diagonals
if(checkRows(array,sum)&&checkCols(array,sum)&&checkDiagonals(array,sum))
return true;
else
return false;
}
public static boolean checkRows(int[][]a,int sum)
{
  
for(int row=0;row<a.length;row++)
{
int rowSum=0;
for(int col=0;col<a[0].length;col++) {
rowSum+=a[row][col];
}//end column loop
if(rowSum!=sum)
return false; //This will jump out of the method and return false
if not magic square
}//end row loop
  
return true; //The rows all add up to the same value
}
public static boolean checkCols(int[][]a,int sum)
{
for(int col=0;col<a[0].length;col++)
{
int colSum=0;
for(int row=0;row<a.length;row++) {
colSum+=a[row][col];
}//end row loop
if(colSum!=sum)
return false; //This will jump out of the method and return false
if not magic square
}//end col loop
  
return true; //The volumns all add up to the same value
}
public static boolean checkDiagonals(int[][]a,int sum)
{ int leftDiagonalSum=0;
int rightDiagonalSum=0;
  
for(int row=0;row<a.length;row++)
{
int rowSum=0;
for(int col=0;col<a[0].length;col++) {
if(row == col)
{
leftDiagonalSum +=a[row][col];
}
if(row + col == a.length - 1)
{
rightDiagonalSum += a[row][col];
}
}//end column loop
  
}//end row loop
  
return leftDiagonalSum == sum && rightDiagonalSum ==
sum;
  
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[][]array1= {{6,1,8},{7,5,3},{2,9,4}};
int[][]array2= {{9,1,5},{3,4,8},{6,7,2}};
System.out.println(checkMagicSquare(array1));
System.out.println(checkMagicSquare(array2));
}
}
OUTPUT:
