EECS 183 Labs

EECS 183 Lab 8: Exam 2 Practice

Lab due on your scheduled lab day (March 18-22)

Lab accepted for full credit until Tuesday, March 26, 2024, 11:59 pm Eastern

In this lab, you are completing a practice exam with your peers in preparation for Exam 2. For all labs in EECS 183, to receive a grade, every student must individually submit the lab assignment.

Starter Files

You can download the starter files using this link.

After downloading and unzipping, you will find the following files in the starter-files folder:

IMPORTANT: For this lab, you must include all of the files in a single project in Xcode or Visual Studio.

Lab Assignment

Tasks to Complete

  1. Start a new project with your IDE using the starter files.
  2. For each of the three practice exam questions below:
    • Solve the practice exam question with your group.
    • Transfer your solution to the starter code and submit it to the autograder to check your solution. You will submit solutions for these three functions:
    • MonsterArmy::spawnMonster()
    • MonsterArmy::pointsByType()
    • monsters()
  3. The following files will be submitted to the autograder:
    • MonsterArmy.cpp
    • monsters.cpp

NOTE: The questions below appeared on a previous EECS 183 Exam 2.

Class Definitions

You will reference both this specification and the RMEs in the .h files to complete your functions.

MonsterArmy::spawnMonster()

Implement the MonsterArmy class spawnMonster() member function below, just as it would appear in the MonsterArmy.cpp file. Assume all headers necessary are already included for you. Only implement the spawnMonster() function here. RME and declaration are included here for reference.

/**
 * Requires: type is a non-empty string, points >= 0.
 * Modifies: currentSize, army
 * Effects:  if space is available in the army array, sets the data
 *           of the monster at position currentSize in the array
 *           using the monster type and hit points given.
 *           Returns true if a monster was modified and false
 *           otherwise. currentSize should correctly keep track
 *           of the number of monsters spawned.
 * Note: you may assume the array army has been
 *       initialized with currentSize number of monsters.
**/
bool spawnMonster(string type, int points);

MonsterArmy::pointsByType()

Implement the MonsterArmy class pointsByType() member function below, just as it would appear in the MonsterArmy.cpp file. Assume all headers necessary are already included for you. Only implement the pointsByType() function here. RME and declaration are included here for reference.

HINT: You must use the class member currentSize for the loop condition for this function, not MAX_ARMY_SIZE.

/**
 * Requires: type is a non-empty string
 * Modifies: nothing
 * Effects:  returns the sum of total hit points
 *           for the given Monster type
**/
int pointsByType(string type);

monsters()

Your last task is to implement a program for a user to build their monster army via keyboard input and compute the total hit points for the Demogorgon monster type. Assume that all necessary libraries are included. See below for sample runs of the program.

/**
 * Requires: size > 0
 * Modifies: army, ins, cout
 * Effects:  extracts type and points for each monster from the ins
 *           input stream. The number of monsters to enter is given by
 *           the size parameter.
 */
void buildArmy(MonsterArmy & army, int size, istream & ins) {
    cout << "Enter " << size << " monster types and hit points\n";
    cout << "Each on a new line\n";
    string type;
    int hitPoints = 0;
    int count = 0; 
    while (count < size && ins >> type >> hitPoints) {
        if (army.spawnMonster(type, hitPoints)) {
            count++;
        } else {
            return;
        }
    }
    return;
}

void monsters() {
    // TODO: replace the comments below with statements by filling in the blanks
    // See lab specification for sample runs of this function
	
    // __________ armySize = 5;
	
    // __________ << "Enter army size (>0): ";
	
    // __________ >> __________
	
    // __________ army;
	
    // buildArmy(__________, __________ , cin );
	
    // __________ totalHitPoints = army.__________("demogorgon");
	
    // __________ << "Your army of darkness is " << __________;
	
    cout << " demogorgon hit points strong!" << endl;	
    
    return;
}

Sample Runs

Sample runs of the program. User input is shown in red text.

Sample run 1

Enter army size (>0): 3
Enter 3 monster names and hit points
Each on a new line
demogorgon 100
vampire 50
fluffyKitten 10
Your army of darkness is 100 demogorgon hit points strong!

Sample run 2

Enter army size (>0): 4
Enter 4 monster names and hit points
Each on a new line
demogorgon 100
vampire 50
demogorgon 10
dragon 50
Your army of darkness is 110 demogorgon hit points strong!

How to Submit

IMPORTANT: For all labs in EECS 183, to receive a grade, every student must individually submit the Lab Submission. Late submissions for labs will not be accepted for credit. For this lab, you will receive ten submissions per day with feedback.