Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this program C++ you will input matrices and determine whether or not they are Magic Squares. A Magic Square is an n x n

In this program C++ you will input matrices and determine whether or not they are Magic Squares. A Magic Square is an n x n matrix (n is an odd integer >= 3) in which each of the integers 1, 2, 3 ..., n2 appears exactly once and all column sums, row sums and diagonal sums are equal. For example, the following is a 5 x 5 magic square in which all the rows, columns, and diagonals add up to 65. 17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9 Magic Square Program Requirements 1. Use Prog2Input.txt to test your program. 2. Process an unknown number of matrices. The input file consists of a number n, indicating the size of the matrix, followed by n rows, one per line. Your program should use functions with appropriate parameters and return values to perform the following steps for each matrix: a. Void readSquare (n, square) - Read and print n; Read the n rows into a 2-dimensional array, square b. Void printSquare (n, square) - Print the 2-dimensional array, square; formatted and easy to read c. bool checkMagic (n, square) - Checks to see if the 2-dimensional array, square is a magic square by first finding the correct sum for each of the rows, columns and diagonals using the formula total = ((1 + n2) / 2 ) * n; then looping through each row, column and diagonal calling the following functions that you must write each function will return the sum of the particular row, col, or diagonal i. int sumRow (row, n, square) sums the values in a particular row ii. int sumColumn (col, n, square) sums the values in a particular column iii. int sumDiagonal1 (n, square) sums the values in the left diagonal iv. int sumdiagonal2 (n, square) sums the values in the right diagonal function returns true or false indicating whether or not the matrix is a magic square. 3. checkMagic should terminate as soon as you detect that the matrix is NOT a magic square. For example, if you find that row 2 does not add up to the correct total, do not look at the remaining the rows, columns or diagonals. 4. Main program uses a while loop to read matrices until the end of file of reached, calling functions to process the magic squares. HINT: while (inputFile >> n) { read_square (n, square); print_square (n, square); if (check_magic (n, square)) cout << "MAGIC SQUARE "; else cout << "** NOT A MAGIC SQUARE ** "; } Item Range Low End (Did not do or did very little effort) Range High End (Used correctly and spent time/effort on programming) Names of the variables are meaningful and the program comments self-document the program. 0 2 Read from Prog2Input.txt 0 2 Correctly coded the main program 0 3 Correctly coded the function statements, appropriately passing parameters, suing local variables and not accessing global variables 0 6 Formatted the output so it is easy to read and aligned 0 2 The program executes without error and the output is correct 0 4 The zipped program folder and screen shots of the code and console are uploaded to the drop box. 0 1

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL Database Programming

Authors: Chris Fehily

1st Edition

1937842312, 978-1937842314

More Books

Students also viewed these Databases questions

Question

1. Who is responsible for resolving this dilemma?

Answered: 1 week ago

Question

How many Tables Will Base HCMSs typically have? Why?

Answered: 1 week ago

Question

What is the process of normalization?

Answered: 1 week ago