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

Lo Shu magic Square The Lo Shu magic Square is grid with 3 rows and 3 columns. T

ID: 3566029 • Letter: L

Question

Lo Shu magic Square

The Lo Shu magic Square is grid with 3 rows and 3 columns.

The Lo Shu magic Square has the following properties: 1)The grid contains the numbers 1 through 9 exactly. 2)The sum of each row, each column, and each diagonal all add up to same number(15). In a program you can simulate a magic square using a two dimentional array. Write a function that accepts two-dimentional array as an argument, and determine whether the array is Lo Shu magic Square.Test the function in a program.

Explanation / Answer

>   #include <iostream>
>   using namespace std;
>  
>   bool verifyNumbers1to9(int grid[3][3]){
>       bool array[9];
>       for(int i=0; i<9; i++)
>           array[i] = false;
>  
>       for(int i=0; i<3; i++){
>           for(int j=0; j<3; j++){
>  
>               if(grid[i][j] > 9 && grid[i][j] < 1)
>                   return false;
>  
>               array[grid[i][j]-1] = true;
>           }
>       }
>  
>       for(int i=0; i<9; i++){
>           if(!array[i])
>               return false;
>       }
>   }
>  
>   bool verifyMagicSquare(int grid[3][3])
>   {
>       if(!verifyNumbers1to9(grid)){
>           return false;
>       }
>  
>       int sum = grid[0][0] + grid[0][1] + grid[0][2];
>  
>       for(int i=1; i<3; i++){
>           if(sum != grid[i][0] + grid[i][1] + grid[i][2]){
>               return false;
>           }
>       }
>  
>       for(int i=0; i<3; i++){
>           if(sum != grid[0][i] + grid[1][i] + grid[2][i]){
>               return false;
>           }
>       }
>  
>       if(sum != grid[0][0] + grid[1][1] + grid[2][2]){
>           return false;
>       }
>  
>       if(sum != grid[2][0] + grid[1][1] + grid[0][2]){
>           return false;
>       }
>  
>       return true;
>   }
>  
>   int main(){
>       int grid[3][3] = {
>           {1,2,3},
>           {4,5,6},
>           {7,8,9}
>       };
>  
>       cout << verifyMagicSquare(grid) << endl;
>       return 0;
>   }

Code in action: http://ideone.com/R9Onqe

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote