Question

I am using C++ Write a program to determine if a gird follows the rules to...

I am using C++

Write a program to determine if a gird follows the rules to be classified as a Lo Shu Magic Square. The Lo Shu Magic Square is a grid with 3 rows and 3 columns shown in figure below. 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 below.
  • No number is repeated in the square

In a program, you can simulate a magic square using a two-dimensional array. In this assignment, you will write a program with the following structure:

  • Two or more 3x3 integer arrays with test data
  • A function isMagicSquare
  • A function showArray
  • The main function

The function isMagicSquare will accept a two-dimensional array as an argument, and determines whether the array is a Lo Shu Magic Square. The function prototype is given below. This must work by testing each criterion of Magic Square: are the numbers unique? What are the sums of diagonals? What are the sums in each row, each column? You will need a series of loops, at least two: one loop adding up each row and testing its sum, another adding columns. You may check the other criteria in any way you wish.

Do not simply compare a test array against a known Magic Square array.

The function showArray, also has a prototype below; it accepts a two-dimensional array as an argument and prints it out neatly and clearly.

The main function will go through all the test arrays. For each array, it will use showArray to display the contents, then run isMagicSquare and report whether that test array is a magic square or not.

You must have at least two arrays to test. One array must be a magic square, the other must not be a magic square. For the magic-square array, you may use the magic square in the image given before, or the one below, or any other magic square that you come up with yourself. You do not have to make the user enter any arrays by hand when the program runs. For this assignment, it is acceptable to just initialize your test arrays with hard-coded values.

The following sample code contains function prototypes and other pieces to guide your work.

// The following function test all rules and returns true only
// if it succeeds in each!
bool isMagicSquare(int values[][COLS]); 

// This function format it nicely.
void showArray(int values[][COLS]);

