I wrote this code(with a little help) that makes a magic square. Now I need to sum the rows and columns and diagonals. I can't figure out what i'm doing wrong.
Lines 57 thru 63 are my attempt at doing so. Can someone help me figure out what i'm doing wrong? I've tried adjusting everything I can think of with no luck.
bool magicSquare(int **matrix,int dimension) // add rows, columns, and diagonals
{
int firstSum = 0, sum;
bool magic = true;
for (int r = 0; r < dimension; r++) // add 1st column for a comparison
firstSum += matrix[r][1];
for (int r = 1; r < dimension; r++) // row loop first when adding rows
{
sum = 0;
for ( int c = 0; c < dimension; c++)
sum += matrix[r][c]; // add row
if ( sum != firstSum) // check for magic failure
return (false); // not magic
}
for ( int c = 0; c < dimension; c++) // column loop first when adding columns
{
sum = 0;
for (int r = 0; r < dimension; r++)
sum += matrix[r][c]; // add columns
if ( sum != firstSum) // check for magic failure
return (false); // not magic
}
sum = 0;
for (int r = 0; r < dimension; r++)
sum += matrix[r][r]; // add front diagonal
if ( sum != firstSum) // check for magic failure
return (false); // not magic
sum = 0;
for (int r = 0; r < dimension; r++)
sum += matrix[r][dimension - r - 1]; // add back diagonal
if ( sum != firstSum) // check for magic failure
return (false); // not magic
elsereturn (true);
} // end magicSquare function