2 7 6 9 5 1 4 3 8If you add up any row, colum, or diagonal, the sum of the numbers will be 15.
Magic squares come in all sizes--they are not all 3x3.
Write the predicate isMagicSquare(int[][] square) that takes an nxn array of ints and returns #t if the array contains a magic square and #f otherwise. You may assume that the input to isMagicSquare has the same number of rows as it does columns.
isMagicSquare needs to work on ALL squares, not just the example or 3x3 squares. If you write isMagicSquare so that it works only on the sample data, that is not The Idea.
Here is some code that you might find useful:
public class Main { public static void main(String[] args) { int[][] test = { {2, 7, 6}, // Nothing wrong with this sample 3x3 array, {9, 5, 1}, // but isMagicSquare needs to work for ANY {4, 3, 8} }; // nxn array print2DArray(test); System.out.println(isMagicSquare(test)); } public static boolean isMagicSquare(int[][] arr) { int sum = 0; for (int column = 0; column < arr.length; column++) { sum += arr[0][column]; // Add up elements of row 0 } return (rowsOK(arr, sum) && colsOK(arr, sum) && diagsOK(arr, sum) && correctElements(arr)); } public static boolean rowsOK(int[][] arr, int sum) { // Replace the below stub with real code return false; } public static boolean colsOK(int[][] arr, int sum) { // Replace the below stub with real code return false; } public static boolean diagsOK(int[][] arr, int sum) { // Replace the below stub with real code return false; } public static boolean correctElements(int[][] arr) { // Verify that the numbers from 1 to n^2 are all in the array return false; } // Print utility for a 2-dimensional array of ints public static void print2DArray(int[][] arr) { for (int row = 0; row < arr.length; row++) { for (int col = 0; col < arr[0].length; col++) { System.out.print(arr[row][col] + " "); } System.out.println(); } } }