Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

IN C++ Please. CONCEPT: VECTORS A magic square is an n-by-n vector where the integers 1 to n? appear exactly once and the sum of

IN C++ Please.

image text in transcribedimage text in transcribedimage text in transcribed

CONCEPT: VECTORS 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 Implement the class with two member functions: a constructor and a display method. You should use an STL vector. Given an input of 7, when we run out main our output should be: 30 10 19 39 47 48 7 27 38 46 18 26 28 29 37 6 35 8 16 14 1 9 17 25 33 41 49 34 36 5 13 45 42 4 15 23 31 24 32 44 3 21 22 43 2 12 20 40 11 (Hint: Use setw(3) in your cout for each number to get the right output) You should be able to construct an n-by-n magic square for any odd value n from 3 to 15. When j=1, place the value of jin the middle of the first row. Then, for a counter value ranging from 1 to n2, move up one row and to the right one column and store the counter value, unless one of the following events occurs: When the next row becomes 0, make the next row equal to n (this assumes you used 1 for the first row). When the next column becomes n+1, make the next column equal to 1 (this assumes that you used 1 for the first column). If a position is already filled, or the upper-right corner element has just obtained a value, place the next counter value in the position that is one row below the position where the last counter value has been placed. wewe LAB ACTIVITY 5.28.1: Magic Square 0/60 Current file: main.cpp Load default template. 1 #include "MagicSquare.h" 2 3 int main 4 5 // your program goes here 6 7 return 0; Current file: magicsquare.h Load default template... 1 #ifndef MAGICSQUARE_H 2 #define MAGICSQUARE_H 3 4 #include 5 #include 6 #include 7 #include 8 9 #endir 10 11 using namespace std; 12 13 class MagicSquare 14 { 15 public: 16 /* constructors / 17 18 // initialized magic square takes a number 19 MagicSquare(int n); 20 21 // destructor 22 virtual MagicSquare () 23 24 /user functions/ 25 // print square to stdout 26 void display(); 27 28 private: 29 }; Current file: magicsquare.cpp Load default template... 1 #include "magic square.h" 2 /** * Constructor with input 5 * input: an integer size > 3, and not even! 6 * output: builds a magic square 7 */ 8 MagicSquare: MagicSquare(int n) 91 10 // magical!:D 11 ) 12 13 ** 14 * Prints out the square 15 * input: void 16 * output: void 17 * 18 void MagicSquare::display() { 19 // implement your solution here. 20 1/ note: use setw(3) before printing a number to make sure your solution matches the test solution! 21 } Magic Square in c++ needs a main.cpp needs a magicsquare.cpp needs a magicsquare.h

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Practical Azure SQL Database For Modern Developers Building Applications In The Microsoft Cloud

Authors: Davide Mauri, Silvano Coriani, Anna Hoffma, Sanjay Mishra, Jovan Popovic

1st Edition

1484263693, 978-1484263693

More Books

Students also viewed these Databases questions

Question

What are Measures in OLAP Cubes?

Answered: 1 week ago

Question

How do OLAP Databases provide for Drilling Down into data?

Answered: 1 week ago

Question

How are OLAP Cubes different from Production Relational Databases?

Answered: 1 week ago