Lo Shu Magic Square The Lo Shu Magic Square is a grid with 3

Lo Shu Magic Square

The Lo Shu Magic Square is a grid with 3 rows and 3 columns shown in figure 7-19. The Lo Shu magic Square has the following properties;

- The grid contains the numbers 1 through 9 exactly.

-The sum of each row, each column, and each diagonal all add up to the same number. This is shown in figure 7-20.

In a program you can simulate a magic square using a two-dimentional array. Write a function that accepts a two-dimensional array as an argument, and determines whether the array is a Lo Shu Magic Square. Test the function in a program.

The following functions must be used for the various functions needed in this program. Please help, I do not understand what to do.

showArray. This function accepts a two-dimensional int array as an argument and displays its contents.       

checkRange. This function accepts a two-dimensional int array as an

argument and returns true if the values are within the specified range

(1 - 9). Otherwise, it returns false.

checkRowSum. This function accepts a two-dimensional int array as an

argument, and returns true if the sum of the values in each of the array\'s rows are equal. Otherwise, it returns false.

checkColSum. This function accepts a two-dimensional int array as an

argument, and returns true if the sum of the values in each of the array\'s columns are equal. Otherwise, it returns false.

checkDiagSum. This function accepts a two-dimensional int array as an argument, and returns true if the sum of the values in each of the array\'s diagonals are equal. Otherwise, it returns false.

isMagicSquare. This function accepts a two-dimensional int array as an argument, tests to determine if it is a Lo Shu Magic Square and displays the result.

Solution

#include <iostream>
using namespace std;
/*showArray. This function accepts a two-dimensional int array as an argument and displays its contents.     */
void showArray(int square[][3])  
{
cout<<endl<<\"Magic Square\"<<endl;
for(int i=0;i<3;i++)
{
  for(int j=0;j<3;j++)
  {
   cout<<square[i][j]<<\" \";
  }
  cout<<endl;
}
}

/*checkRange. This function accepts a two-dimensional int array as an
argument and returns true if the values are within the specified range
(1 - 9). Otherwise, it returns false. */
bool checkRange(int square[][3])  
{

for(int i=0;i<3;i++)
{
  for(int j=0;j<3;j++)
  {
   if(!(square[i][j]>=1 && square[i][j]<=9))
   {
    return false;
   }
  }
return true;
}
}
/*checkRowSum. This function accepts a two-dimensional int array as an
argument, and returns true if the sum of the values in each of the array\'s rows are equal. Otherwise, it returns false.*/
bool checkRowSum(int square[][3])  
{
    int sum[3],prev;
for(int i=0;i<3;i++)
{
  sum[i]=0;
  for(int j=0;j<3;j++)
  {
   sum[i]+=square[i][j];
  }
    }
    if(sum[0]==sum[1] && sum[1]==sum[2])
return true;
else
return false;
}
/*checkColSum. This function accepts a two-dimensional int array as an
argument, and returns true if the sum of the values in each of the array\'s columns are equal. Otherwise, it returns false.*/
bool checkColSum(int square[][3])  
{
    int sum[3];
for(int i=0;i<3;i++)
{
  sum[i]=0;
  for(int j=0;j<3;j++)
  {
   sum[i]+=square[j][i];
  }
    }
    if(sum[0]==sum[1] && sum[1]==sum[2])
return true;
else
return false;
}
/*checkDiagSum. This function accepts a two-dimensional int array as an argument, and returns true if the sum of the values in each of the
array\'s diagonals are equal. Otherwise, it returns false. */
bool checkDiagSum(int square[][3])  
{
    int sum1,sum2;
sum1=square[0][0]+square[1][1]+square[2][2];
sum2=square[0][2]+square[1][1]+square[2][0];
    if(sum1==sum2 )
return true;
else
return false;
}
/*isMagicSquare. This function accepts a two-dimensional int array as an argument, tests to determine if it is a Lo Shu Magic
Square and displays the result.*/

bool isMagicSquare(int square[][3])  
{
    if(checkRange(square) &&checkRowSum(square) &&checkColSum(square) &&checkDiagSum(square))
return true;
else
return false;
}

int main()
{
int square[][3]={{1,4,5},{2,4,3},{5,1,8}};
if(isMagicSquare(square))
{
  showArray(square);
  cout<<endl<<endl<<\"This is a magic square\";
}
else
{
     showArray(square);
  cout<<endl<<endl<<\"This is not a magic square\";
}
}

Lo Shu Magic Square The Lo Shu Magic Square is a grid with 3 rows and 3 columns shown in figure 7-19. The Lo Shu magic Square has the following properties; - Th
Lo Shu Magic Square The Lo Shu Magic Square is a grid with 3 rows and 3 columns shown in figure 7-19. The Lo Shu magic Square has the following properties; - Th
Lo Shu Magic Square The Lo Shu Magic Square is a grid with 3 rows and 3 columns shown in figure 7-19. The Lo Shu magic Square has the following properties; - Th

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site