Question: Write a C++ program that determines if a given N matrix is a magic square, where N Magic Square: An N Magic Square is a

Write a C++ program that determines if a given N matrix is a magic square, where N

Magic Square: An N Magic Square is a N matrix (i.e... a square N x N matrix) with positive integer values for its elements (values in X's)). An N Magic Square has the following properties:

1. It is an N Matrix

2. The integer elements values range from 1 to N squared (N*N) i.e... 1.. N*N

3. The rows, columns and diagonals all have the same sum, called the Magic Square Sum:

Magic Square Sum = [n (n2 + 1)]/ 2.

4. All the integer elements in the magics quare are unique i.e...no two elements can have the same positive integer value.

This criteria is actually checked by criteria 3. If any elements are repeated, the magic sum will not calculate correctly for at least one or more of the rows, columns or diagonals. So this criteria does not need to be explicitly checked.

Example of a 5 Magic Square:

Write a C++ program that determines if a given N matrix is

Criteria Check:

I. It is a 5 x 5 Matrix, so its a 5 square.

II. The integer elements values range from 1 to (5 x 5) i.e... 1.. 25

III. All the integer element values are distinct i.e... different from each other, no two are equal.

The rows, columns and diagonals all have the same magic sum of 65, so the Magic Sum is 65.

All the above criteria are true, so its a 5 magic square with a magic sum of 65.

The code design should be modular and use the functions in its design. Large linear code sets, especially in the main function, will lose points due to non-modular poor code structure.

Create a text file containing candidate matrices named magic.txt, which you will put in the appropriate folder in the VisualStudio solution folder.

You must include at least five (5) magic squares that you add into the magic.txt file. You can search the web for valid magic squares to add into your magic.txt file to check your program.

Also, you must add in in at least 3 invalid magic squares into your magic.txt.

The magic.txt file input data will have the following format: N will tell the number of rows and columns per row to read in for each N square.

Example:

a magic square, where N Magic Square: An N Magic Square is

Your program must be able to handle any size file of candidate matrices. Do not be concerned with error checking the file format.

You are only responsible for checking the following regarding the input file:

1. If the file open succeeded

2. Correctly detecting the end of the file

Your program will apply the aforementioned criteria to check whether or not a candidate Nmatrix is a magic square. Your program will display each matrix on the screen one at a time.

You can use #include with system(cls) to clear the screen. You can use #include with system(pause) to hold the screen. Write a program in C++ that determines if a given N matrix is a magic square, where N

The matrix display must be in a neat row and columnar format, so use the stream manipulators with cout to produced well formatted results.

Below the matrix an appropriate message will appear stating whether or not the matrix is a magic square. If the matrix is a magic square, a message should appear that will tell that the matrix is an NMagicSquare (with the value ofNfor the square)and the MagicSquareSum.

At the bottom of the matrix result screen a message will state to press any key to continue. You may use the system (pause) command for this feature.

After the last candidate matrix has been processed, the display should clear the screen and display the message: "Th-Th-That's All Folks..."

Example Output:

a N matrix (i.e... a square N x N matrix) with positive

MagicSquares.cpp:

// MagicSquares.cpp : Defines the entry point for the console application.

#include "stdafx.h" #include #include #include #include #include #include #include #include

using namespace std;

string inputFileName = "magic.txt"; const uint16_t maxSquareSize = 10; uint16_t inputSquare [maxSquareSize] [maxSquareSize] = { 0 };

bool readSquareFromFile (uint16_t [][maxSquareSize], uint16_t &, ifstream &); bool isSquareMagic (uint16_t [][maxSquareSize], uint16_t, uint16_t &); void displaySquare (uint16_t [][maxSquareSize], uint16_t); bool isSquareMagic (uint16_t [][maxSquareSize], uint16_t); bool isSquare1ToNSquared (uint16_t [][maxSquareSize], uint16_t);

int main() { uint16_t inputSquareSize, magicSum;

ifstream inputFileStreamObj; inputFileStreamObj.open(inputFileName, ios::in); if ( inputFileStreamObj.fail() ) { cout

if (!(doneBool = readSquareFromFile(inputSquare, inputSquareSize, inputFileStreamObj))) { displaySquare(inputSquare, inputSquareSize); if (!(isSquareMagic(inputSquare, inputSquareSize, magicSum))) cout

} while (!doneBool);

inputFileStreamObj.close();

cout

system("pause");

return 0; }

bool readSquareFromFile(uint16_t square[][maxSquareSize], uint16_t & squareSize, ifstream & fileStreamObj) {

return(false); }

void displaySquare(uint16_t square [] [maxSquareSize], uint16_t squareSize) {

}

bool isSquareMagic(uint16_t square [] [maxSquareSize], uint16_t squareSize, uint16_t & magicSum) { return(false); return true; }

bool isSquare1ToNSquared(uint16_t square[][maxSquareSize], uint16_t squareSize) {

return true; }

Magic.txt :

5 11 24 07 20 03 04 12 25 08 16 17 05 13 21 09 10 18 01 14 22 23 06 19 02 15 4 12 16 18 33 35 34 98 04 08 10 11 23 76 45 32 32 3 4 9 2 3 5 7 8 1 6 3 8 1 6 3 5 7 4 9 2

Please provide output screenshots if possible.

36925 01021 20210 75319 02-01 42586 21010 14703

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!