Magic Square (in C++) A magic square is a square matrix whose rows, columns, and
ID: 3827551 • Letter: M
Question
Magic Square (in C++)
A magic square is a square matrix whose rows, columns, and main diagonals all sum to the same number. A 3×3 magic square follows, (we will only use a 3x3 matrix but the magic squares can be run on any size of square.)
618
753
294
Write a boolean-valued function IsMagic that returns true if it’s square matrix parameter is magic and false otherwise. Call the functions RowSum and ColSum. Should use a 2D array for the matrix, and once entered by the user, check to see that the input is valid, ie. Is not already in the array. Should fill the array with input from the user. Please enter the numbers 1-9 for the order of row 1, then row 2, etc. of the matrix.
1 2 3 4 5 6 7 8 9
8 3 6
1 7 9
2 5 4
Sorry, the square you entered is NOT magic.
Try again? Y or N
You should write to the output file, magicSquare.txt, the square values if the square is in fact magic. If not, just display the output to the console to say it is not.
Call this lab magic.cpp
Turn in magic.cpp file and the corresponding magicSquare.txt file.
Explanation / Answer
Below is the given program for magic square in c++ with the requested features. Hope it helps!! #include #include using namespace std; const int ROW = 7; const int COL = 9; bool openFile( int[][COL] ); void magicSquare( int[][COL], bool[] ); void output( int[][COL], bool[] ); int main() { int nums[ROW][COL]; bool check[ROW]; bool flag = true; flag = openFile( nums ); if ( flag ) { magicSquare(nums, check); output(nums, check); } else coutRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.