CS 202 – Assignment #4 Purpose: Learn class inheritance, dynamic allocation, pointers, and make utility. Points: 100 Assignment: In recreational mathematics, a Magic Square1 is an arrangement of...

1 answer below »

Design and implement two C++ classes to provide a normal Magic Square creation program. The rest of the details are in the Assign 4 pdf file.




CS 202 – Assignment #4 Purpose: Learn class inheritance, dynamic allocation, pointers, and make utility. Points: 100 Assignment: In recreational mathematics, a Magic Square1 is an arrangement of numbers (usually integers) in a square grid, where the numbers in each row, and in each column, and the numbers in the forward and backward main diagonals, all add up to the same number. A magic square has the same number of rows as it has columns, typically referred to as the order. A magic square that contains the integers from 1 to n2 is called a normal magic square. Design and implement two C++ classes to provide a normal Magic Square creation program. The classes we will implement are as follows: ● Board Class ◦ Implement a generic board class that can be used for any type of square application. Such applications include chess, checkers, bingo, and Sudoku. This class may be used in future assignments. The class will dynamically create a square two dimensional array based on the passed order. Functions will get and set various board locations and include error checking of the indexes based on the current board order. ● Magic Square Class ◦ Implement a class that uses the board class and creates normal magic squares based on the specified order. A magic square is-a board (square) with special rules and added functionality. The algorithms for creating normal magic squares are provided. Understanding the algorithm is more challenging that coding. For all implementation code, points will be deducted for poor or especially inefficient solutions. 1 For more information, refer to: http://en.wikipedia.org/wiki/Magic_square Development and Testing In order to simplify development and testing, the project is split into two (2) main parts; board class and magic square class. • The board class must be developed and fully operational before attempting the magic square class. The board class can be developed and tested independently of the other class. An independent main, brdMain, that uses and tests only the board class is provided and it can be built independently using the provided main file (see next section). • The magic square class uses the board class and will implement the normal magic square algorithms based on the order (odd, singly even, or doubly even). It is suggested to to develop and test one algorithm at a time. For example, develop and test the even algorithm before attempting the other algorithms. The example output can help in checking the intermediate results. ◦ The provided main, magicSqr, will perform a series of tests on the magic square object. If some command line arguments are provided, it just create a normal magic square of the specified order. For example; ./magicSqr -o 5 will create and display a magic square of order 5. If no command line arguments are provided, it will execute a series of predefined tests (including invalid values). Make File: The provided make file assumes the source file names include (brdMain.cpp, boardType.h, boardTypeImp.cpp, magicSqr.cpp, magicSquareType.h, magicSquareImp.cpp). To build the provided main for board testing; make brdMain And to build the provided main for the magic square class; make Which will create the brdMain (for boardType Class) or the magicSqr executable. Submission: • It is strongly suggested that you submit each part of the program for testing as they are developed. For example, when the board class is completed, it can be submitted which will allow testing of that portion of the project. This will ensure it is fully working before moving on to the next part. ◦ If you find any issues, they can be corrected and resubmitted for testing and unlimited number of times (before the due date/time). • All files must compile and execute on Ubuntu and compiler with C++11. • Submit source files ◦ Note, do not submit the provided mains (we have them). • Once you submit, the system will score the project and provide feedback. ◦ If you do not get full score, you can (and should) correct and resubmit. ◦ You can re-submit an unlimited number of times before the due date/time. • Late submissions will be accepted for a period of 24 hours after the due date/time for any given lab. Late submissions will be subject to a ~2% reduction in points per an hour late. If you submit 1 minute - 1 hour late -2%, 1-2 hours late -4%, … , 23-24 hours late -50%. This means after 24 hours late submissions will receive an automatic 0. Program Header Block All program and header files must include your name, section number, assignment, NSHE number, input and output summary. The required format is as follows: /* Name: MY_NAME, NSHE, CLASS-SECTION, ASSIGNMENT Description: Input: Output: */ Failure to include your name in this format will result in a loss of up to 5%. Code Quality Checks A C++ linter2 is used to perform some basic checks on code quality. These checks include, but are not limited to, the following: • Unnecessary 'else' statements should not be used. • Identifier naming style should be either camelCase or snake_case (consistently chosen). • Named constants should be used in place of literals. • Correct indentation should be used. • Redundant return/continue statements should not be used. • Selection conditions should be in the simplest form possible. • Function prototypes should be free of top level const. Not all of these items will apply to every program. Failure to to address these guidelines will result in a loss of up to 5%. Scoring Rubric Scoring will include functionality, code quality, and documentation. Below is a summary of the scoring rubric for this assignment. Criteria Weight Summary Compilation - Failure to compile will result in a score of 0. Program Header 5% Must include header block in the required format (see above). General Comments 10% Must include an appropriate level of program documentation. Line Length 5% No lines should exceed more than eighty (80) characters. Code Quality 5% Must meet some basic code quality checks (see above) Program Functionality Program must meet the functional requirements as outlined in the assignment. Must be submitted on time for full score. • Board Type 30% • Magic Square 45% 2 For more information, refer to: https://en.wikipedia.org/wiki/Lint_(software) Board Class Create a board class, boardType, to provide basic square board functionality. boardType #size: int #**board: int -BRD_SIZE_MIN=3: static constexpr int -BRD_SIZE_MAX=30: static constexpr int +boardType(int) +~boardType() +setCell(int, int, int): void +getCell(int, int) const: int +getOrder() const: int +printGrid() const: void Your implementation and header files should be fully commented. This class may be used on a future assignment. Function Descriptions The following are more detailed descriptions of the required functions. • The constructor boardType(int) should verify the passed size (between SIZE_MIN and SIZE_MAX, inclusive). If the size is valid, an integer two-dimensional array, size x size, should be dynamically created and all values set to 0. If the size is invalid, a message should be displayed (e.g., "Error, invalid board size." and "No board created.”) and the board class variable set to NULL and the bSize set to 0. Refer to the example executions for output formatting. • The destructor ~boardType() should delete the dynamically allocated memory (if any). The destructor is called automatically when the object goes out of scope. • The getCell(int, int) function should return the cell contents at the passed row and column location (in that order). The passed row and column must be validated to ensure invalid locations in the array are not accessed. If an invalid location is passed, an error message should be displayed (e.g., "Error, invalid board location.") and a 0 returned. • The setCell(int, int, int) function should set the cell contents at the passed row and column location. The arguments are row, column, and value (in that order). The passed row and column must be validated to ensure invalid locations in the array are not accessed. If an invalid location is passed, an error message should be displayed (e.g., "Error, invalid board location.") and the current value left as is. • The getOrder() function should return the board size. • The printGrid() function should print the grid in a formatted manner (with each value in a small text box). An empty grid should not print anything. Refer to the example executions for output formatting. Use the provided main, bMain.cpp, to demonstrate that the boardType class functions correctly. The main and provided makefile reference files bMain,.cpp, boardTypeImp.cpp and boardType.h. Your implementation and header files should be fully commented. Magic Square Class A magic square will require an nxn board so the magicSquareType class will inherit the basic grid functionality from the boardType class. Create a magicSquareType class to provide magic square creation functionality. magicSquareType -title: string +magicSquareType(int, string) +createMagicSquare(): void +printMagicSquare() const: void +readMagicSquare(): void +validateMagicSquare() const: bool +clearMagicSquare(): void +magicNumber() const: int +getTitle() const: string +setTitle(string): void Function Descriptions The following are more detailed descriptions of the required functions. • The magicSquareType(int) constructor should set the title and use the base class constructor to allow the base class to create the grid. • The magicNumber() function should return the magic number or sum based on the board size or order. The magic number or sum for a normal magic square is computed as follows: magicNumber = n ( n 2+1 2 ) • The validateMagicSquare() function should validate a magic square by ensuring the sum of each column, each row, and each of the two main diagonals sum to the magic number. • The getTitle() function should return the current title value. • The setTitle(string) function should set the title to the passed value. • The clearMagicSqaure() function should reset each cell entry to 0 and clear the title string. • The readMagicSquare() function should prompt the user for a title (ensuring it is not empty). The title may be multiple words. Then read integers, one row at a time (based on the board size for the current object). • The printMagicSquare() function should display some header information ("CS 202 – Magic Squares", the title, order, and magic number. The headers should be displayed in
Answered 1 days AfterSep 19, 2021

