Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Creating Magic Squares in C++: An n x n array, that is filled with integers 1, 2

ID: 3766730 • Letter: C

Question

Creating Magic Squares in C++: 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 it 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.

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. You can use additional functions (optional) for other operations. Make sure your program runs till the magic square is found.

*I'm mainly looking for help with the checkUnique() and magicSquare() functions, but an example of a complete working program would be extremely helpful.

Explanation / Answer


/*C++ program that generates a random array of size 3x3 with
1 -9 unique values and finds the magic square and prints
the magic square*/
#include<iostream>
#include<iomanip>
using namespace std;

//function prototypes
bool checkUnique(int mat[][3],int n,int r);
bool magicSquare(int mat[][3],int n);

int main()
{

   //Set a constant of size ,n=3
   const int n=3;

   //Seed to generate a random number
   srand(time(0));

   //Create an array of size nxn
   int MagicSquare[n][n]={{0,0,0},
   {0,0,0},
   {0,0,0}};


   int i=0;
   int j=0;

   bool isUnique=false;
   bool repeat=true;

   while(repeat)
   {

       //Generate a matrix with 9 random numbers in a range of 1 - 9
       //without repeation
   for(int x=0; x<n; x++)
   {
       for(int y=0; y<n; y++)
       {
           int r= rand()%9+1;
           while(!checkUnique(MagicSquare,n,r))
           {
               r= rand()%9+1;
           }
           //Set unique vlaue to magic square array
           MagicSquare[x][y]=r;
       }

      
   }

   //Call magicSquare that takes arr, MagicSquare with array, MagicSquare of size , 3
   if(magicSquare(MagicSquare,3))
       //Set repeat to false if magicSquare is found
       repeat=false;  
   }

   //Print the magic square
   printf("Magic Square ");
   for(int x=0; x<n; x++)
   {
       for(int y=0; y<n; y++)
           cout << MagicSquare[x][y]<<" ";
       cout << endl;
   }

   system("pause");
   return 0;

}


//Returns true if magicSquare that takes array , mat of size,n=3
bool magicSquare(int mat[][3],int n)
{

   //set flag to zero
   bool flag=0;

   //Sum of diagonals
   int sum = 0;
   for (int row = 0; row < n; row++)
   {
      for (int column = 0; column < n; column++)
      {
         if (row == column)
            sum = sum + mat[row][column];
      }
   }

   //Sum of rows to check row sums
   for (int row = 0; row < n; row++)
   {
      int sum1 = 0;
      for (int column = 0; column < n; column++)
      {
         sum1 = sum1 + mat[row][column];
      }
    
      if (sum == sum1)
         flag = 1;
      else {
         flag = 0;
         break;
      }
   }

   ////Sum of Colums to check column sums
   for (int row = 0; row < n; row++) {
      int sum2 = 0;
      for (int column = 0; column < n; column++) {
         sum2 = sum2 + mat[column][row];
      }
      if (sum == sum2)
         flag = 1;
      else {
         flag = 0;
         break;
      }
   }

   //Set flag to 1 for magic square
   if (flag == 1)
       return true;
   else
       return false;

}

//Method checkUnique that takes mat array and size, n and random value,r
bool checkUnique(int mat[][3],int n,int r)
{
   bool found=true;
   for(int x=0; x<n; x++)
   {
       for(int y=0; y<n; y++)
       {
           //Check if mat element is equal to value,r
           if(mat[x][y]==r)
               //set found to false
               found=false;
       }
   }

   return found;
}

-------------------------------------------------------------------------------

Note: This program might took takes long time(in minutes) to find magic square with random values

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote