I wrote the java code about how to create magic square. However, i don\'t know w
ID: 3641899 • Letter: I
Question
I wrote the java code about how to create magic square. However, i don't know what happened to my code, but when i run it it will not show any result. Here's code. I just want someone check it and tell me how to fix it.import java.util.*;
public class Problem13
{
static Scanner console = new Scanner(System.in);
static final int ROWS = 4;
static final int COLUMNS = 4;
static final int LISTSIZE = 16;
public static void main(String[] args)
{
int[] list = new int[LISTSIZE];
int[][] matrix = new int [ROWS][COLUMNS];
createArithmeticSeq(list);
matricize(list,matrix);
printMatrix(matrix);
reverseDiagonal(matrix);
printMatrix(matrix);
magicCheck(list,matrix);
}
public static void createArithmeticSeq(int [] sequence)
{
int first;
int diff;
int i;
System.out.println("Please enter initial number: ");
first = console.nextInt();
System.out.println("Please enter the difference: ");
diff = console.nextInt();
for (i = 0; i < 16; i++)
{
sequence[i] = first + diff*i;
}
}
public static void matricize(int [] single,int [][] matrice)
{
int row;
int col;
for (row =0; row < matrice.length; col++)
{
for (col = 0; col < matrice[row].length; col++)
single[row*4 + col] = matrice[row][col];
}
}
public static void reverseDiagonal(int[][] matrix)
{
int row, col;
int temp;
for (row = 0; row < matrix.length / 2; row++)
{
temp = matrix[row][row];
matrix[row][row] = matrix[matrix.length - 1 - row][matrix.length - 1 - row];
matrix[matrix.length - 1 - row][matrix.length - 1 - row] = temp;
}
for (row = 0; row < matrix.length / 2; row++)
{
temp = matrix[row][matrix.length - 1 - row];
matrix[row][matrix.length -1 - row] = matrix[matrix.length - 1 - row][row];
matrix[matrix.length - 1 - row][row] = temp;
}
}
public static void printMatrix(int [][] matrice)
{
int row,col;
for (row = 0; row < matrice.length; row++)
{
for (col = 0; col < matrice[row].length; col++)
System.out.printf("%5d", matrice[row][col]);
System.out.println();
}
}
public static void magicCheck(int [] sequence, int [][] matrix)
{
int sum = 0, magicNumber, row, col;
int rowSum1 = 0, rowSum2 = 0, rowSum3 = 0, rowSum = 0, colSum1 = 0, colSum2 = 0, colSum3 = 0, colSum = 0, diagSum1 = 0, diagSum = 0;
for (int j = 0; j < sequence.length; j++)
{
sum = sum + sequence[j];
}
magicNumber = sum / 4;
// Sum of Rows
for (col = 0; col < matrix.length; col++)
{
row = 0;
rowSum = rowSum + matrix[row][col];
}
for (col = 0; col < matrix.length; col++)
{
row = 1;
rowSum1 = rowSum1 + matrix[row][col];
}
for (col = 0; col < matrix.length; col++)
{
row = 2;
rowSum2 = rowSum2 + matrix[row][col];
}
for (col = 0; col < matrix.length; col++)
{
row = 0;
rowSum3 = rowSum3 + matrix[row][col];
}
// ...
// Sum of Columns
for (row = 0; row < matrix.length; row++)
{
col = 0;
colSum = colSum + matrix[row][col];
}
for (row = 0; row < matrix.length; row++)
{
col = 1;
colSum1 = colSum1 + matrix[row][col];
}
for (row = 0; row < matrix.length; row++)
{
col = 2;
colSum2 = colSum2 + matrix[row][col];
}
for (row = 0; row < matrix.length; row++)
{
col = 3;
colSum3 = colSum3 + matrix[row][col];
}
// ...
// Sum of Diagonals
for (row = 0,col = 0; row < matrix.length && col < matrix.length; row++)
{
diagSum = diagSum + matrix[row][col];
col = col++;
}
for (col = 0, row = 3; col < matrix.length && row < matrix.length; col++)
{
diagSum1 = diagSum1 + matrix[row][col];
row--;
}
if (rowSum == magicNumber && rowSum1 == magicNumber && rowSum2 == magicNumber && rowSum3 == magicNumber &&
colSum == magicNumber && colSum1 == magicNumber && colSum2 == magicNumber && colSum3 == magicNumber &&
diagSum == magicNumber && diagSum1 == magicNumber)
{
System.out.println("It is a magic square.");
}
else
System.out.println("It's not ");
}
}