Answer To: CS 202 – Assignment #4 Purpose: Learn class inheritance, dynamic allocation, pointers, and make...

Vaibhav answered on Sep 21 2021
133 Votes
boardType.h
/*
Name: MY_NAME, NSHE, CLASS-SECTION, ASSIGNMENT
Description:
Input:
Output: t>
*/
class boardType {
protected:
int size;
int **board;
private:
static constexpr int BRD_SIZE_MIN = 3;
static constexpr int BRD_SIZE_MAX = 30;
public:
/**
* @brief Construct a new board Type object.
* Initializes the size parameter for the board and creates a board with the
* specified size.
*
* @param size
*/
boardType(int size);
/**
* @brief Destroy the board Type object.
* When the board is no longer needed, free up the memmory assosiated with the
* current board.
*
*/
~boardType();
/**
* @brief Set the Cell object. Used to initialize the cell with the specified
* row and column to a specific value.
*
* @param row
* @param col
* @param val
*/
void setCell(int row, int col, int val);
/**
* @brief Get the Cell object. Returns the value stored at the particular cell
* of the board.
*
* @param row
* @param col
* @return int
*/
int getCell(int row, int col) const;
/**
* @brief Get the Order object. Returns the size of the current board.
*
* @return int
*/
int getOrder() const;
/**
* @brief Used to print the values stored on the board in a well formatted
* format.
*
*/
void printGrid()...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here