I seem to be having trouble with the three user created methods and calling them correctly in the main method. I don't have any syntax errors that I know of, so that leaves logical ones, the parameters look fine too.
The program is suppose to have an output similar to the last output below. It's a program that takes a 2D array and checks if it's a magic square. Each method has a responsibility regarding the vetting/checking process to determine if it's a magic square or not.
I'd really appreciate some help getting this working, I'm stumped.
Code:
import java.util.Arrays;
public class Magic10
{
// A method to check whether rows equal the target sum
private static boolean rowsEqTargetSum(int[][] a, int targetSum, int n, int row, int col, int sum)
{
// Calculate the sum of each row ... if magic, then equal to targetSum
for(row=0; row<n; row++)
{
System.out.print("row "+row+": ");
for(col=0; col<n; col++)
{
int value = a[row][col];
sum += value;
if (col > 0)
System.out.print(" + "); // print plus before all except 1st
System.out.print(value);
}
System.out.println(" = "+sum);
if(sum != targetSum)
{
System.out.println("Row sum incorrect : Not a magic Square!");
}
}
return rowsEqTargetSum(null, 0, sum, sum, sum, sum);
}
// A method to check whether diagonals equal the target sum
private static boolean diagonalEqTargetSum(int[][] a, int targetSum, int n, int row, int col, int sum)
{
System.out.print("diagonal: ");
for(int pos=0; pos<n; pos++)
{
row = n-1 - pos;
col = pos;
int value = a[row][col];
sum += value;
if (pos > 0)
System.out.print(" + "); // print plus before all except 1st
System.out.print(value);
}
System.out.println(" = "+sum);
if(sum != targetSum)
{
System.out.println("Diagonal is incorrect : Not a magic Square!");
}
return diagonalEqTargetSum(null, 0, 0, sum, sum, sum);
}
// A method to check whether all numbers are used exactly once in the 2D array
private static boolean allNumbersRepresented(int[][] a, int n, int col, int row)
{
// Lastly, we check that every number from 1 to n is represented
final int nSquare=n*n;
boolean[] flag= new boolean[n*n];
for(row=0; row<n; row++)
{
for(col=0; col<n; col++)
{
int num = a[row][col];
if (n < 1 || num > nSquare)
{
System.out.println("Number out of range : Not a magic Square!");
}
if (flag[num-1])
{
System.out.println("Duplicate number : Not a magic Square!");
}
flag[num-1] = true;
}
}
return allNumbersRepresented(null, 0, 0, 0);
}
public static void main(String []args)
{
int[][] a ={{4,9,2},
{3,5,7},
{8,1,6}};
final int n=a.length;
final int targetSum=n*(n*n+1)/2;
System.out.println(" The following two dimensional array is Magic!");
for (int i = 0;i< a.length;i++)
{
System.out.println(Arrays.toString(a[i]));
}
// Calls rowsEqTargetSum Method
if (!rowsEqTargetSum(a, targetSum, targetSum, targetSum, targetSum, targetSum))
{
return;
}
// Calls diagonalEqTargetSum Method
if (!diagonalEqTargetSum(a, targetSum, targetSum, targetSum, targetSum, targetSum))
{
return;
}
// Calls allNumbersRepresented Method
if (!allNumbersRepresented(a, targetSum, targetSum, targetSum))
{
return;
}
}
}
Current Output (Something is wrong with calling the methods?)
The following two dimensional array is Magic!
[4, 9, 2]
[3, 5, 7]
[8, 1, 6]
row 0: 4 + 9 + 2Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at Magic10.rowsEqTargetSum(Magic10.java:14)
at Magic10.main(Magic10.java:91)
Output looking somewhat like this:
row 0: 4 + 9 + 2 = 15
row 1: 3 + 5 + 7 = 15
row 2: 8 + 1 + 6 = 15
diagonal: 8 + 5 + 2 = 15
The following two dimensional array is Magic !
[4, 9, 2]
[3, 5, 7]
[8, 1, 6]