Assignment Description: Magic Squares: An n x n array, that is filled with integ
ID: 3766798 • Letter: A
Question
Assignment Description:
Magic Squares: An n x n array, that is filled with integers 1, 2, 3, …,n2 is a magic square if the sum of the elements in each row, in each column, and in the two diagonals is the same value (see the magic square below). 2 7 6 9 5 1 4 3 8
Write a program that generates a magic square for an array of size 3 * 3.
Please follow the given steps:
1. Declare a 2D array of 3 * 3.
2. Fill the array with unique values (1-9) generated randomly:
a. Generate a random number in the range of 1-9
b. Check if the random number generated has been previously generated in the array (check if is unique or not)
c. If the number is unique, store it in your 2D array.
3. Display the 2D array
4. Check if the 2D array is a magic square:
i.e: sum of each row = sum of each col = sum of each diagonal
5. Keep repeating (looping) steps 1 to 4 till magic square found.
Sample Output:
1 2 3
4 5 6
7 8 9 Not a Magic Square !
2 3 1
4 5 6
7 8 9 Not a Magic Square !
2 7 6
9 5 1
4 3 8
Magic Square !
Please make sure your code has following functions:
1. checkUnique(): Function to check if the random number generated is a unique number, i.e if the number is not previously generated number in your 2D array.
2. magicSquare(): Function to check if the 2D array is a magic square. Check if sum of each row, sum of each column a sum each diagonal are equal.
Explanation / Answer
Here is the code for you. If you need any refinement, just get back to me.
#include <iostream>
#include <ctime>
using namespace std;
void getData(int Array[3][3]) //Reads the data into the array, while validating.
{
int x;
bool check[10];
for(int i = 0; i < 10; i++)
{
check[i] = false;
}
for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++)
{
x = rand()%9 +1;
if(check[x-1])
{
j--;
}
else
{
Array[i][j] = x;
check[x-1] = true;
}
}
}
bool checkMatrix(int Array[][3]) //Checks whether the square forms a magic square or not.
{
int r1, r2, r3, c1, c2, c3, d1, d2;
r1 = Array[0][0] + Array[0][1] + Array[0][2];
r2 = Array[1][0] + Array[1][1] + Array[1][2];
r3 = Array[2][0] + Array[2][1] + Array[2][2];
c1 = Array[0][0] + Array[1][0] + Array[2][0];
c2 = Array[0][1] + Array[1][1] + Array[2][1];
c3 = Array[2][0] + Array[2][1] + Array[2][2];
d1 = Array[0][0] + Array[1][1] + Array[2][2];
d2 = Array[0][2] + Array[1][1] + Array[2][0];
if(r1 == 15 && r2 == 15 && r3 == 15 && c1 == 15 && c2 == 15 && c3 == 15 && d1 == 15 && d2 == 15)
return true;
else
return false;
}
void showSquare(int Array[][3]) //Displays the Square in a row column fashion.
{
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
cout<<Array[i][j]<<" ";
cout<<endl;
}
}
int main()
{
int Array[3][3];
char choice;
srand(time(NULL));
cout<<"The magic square is a square in which all the rows, columns and diagonals sum up to the same individually. ";
cout<<"Starting the game..."<<endl;
while(1) //Repeat infinitely.
{
getData(Array); //Calls a function to read the data.
showSquare(Array);//Shows the Square.
if(checkMatrix(Array)) //If it forms a Magic Square prompt accordingly.
cout<<"Magic Square !"<<endl;
else
cout<<"Not a Magic Square !"<<endl;
//cout<<"Do you want to play again (Y/N): "; //Ask to play again or not, and act accordingly.
//cin>>choice;
if(checkMatrix(Array))
exit(0);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.