Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

/* complete the code for the following program which determines if a 2 dim array

ID: 3626530 • Letter: #

Question

/* complete the code for the following program which determines if a 2 dim array is a magic square.
a magic square is an array whose rows, cols, and diagonals all sum to the same number. Complete the function isMagic
which fins the sum of all the rows,columns, and diagonals by calling the function summer. Complete the function summer
which sums the array passed to it and returns the sum.*/

#include <iostream>
using namespace std;

const int NUMRC=3;

bool isMagic(int [NUMRC] [NUMRC] );
int summer(int [NUMRC]);
int main()
{
int square1[NUMRC] [NUMRC]= { {2,7,6},{9,5,1},{4,3,8}};
if (isMagic(square1))
cout <<"The square is a magic square ";
else
cout <<"The square is not a magic square ";
}
bool isMagic(int square[][_____])
{
const int totalsums = 2*______ + 2;//compute how many sums needed
int columnOrDiagcout [NUMRC];//array to store column or diagonal of numbrs
int sums[________];//array to hold the sums of all rows,columns, and diagonals
for(int i=0; i<NUMRC ; i++)
_____[i] = summer(square[_____]); //store sum for ith rows
for ( int i=0; i<NUMRC; i++)
{
for int j=0; j<NUMRC ; j++)
columnOrDiag[j] = square[__][__];
sums[NUMRC+i]=summer(_______________);
}
for (int j=0; j<NUMRC ; j++)//loop to load the values of the 1st diagonal from top left to lower right
columnOrDiag[j]=_____[__][___];
sums[2*NUMRC] = summer(______);//find the sum of the first diagonal
for ( int j=NUMRC-1; j>=0; j++)
_______[j]=square[j][_______];
sums[_____+1]=summer(columnOrDiag);//find the sum of the second diagonal
for( int j=1; j<2*NUMRC+2; j++)//test if all sum values are equal
if (____[j-1] !=_____[j])
retun false;
return true;
}
int summer(int array[NUMRC])
{
int sum = 0;
for (int i=0; _____;i++)
______+=_________
return sum;
}

Explanation / Answer

#include <iostream>
using namespace std;

const int NUMRC=3;

bool isMagic(int [NUMRC] [NUMRC] );
int summer(int [NUMRC]);
int main()
{
int square1[NUMRC] [NUMRC]= { {2,7,6},{9,5,1},{4,3,8}};
if (isMagic(square1))
cout <<"The square is a magic square ";
else
cout <<"The square is not a magic square ";
}
bool isMagic(int square[][NUMRC])
{
const int totalsums = 2*NUMRC+ 2;//compute how many sums needed
int columnOrDiagcout [NUMRC];//array to store column or diagonal of numbrs
int sums[totalsums];//array to hold the sums of all rows,columns, and diagonals
for(int i=0; i<NUMRC ; i++)
sums[i] = summer(square[i]); //store sum for ith rows
for ( int i=0; i<NUMRC; i++)
{
for int j=0; j<NUMRC ; j++)
columnOrDiag[j] = square[i][j];
sums[NUMRC+i]=summer(columnOrDiag[j]);
}
for (int j=0; j<NUMRC ; j++)//loop to load the values of the 1st diagonal from top left to lower right
columnOrDiag[j]=square[i][j];
sums[2*NUMRC] = summer(columnOrDiag[j]);//find the sum of the first diagonal
for ( int j=NUMRC-1; j>=0; j++)
sums[j]=square[j][j];
sums[NUMRC+1]=summer(columnOrDiag);//find the sum of the second diagonal
for( int j=1; j<2*NUMRC+2; j++)//test if all sum values are equal
if (sums[j-1] !=sums[j])
retun false;
return true;
}
int summer(int array[NUMRC])
{
int sum = 0;
for (int i=0; i<NUMRC;i++)
sum+= array[i];
return sum;
}