int main() {
   // Create a two-dimensional array that does not satisfy Magic Square requirements
   int normalArray[ROWS][COLS] = {
      { 1, 2, 3 },
      { 4, 5, 6 },
      { 7, 8, 9 } };

   // Create a test two-dimensional array that does satisfy Magic Square requirements
   int magicArrayExample[ROWS][COLS] = { 
      { 4, 3, 8 },
      { 9, 5, 1 },
      { 2, 7, 6 } };

   // Maybe a third two-dimensional array
   int otherArray[ROWS][COLS] = {
      { 7, 7, 7 },
      { 7, 7, 7 },
      { 7, 7, 7 } };
 
The output should look something like this:
1 2 3
4 5 6
7 8 9
This is not a Lo Shu magic square.

4 3 8
9 5 1
2 7 6
This is a Lo Shu magic square.

7 7 7
7 7 7
7 7 7
This is not a Lo Shu magic square.


Use the repo from the link above to start and submit your assignment. Include the standard header and comments in your source code. Compile, run, and test your program. Make sure there are no errors and it functions properly. You can hit [Ctrl or command]+[Shift]+B to compile and run the program. Your program must run without errors to get full credit! Commit and push (upload) your repo, including your cpp file to GitHub for grading. It must be named correctly and saved as a readable cpp file with the proper extension.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>

#define ROWS 3

#define COLS 3

using namespace std;

// The following function test all rules and returns true only

// if it succeeds in each!

bool isMagicSquare(int matrix[][COLS]){


int n = COLS;

    int elements = n * n;

    int totalMatrixSum = (n * n * (n * n + 1) / 2) / n;

    int sumOfRow = 0, sumOfColoumns = 0, sumOfPrimaryDiagonal = 0, sumOfSecondaryDiagonal = 0;


    bool *visited = new bool[n * n];

    for (int i = 0; i < n; i++) {

      // calculates sum of row

      sumOfRow = 0;

      // calculates sum of column

      sumOfColoumns = 0;

      for (int j = 0; j < n; j++) {

        // Check if elements is less than 1 or Elements is more than sum

        // inteded

        if (matrix[i][j] < 1 || matrix[i][j] > elements)

          return false;

        // For duplicated, if duplicate value not found return false;

        if (visited[matrix[i][j] - 1] == true)

          return false;

        // Keep track of elements in visited array

        visited[matrix[i][j] - 1] = true;

        // calculate sum of rows

        sumOfRow += matrix[i][j];

        // calculate sum of column

        sumOfColoumns += matrix[j][i];

      }

      // calculate sum of primary diagonal

      sumOfPrimaryDiagonal += matrix[i][i];

      // calculate sum of secondary diagonal

      sumOfSecondaryDiagonal += matrix[i][n - i - 1];

      // check for magic square condition

      if (sumOfRow != totalMatrixSum || sumOfColoumns != totalMatrixSum)

        return false;

    }

    // check for magic square condition

    if (sumOfPrimaryDiagonal != totalMatrixSum || sumOfSecondaryDiagonal != totalMatrixSum)

      return false;

    return true;

}

// This function format it nicely.

void showArray(int values[][COLS]){

for(int i=0 ; i<COLS ; ++i){

for(int j= 0 ; j<COLS;++j){

cout<<values[i][j] <<" ";

}

cout<<endl;

}

}

int main() {

// Create a magic two-dimensional array.

int magicArray[ROWS][COLS] = {

{ 4, 9, 2 },

{ 3, 5, 7 },

{ 8, 1, 6 } };

// Create a normal two-dimensional array.

int normalArray[ROWS][COLS] = {

{ 1, 2, 3 },

{ 4, 5, 6 },

{ 7, 8, 9 } };

// Create a normal two-dimensional array.

int otherArray[ROWS][COLS] = {

{ 7, 7, 7 },

{ 7, 7, 7 },

{ 7, 7, 7 } };



showArray(normalArray);

if(isMagicSquare(normalArray))

cout<<"This is a Lo Shu magic square. "<<endl;

else

cout<<"This is not a Lo Shu magic square. "<<endl;

showArray(magicArray);

if(isMagicSquare(magicArray))

cout<<"This is a Lo Shu magic square. "<<endl;

else

cout<<"This is not a Lo Shu magic square. "<<endl;



showArray(otherArray);

if(isMagicSquare(otherArray))

cout<<"This is a Lo Shu magic square. "<<endl;

else

cout<<"This is not a Lo Shu magic square. "<<endl;

}

==============================================
SEE OUTPUT

PLEASE COMMENT if there is any concern.

==============================

Add a comment
Know the answer?
Add Answer to:
I am using C++ Write a program to determine if a gird follows the rules to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 9. Lo Shu Magic Square The Lo Shu Magic Square is a grid with three rows and three columns that h...

    9. Lo Shu Magic Square The Lo Shu Magic Square is a grid with three rows and three columns that 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 the following figure · 15 4 9 2-15 3 5 7-15 81 615 15 15 15 15 Write a program that simulates a magic square using...

  • I need help as quick as possible, thanks beforehand. Please provide the test output The Lo...

    I need help as quick as possible, thanks beforehand. Please provide the test output The Lo Shu Magic Square is a grid with 3 rows and 3 columns shown below. 35 The Lo Shu Magic Square has the following properties: The grid contains the numbers 1 - 9 exactly The sum of each row, each column and each diagonal all add up to the same number. This is shown below: 15 4 92 15 - 81 + 15 15 15...

  • Concepts tested by the program: Working with one dimensional parallel arrays Use of functions Use of...

    Concepts tested by the program: Working with one dimensional parallel arrays Use of functions Use of loops and conditional statements Description The Lo Shu Magic Square is a grid with 3 rows and 3 columns shown below. The Lo Shu Magic Square has the following properties: The grid contains the numbers 1 – 9 exactly The sum of each row, each column and each diagonal all add up to the same number. s is shown below: Write a program that...

  • please use java language please used ArrayList The Lo Shu Magic Square is a grid with...

    please use java language please used ArrayList The Lo Shu Magic Square is a grid with 3 rows and 3 columns, shown in Figure 7-31. The • The sum of each row, each column, and each diagonal all add up to the same number 20. Lo Shu Magic Square Lo Shu Magic Square has the following properties: • The grid contains the numbers 1 through 9 exactly. This is shown in Figure 7-32. In a program you can simulate a...

  • In Java Please Create A Program For The Following; Please Note: This program should be able...

    In Java Please Create A Program For The Following; Please Note: This program should be able accept changes to the values of constants ROWS and COLS when testing the codes. Switch2DRows Given a 2D array with ROWS rows and COLS columns of random numbers 0-9, re-order the rows such that the row with the highest row sum is switched with the first row. You can assume that 2D arrau represents a rectangular matrix (i.e. it is not ragged). Sample run:...

  • Help pls! and kindly explain how you do this as well. Thaank you Write a program...

    Help pls! and kindly explain how you do this as well. Thaank you Write a program to test whether a square is a 3x3 magic square. A magic square is a grid with 3 rows and 3 columns, like the figure below. A magic square has the following properties: the grid contains only the numbers 1 through 9 the sum of each row, each column, and each diagonal all add up to the same number Notes: I have provided the...

  • IN C++ Write a program that uses a 3x3 array and randomly places each integer from...

    IN C++ Write a program that uses a 3x3 array and randomly places each integer from 1 to 9 into the nine squares. The program calculates the "magic number" by adding all the numbers in the array and then dividing the sum by 3. The 3x3 array is said to be a "magic square" if the sum of each row, each column, and each diagonal is equal to the magic number. Your program must use at least the following functions:...

  • Use basic java for this after importing PrintWriter object You will make a simple Magic Square...

    Use basic java for this after importing PrintWriter object You will make a simple Magic Square program for this Java Programming Assignment. Carefully read all the instructions before beginning to code. It is also required to turn in your pseudocode or a flowchart along with this program. Here are the instructions: At the beginning of the program, briefly describe to the user what a Magic Square Matrix is (described further on), and then allow them to enter “start” to begin...

  • C++: Write a program that uses a 3 X 3 array and randomly place each integer...

    C++: Write a program that uses a 3 X 3 array and randomly place each integer from 1 to 9 into the nine squares. The program calculates the magic number by adding all the numbers in the array and then dividing the sum by 3. The 3 X 3 array is a magic square if the sum of each row, each column, and each diagonal is equal to the magic number. Your program must contain at least the following functions:...

  • Please solve only if you know how to do it. Write the code using C++ (not...

    Please solve only if you know how to do it. Write the code using C++ (not Python or Java). Show and explain everything neatly. COMMENTS (7.5% of programming assignment grade): Your program should have at least ten (10) different detailed comments explaining the different parts of your program. Each individual comment should be, at a minimum, a sentence explaining a particular part of your code. You should make each comment as detailed as necessary to fully explain your code. You...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT