Question: CS 2413 Data Structures Spring, 2021 Project Assignment #1 Magic Square Focus on: Object Oriented ProgrammingClass definition and class implementation A Magic Square is a

CS 2413 Data Structures

Spring, 2021

Project Assignment #1

Magic Square

Focus on: Object Oriented ProgrammingClass definition and class implementation

A Magic Square is a square array of integers such that the sum of every row, the sum of every column, and sum of each diagonal are equal.

Task: Define a class, namely MagicSquare, that generates a magic square by the following method. This method works only when the size of the square is an odd number. Starting by placing 1 in the middle of the top row, write down successive integers2, 3, along a diagonal going upwards and to the right. When you reach the top tow ( as you do immediately since 1 is in the top row.), continuing to the bottom row as though the bottom row were immediately above the top row. When you reach the rightmost column, continue to the leftmost column as though it were immediately to the right of the rightmost one. When you reach a position that is already occupied, instead drop straight down one position from the previous number to insert the new one.

The 3 *3 magic square is shown:

8

1

6

3

5

7

4

9

2

Magic constant = 15

The 5 * 5 magic square is shown:

17

24

1

8

15

23

5

7

14

16

4

6

13

20

22

10

12

19

21

3

11

18

25

2

9

Magic constant = 65

Your program reads a N * N square from the keyboard ( 3 <= N <=9), then tests to see if the input square is a magic square. If yes, your program prints a greeting message, otherwise, prints It is not a Magic Square, and then outputs a magic square.

Magic Constant (Magic Sum) = n * (n2 + 1 ) / 2 where n is the side length of the square

For example: Side length = 3, and then magic constant = 3 * (9 +1) /2 = 3* 5 = 15

Side length = 5,, and then magic constant = 5 * (25 +1)/2 = 5 * 13 = 65

Requirements:

  1. Your project may contain three files:
  1. Magic_Square.h This file contains data and methods:

Data:

square2-D array

number of columns store the number of columns

number of rows store the number of rows

counter store a number

Methods:

constructor(s)initialize data members

read_data read data from keyboard

print_square print a magic square

is_magic_square check to see a square is a magic square

generate_magic_square generate a magic square

  1. Magic_Square.cpp

This file contains your implementation code for your magic square class. Note: you need to include your Magic_Square.h

  1. Main.cpp

You use this file to test your class. Note: you need to include your Magic_Square.cpp

  1. Comment your code properly (10%)

  1. Header before your code. (5%) The header include:

Your name:

Class:

Assignment #:

Due date:

Instructor:

Program Description:

  1. Program output (5%)

  1. Turn in your program on time. You may suffer 10% deduction per day if you turned in a late project.

Your program output may look like:

Magic Square Program

Enter size of the square: 3

Enter data row by row:

8 1 6

3 5 7

4 9 2

It is a magic square!

Magic Square Program

Enter the size of the square: 3

Enter data row by row

8 1 6

3 5 7

4 2 9

It is not a Magic Square. The Magic Square is shown:

8 1 6

3 5 7

4 9 2

Your main function might look like:

int main()

{

MagicSquare mymagic;

char answer;

do{

mymagic.instruction();

mymagic.read_data( );

mymagic.is_magicsquare();

cout<

cin >> answer;

}while ( tolower(answer) == y );

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!