sum the rows and columns of a magic square

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.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677`` ``````#include #include using namespace std; int size(bool flag) { int test; if(flag) { do { test=(int)(double(rand())/double(RAND_MAX)*14.0+3.0); }while(test%2==0); return test; } else return 7; } int main() { int n = 7; int row, col, size; int magic[19][19]; for(int j=0 ; j
here is my concept for my last time project
your looping not wrong. but can create a new variable instead you use magic[n+1]

 ``12345678910111213141516171819202122232425262728293031323334353637`` ``````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 else return (true); } // end magicSquare function ``````
Topic archived. No new replies allowed.