Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Author: X
- Date: 04/27/2015
- Purpose: Creates a 3x3 magic square. It's 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).
- Algorithm:
- 1. 2D array of 3 * 3.
- 2. Array is filled with unique values (1-9) that are generated randomly.
- 3. Checks if the random number generated has been previously generated in the arrack.
- 4. If the number is unique, it is stored in the array.
- 5. The array is displayed.
- 6. Check if the array is a magic square.
- 7. Keep looping steps until magic square is found.
- */
- //Includes
- #include <iostream>
- #include <ctime>
- #include <cstdlib>
- //Functions
- int checkUnique(); // Function to check if the random number generated is a unique number, i.e if the number is not previously generated in the 2D array.
- int magicSquare(); // Function to check if the 2D array is a magic square. Checks if sum of each row and sum of each column and diagonal are equal.
- using namespace std;
- bool checkUnique(int s[][3], int num){
- for (int i = 0; i < 3; i++){
- for(int j = 0; j < 3; j++){
- if (s[i][j] == 0)
- return true;
- else if (s[i][j] == num)
- return false;
- }
- }
- return true;
- }
- void initializeArr(int s[][3], int size = 3){
- // Code to generate a randum number.
- bool uniq = false;
- int num;
- // Iterate through the 2D array
- for (int i = 0; i < 3; i++){
- for (int j = 0; j < 3; j++){
- // Generates a number until it is unique.
- while (uniq == false){
- num = rand() % 9 + 1;
- uniq = checkUnique(s, num);
- }
- s[i][j] = num;
- uniq = false;
- }
- }
- }
- void displayArr(int s[][3], int size = 3){
- // Iterate through the 2D array and display the element
- for (int i = 0; i < 3; i++){
- for (int j = 0; j < 3; j++){
- cout << s[i][j] << "\t";
- }
- cout << endl;
- }
- }
- bool magicSquare(int s[][3], int size = 3){
- // Value to store diagonals
- int d1=0, d2=0;
- // Calulating the sum of each row and assigning it to subscript 3
- for (int i = 0; i < 3; i++){
- for (int j = 0; j < 3; j++){
- s[i][3] += s[i][j];
- }
- }
- // Calculating the sum of each col and assigning to to subscript 3
- for (int j = 0; j < 3; j++){
- for (int i = 0; i < 3; i++){
- s[3][j] += s[i][j];
- }
- }
- // Calculating the sum of the diagonal
- for (int i = 0, j = 0; i < 3; i++, j++){
- d1 += s[i][j];
- }
- for (int i = 3, j = 0; j < 3; i--, j++){
- d2 += s[i][j];
- }
- // See if the sum of everything are equal
- if (s[0][2] == d1 && s[1][2] == d1 && s[2][0] == d1 && s[2][1] == d1 && d1 == d2)
- return true;
- else
- return false;
- }
- void zeroArr(int s[][3], int size = 3){
- for (int i = 0; i < 3; i++){
- for (int j = 0; j < 3; j++){
- s[i][j] = 0;
- }
- }
- }
- int main(){
- srand(time(NULL)); //called at the beginning of main so
- // Create the 2D 3x3 array
- int square [3][3] = {0};
- bool mSq = false;
- // Continue creating a new matrix until a magic square is found
- while (mSq == false){
- // Function call to fill in the array
- initializeArr(square);
- displayArr(square);
- // Function call to determine if it is a magic square
- mSq = magicSquare(square);
- if (mSq == true)
- {
- displayArr(square);
- cout << "Magic Square!" << endl;
- }
- else{
- cout << "Not a Magic Square!" << endl;
- // Zero out the array
- zeroArr(square);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement