Ask This Question To Be Solved By Our ExpertsGet A+ Grade Solution Guaranteed

expert
Timotius AlbertMathematics
(/5)

963 Answers

Hire Me
expert
Darshan MaheshwariEducation
(/5)

1000 Answers

Hire Me
expert
Laura LeighlandMathematics
(/5)

895 Answers

Hire Me
expert
Adetayo OjeniranStatistics
(/5)

711 Answers

Hire Me
C++ Programming

Programming with CPP Using Classes and Objects Arrays of Objects

INSTRUCTIONS TO CANDIDATES
ANSWER ALL QUESTIONS

CSE 240 Homework 2 – Programming with C++ Due: Monday, April 1, 11:59 PM 

  1. What This Assignment Is About: 
  • Classes and Objects 
  • Methods 
  • Arrays of Primitive Values 
  • Arrays of Objects 
  • Recursion 
  • for and if Statements 
  • Insertion Sort 
  1. Use the following Guidelines 
  • Give identifiers semantic meaning and make them easy to read (examples numStudents, grossPay, etc). 
  • User upper case for constants. Use title case (first letter is upper case) for classes. Use lower case with uppercase word separators for all other identifiers (variables, methods, objects) 
  • Use tabs or spaces to indent code within blocks (code surrounded by braces). This includes classes, methods, and code associated with ifs, switches and loops. Be consistent with the number of spaces or tabs that you use to indent. 
  • Use white space to make your program more readable. 

For each file in your assignment, provide a heading (in comments) which includes: 

  • The assignment number. 
  • Its author (your name). 
  • A description of what this program is doing. 
  1. Part 1. Primitive Types, Searching, Recursion (35 points). 
  2. a) In the file cpp (available on Canvas), add header comments and 

function comments 

  1. b) Implement the function initializeArray that receives two parameters: an array of integers 

and the array size. Use a for loop and an if statement to put 0s in the odd positions of the array and 5s in the even positions. You must use pointers to work with the array. Hint: review pointers as parameters. 

  1. c) Implement the function printArray that receives as parameters an array of integers and 

the array size. Use a for statements to print all the elements in the array. You must use pointers to work with the array. Hint: review pointers as parameters. 

  1. d) Implement the function arrayInsertionSort that receives as parameters an array of integers and the array size, and order the array’s elements in descending order. Implement Insertion Sort algorithm. It should be Insertion Sort, not Selection Sort, not Quick Sort, etc. 
  2. e) Implement the recursive function that calculates and returns the factorial of a number. 

The function receives the number (integer number) as parameter 

  1. f) Compile and run the code for cpp 

Any changes made to the main method of the file will be penalized unless otherwise instructed 

Grading Criteria for the part 1 

01 pts: file contains header information 01 pts: adequate comment to explain every function 01 pts: consistent indentation and spacing 08 pts: arrayInsertionSort 08 pts: printArray 08 pts: initializeArray 08 pts: factorial 

  1. Part 2 Structs and Arrays (65 points). 

In this assignment, we will be making a program to populate a game board with pieces and to move those pieces around on the board. 

Use the files homework_part_2.cpp, gamepiece.h, gamepiece.cpp, gameboard.h, and gameboard.cpp (available on Canvas). You will need to add header comments to each file along with comments for all the methods you will be implementing. 

Any changes made to the main method of the file will be penalized unless otherwise instructed. 

Step 1. 

For the GamePiece class you will use the files gamepiece.h and gamepiece.cpp. The gamepiece.cpp file should implement the methods for the GamePiece class declared in gamepiece.h. You will need to add a private member variable, char label[30], to the class declaration in gamepiece.h. Implement the methods in gamepiece.cpp as follows: 

Function Description void GamePiece () Assign the default string "---" to the game piece’s label. 

void GamePiece(char* newLabel) Assign the game piece’s label with the newLabel 

provided. 

char* getLabel() Returns the piece’s label. 

char* toString() Constructs a C string of length 3 from the piece’s label 

(Note: this length does not include the null character). If the label is shorter than length 3, then the new string should be the label with spaces appended to make it the correct length. If the label is longer than 3, then use the first 3 characters of the label. 

Step 2. 

For the GameBoard class you will use the files gameboard.h and gameboard.cpp. The gameboard.cpp file should implement the methods for the GameBoard class declared in gameboard.h. The class will contain a 2-dimensional array called “board” of GamePiece type and 2 ints, rows and columns. These member variables should be added to the class declaration in gameboard.h as private members. Implement the methods in gameboard.cpp as follows: 

Function Description void GameBoard(int rows, int cols) Instantiates the 2-dimensional array “board” to the size "rows" by "columns" specified by the parameters, then sets the GameBoard’s rows and columns values. Each 

