Getting sum of columns and rows of 2d array/magic square

• 02-27-2012, 10:13 PM
bdl1127
Getting sum of columns and rows of 2d array/magic square
Need to use this method to get the sum of columns and rows in a 2d array. In order for it to be considered a magic row, each column and each row must add up to be 15. I got the program to confirm that all the rows were equal to 15, however I am confused as how to do the columns. This is where I am at. Suggestions?

public static boolean test(int[] [] tester) {

boolean bool = true;

int sumRows[] = new int[tester.length];
int sumColumns[] = new int[tester[0].length];

int sumR = 0;
int sumC = 0;

for (int a = 0; a < tester.length; a++){
for(int b = 0; b < tester[0].length; b++){
sumR += tester[a][b];

sumRows[a] = sumR;
if (a==0){
sumR = 0;
}
if (a>0) {
if (sumR != sumRows[a-1]){
System.out.println("This is not a magic square");
bool = false;
break;
}else{
sumR = 0;
}
}
sumColumns[b] = sumC;
if (b==0){
sumC=0;
}
if (b>0) {
if (sumC != sumColumns[b-1]){
bool = false;
}else
sumC = 0;
}
}
}
return bool;

}