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

Create a program that will fill in the numbers of a magic square. A magic square

ID: 3624884 • Letter: C

Question

Create a program that will fill in the numbers of a magic square. A magic square is a n x n grid in which the numbers 1, 2, 3, ….. n2 appear exactly one time, the sum of the each of the columns equals a specific number, the sum of each of the rows equals the same number, and the sum of each of the diagonals equals the same number. Below is an example for a 3 x 3 grid and a 5 x 5 grid.

4 3 8                     11 10 4    23 17
9 5 1                     18 12   6    5    24
2 7 6                     25 19   13 7    1

                           2   21   20 14 8

                             9   3     22 16 15

Note that the rows (4 + 3 + 8, 9 + 5 + 1, and 2 + 7 + 6), columns (4 + 9 + 2, 3 + 5 + 7, and 8 + 1 + 6), and diagonals (8 + 5 + 2, and 4 + 5 + 6) sum up to 15 for the 3 x 3 grid. The sum for the 5 x 5 grid is 65.
An n x n square, where n is any odd integer > 0, can be filled using the following rules:
• Let k be an integer that has the values from 1 to n2.
• Place one (1) in the last column, middle row.
• Next continue to fill the grid by placing k + 1 in the cell one column to the right and one row down from the cell where k was placed except when one of the following occurs:
1. if a move takes you below the bottom row in the jth column, then place k + 1 in the top row of the jth column;
2. if a move takes you outside to the right of the ith row, then place k + 1 in the 1st (left) column of the ith row;
3. or if a move takes you to a cell that is already occupied or outside the cell in the lower right corner, then k +1 should be placed immediately to the left of k.

Your program should be able to fill out any n x n grid where n is an ODD integer less than twenty and greater than 1 that the user enters. Your program should repeatedly prompt the user for a new number if the user enters and invalid number for n. Once the validity of n has been confirmed, you should call a function to fill the array employing the rules given above. The function to fill the magic square should accept a 2-dimensional array and the size to be used. Once the grid has been filled, your program should output the magic square by calling another function that accepts the 2-dimensional array and the size of the array. This function to output the array should employ nested loops.

Do not use “break,” “continue,” or “goto” in your loops

Hints!
1) Before trying to write the program, follow the rules stated to fill out the 5 x 5 grid shown above on your own, then fill out a 7 x 7 on your own.
2) Declare your array to the maximum size needed (remember that you do not have to use all the elements of the array).
3) Initialize all the elements of the array to 0 or a negative number so you can tell when a cell has a number in it already. Create another function to do this!
- You will have THREE functions, besides main(), based on this criteria.

Explanation / Answer

please rate - thanks

#include<iostream>
using namespace std;
int main()
{
             int a[20][20],r,c,br,bc,k,n;
      
             cout<<"Enter the Order of Magic Square(Odd): ";
             cin>>n;
             while(n%2==0||n<1||n>20)
                {cout<<"Enter the Order of Magic Square(Odd): ";
                 cin>>n;
                 }
             for(r=0;r<n;r++)
                          for(c=0;c<n;c++)
                                       a[r][c]=0;
             r=0;
             c=n/2;
             for(k=1;k<=n*n;k++)
             {
                          a[r][c]=k;
                          br=r++;
                          bc=c++;
                          r=(r+1)%n;
                          c=(c+1)%n;
                          if(a[r][c]!=0)
                             {
                                       c=bc;
                                       r=br-1;
                                       if(r<0)r=n-1;
                             }
             }
             cout<<"The Magic Square... ";
             for(r=0;r<n;r++)
             {
                          for(c=0;c<n;c++)
                          cout<<" "<<a[r][c];
                          cout<<" ";
             }
system("pause");
return 0;           
}

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