GamePiece in the board should be instantiated with the default label using the default constructor. 

bool isSpaceValid(int row, int col) The function checks if the parameters row and col are 

valid. If at least one of the parameters "row" or "col" is less than 0 or larger than the last index of the array (note that the number of rows and columns can be different), then it return false. Otherwise it returns true. 

bool addPiece(GamePiece piece, int row, int col) 

This function should validate that the space specified by row and col is valid and that the space is not occupied by a piece. If the GamePiece at the space has the default label, the space should be considered not occupied. If the space is both valid and not already occupied, then the space should be replaced by the parameter “piece” and the method should return true. Otherwise, return false. 

int movePiece(int srcRow, int srcCol, int destRow, int destCol) 

This method should validate that both the src and dest spaces are valid and that the dest space is not occupied. If both conditions pass, then the piece located at (srcRow, srcCol) should be moved to (destRow, destCol). The space at (srcRow, srcCol) should be replaced by the default GamePiece. If this method moves the piece, return true, otherwise return false. void print() It prints information of the "board". It should show the list 

of pieces placed on the board using the GamePiece::toString method (it shows the first 3 characters of each piece). Use the following format: 

The GameBoard -------------------- --- Kin --- Paw --- --- --- --- Paw 

Please see the sample output listed below. 

After compiling the homework_part_2.cpp, gameboard.cpp, and gamepiece.cpp files, you need to execute it. 

Sample Output: (the inputs entered by a user are shown in red) 

Please enter the number of rows. 3 Please enter the number of columns. 3 Please enter a label for a new piece. Enter "Q" when done. King Please enter a row for the piece. 0 Please enter a column for the piece. 1 New piece "King" added. Please enter a label for a new piece. Enter "Q" when done. Pawn Please enter a row for the piece. 1 Please enter a column for the piece. 0 New piece "Pawn" added. Please enter a label for a new piece. Enter "Q" when done. Pawn Please enter a row for the piece. 2 Please enter a column for the piece. 2 New piece "Pawn" added. Please enter a label for a new piece. Enter "Q" when done. Queen Please enter a row for the piece. 4 Please enter a column for the piece. 5 Invalid row and/or column. Please enter a label for a new piece. Enter "Q" when done. King Please enter a row for the piece. -1 Please enter a column for the piece. 2 Invalid row and/or column. Please enter a label for a new piece. Enter "Q" when done. Q The GameBoard -------------------- --- Kin --- Paw --- --- 

--- --- Paw Would you like to move a piece? Enter "Y" to move a piece. Y Please enter the piece's row. 0 Please enter the piece's column. 1 Please enter the piece's new row. 0 Please enter the piece's new column. 2 Piece moved to new space. The GameBoard -------------------- --- --- Kin Paw --- --- --- --- Paw Would you like to move a piece? Enter "Y" to move a piece. 

Grading Criteria for the part 2 

05 pts: Every file contains header information 05 pts: adequate comment to explain every function 05 pts: consistent indentation and spacing 05 pts: it compiles 05 pts: The GamePiece default constructor is correct 05 pts: The GamePiece(char*) constructor is correct 05 pts: The method GamePiece::getLabel is correct 05 pts: The method GamePiece::toString is correct 05 pts: The GameBoard constructor is correct 05 pts: The method GameBoard::isSpaceValid is correct 05 pts: The method GameBoard::addPiece is correct 05 pts: The method GameBoard::movePiece is correct 05 pts: The method GameBoard::print is correct 

Related Questions

. The fundamental operations of create, read, update, and delete (CRUD) in either Python or Java

CS 340 Milestone One Guidelines and Rubric  Overview: For this assignment, you will implement the fundamental operations of create, read, update,

. Develop a program to emulate a purchase transaction at a retail store. This  program will have two classes, a LineItem class and a Transaction class

Retail Transaction Programming Project  Project Requirements:  Develop a program to emulate a purchase transaction at a retail store. This

. The following program contains five errors. Identify the errors and fix them

7COM1028   Secure Systems Programming   Referral Coursework: Secure

. Accepts the following from a user: Item Name Item Quantity Item Price Allows the user to create a file to store the sales receipt contents

Create a GUI program that:Accepts the following from a user:Item NameItem QuantityItem PriceAllows the user to create a file to store the sales receip

. The final project will encompass developing a web service using a software stack and implementing an industry-standard interface. Regardless of whether you choose to pursue application development goals as a pure developer or as a software engineer

CS 340 Final Project Guidelines and Rubric  Overview The final project will encompass developing a web service using a software stack and impleme

stay-icon
stay-icon

Stay Updated with latest info & offers