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

COMSC 140->OBJECT ORIENTED C++ INSTRUCTOR: JACKIE KUEHN MAGIC SQUARE (60 POINTS)

ID: 3728048 • Letter: C

Question

COMSC 140->OBJECT ORIENTED C++ INSTRUCTOR: JACKIE KUEHN MAGIC SQUARE (60 POINTS) CONCEPT: VECTORS (HW5) DUE: A magic square is an n-by-n vector where the integers 1 to n' appear exactly once and the sum of the integers in every row, column, and on both diagonals is the same. For example, the following magic square results when n-7. Notice that each row column and both diagonals total 175 MAGIC SQUARE WITH n-7 30 39 48 10 28 38 18 27 29 46 17 26 35 37 25 34 36 15 24 42 4 23 32 43 12 - 31 40 49 20 Implement the class with two member functions: a constructor and a display method. You should use an STL vector. The following code in main should generate output like the above: square,magic(7); magic.display)

Explanation / Answer

#include <stdio.h>
#include <string.h>
#include <bits/stdc++.h>
using namespace std;

class magicSquare
{
   private:
       vector<vector<int> > square;
   public:
       magicSquare(int n)
       {
           vector<vector<int> > square2;
           vector<int> v(n);
           for(int i=0;i<n;i++)
           {
               square.push_back(v);
               square2.push_back(v);
           }
           for(int i=0;i<n;i++)
           {
               for(int j=0;j<n;j++)
                   square2[i][j] = 0 ;
           }
           int i = n/2;
            int j = n-1;
            // One by one put all values in magic square
            for (int num=1; num <= n*n; )
            {
                if (i==-1 && j==n) //3rd condition
                {
                    j = n-2;
                    i = 0;
                }
                else
                {
                    // 1st condition helper if next number
                    // goes to out of square's right side
                    if (j == n)
                        j = 0;
      
                    // 1st condition helper if next number
                    // is goes to out of square's upper side
                    if (i < 0)
                        i=n-1;
                }
                if (square2[i][j]) //2nd condition
                {
                    j -= 2;
                    i++;
                    continue;
                }
                else
                    square2[i][j] = num++; //set number
      
                j++; i--; //1st condition
            }
            for(int i=0;i<n;i++)
            {
               for(int j=0;j<n;j++)
               {
                   square[n-1-j][i] = square2[i][j];
               }
            }

       }

       void display()
       {
           int row = square.size();
           int col = square[0].size();
           int i,j;
           for(i=0;i<row;i++)
           {
               for(j=0;j<col;j++)
                   cout<<square[i][j]<<" ";
               cout<<endl;
           }
       }

} ;

int main()
{
    int n = 7; // Works only when n is odd
    magicSquare obj(7);
    obj.display();
    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