Prerequisites

Recap

Lecture 11

Follow up more on these questions in Perplexity

The recap questions are based off on Lecture 11 main.cpp file

  • What are the purposes of the Bill and Person structs in the program?

  • How does the upper function work, and what is its use in the main loop for input handling?

  • What do the displayUnpaidBills and displayPeople functions display to the user, and which arguments do they take?

  • Describe how the program prompts for and stores roommates and bills. What control structures are used?

  • How is the input “STOP” handled, and why is the input converted to uppercase in comparisons?

  • What happens after new bill information is entered? How does the program update its lists?

  • What types of containers (vector, array, etc.) are used to store the bills and roommate data?

  • How does the program loop through and output unpaid bills and roommate names? Which loop constructs are used?

  • How does the contributions vector track each roommate’s input, and how is it used in calculating totals?

  • How is the fair share for each roommate determined by the program?

  • What message does the program display if a roommate has paid less, more, or exactly their fair share?

  • Describe how the total expenses and total contributions are calculated and displayed to the user?

Lecture 12

Follow up more on these questions in Perplexity

The recap questions are based off on Lecture 12 main.cpp file

  • What are the key fields defined in the Bill and Person structs, and what does each represent in this program?

  • How does the program use vectors to store and manage collections of bills and people?

  • What is the purpose of the populatePeople and populateBills functions and how do they modify the corresponding vectors?

  • How is user input used to add new elements to the roommates and bills vectors?

  • Describe how the assignPayments function works with the bills and roommates vectors to update payment status and contributions.

  • Explain how the displayUnpaidBills and displayPeople functions use vectors as input parameters and what data they output.

  • What is the significance of passing vectors by reference versus by value in function parameters throughout this code?

  • How is a fair share of total expenses calculated and displayed using the tallyUp function and vectors?


Group Questions

Recommended

Use functions to organize your lines of code.

1. Curving student scores

Write a program that keeps track of students’ scores using vector and struct.

  • Then, curve the scores from the highest student score (also known as the Standard Curve)
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
 
using namespace std;
 
// _t is an extra mark to know that the struct is a template, not an object of a specific struct
struct Student_t {
    string name;
    double pointsEarned;
    string gradeLetter;
    
    int id;
} sakura, alan, graham;
 
// Each student object will have an assigned id value that is one unit more than their index
void assignStudentsIds(vector<Student_t> &students) {
    for (size_t i = 0; i < students.size(); i++) {
        /*
            Alternative:
            
            Student_t &student = students[i];
            student.id = i + 1;
        */
        
        students[i].id = i + 1;
    }
}
 
// Assigns the names and pointsEarned for the student objects: sakura, alan, graham
void initStudents() {
    sakura.name = "sakura";
    sakura.pointsEarned = 20;
    
    alan.name = "alan";
    alan.pointsEarned = 65;
    
    graham.name = "graham";
    graham.pointsEarned = 89;
}
 
 
// Create a grade letter in string based on the points earned and maximum points possible
string getGradeLetter(const double pointsEarned, const double maxPts) {
    const double score = pointsEarned/maxPts;
    
    if (score >= 0.9) {
        return "A";
    } else if (score >= 0.8) {
        return "B";
    } else if (score >= 0.7) {
        return "C";
    } else if (score >= 0.6) {
        return "D";
    } else {
        return "F";
    };
}
 
// Get the highest score among the students
double getStudentsHighestScore(const vector<Student_t> &students) {
    double highestScore = students[0].pointsEarned; // default to the first student's score
    
    for (Student_t student : students) {
        if (highestScore < student.pointsEarned) {
            highestScore = student.pointsEarned;
        }
    }
    
    return highestScore;
}
 
// Get the average score of students' scores
double getStudentsAverage(const vector<Student_t> &students) {
    double scoreAverage = 0, totalScores = 0;
    
    for (Student_t student : students) {
        totalScores += student.pointsEarned;
    }
    
    scoreAverage = totalScores/students.size();
    
    return scoreAverage;
}
 
// Curves the student scores
void curveStudentScores(vector<Student_t> &students, const double maximumPossiblePts) {
    const double highestScore = getStudentsHighestScore(students);
    const double studentsAverage = getStudentsAverage(students);
    
    // We add & (ampersand) to this loop because we need to modify the original student value
    for (Student_t &student : students) {
        // abs() is an absolute value function to ensure the number is always positive
        // In standard curve, we add the points difference from the students' highest score and the maximum points possible
        
        const double pointsDifference = abs(  highestScore - maximumPossiblePts );
        // Alternative:                 abs ( maximumPossiblePts - highestScore );
        
        // This calling function will add the points difference to their score
        student.pointsEarned += pointsDifference;
    }
}
 
void printStudentScores(const vector<Student_t> &students, const int maxPossiblePts) {
    cout << "STUDENT SCORES [ " << maxPossiblePts << " MAX POINTS POSSIBLE ]\n";
    cout << "===============\n";
    for (Student_t student : students) {
        // Prints on the same line until endl is printed;
        // Format: student_name [student_id]: grade_letter (pointsEarned/maxPossiblePoints)
        cout << student.name << " [" << student.id << "]: ";
        cout << getGradeLetter(student.pointsEarned, maxPossiblePts) << " ";
        // 
        cout << "(" << student.pointsEarned << "/" << maxPossiblePts << ")";
        // Ends the line for the next student score
        cout << endl;
    }
    cout << "===============\n";
}
 
int main() {
    /* Alternative method (if user needs to prompt the max pts earned)
    
        int maxPtsEarned;
        cout << "How many points are in this assignment";
        cin >> maxPtsEarned;
    */
    
    const int maxPossiblePoints = 100;
    
    // assign the name and points earned for each student object
    initStudents();
    
    vector<Student_t> students = {sakura, alan, graham};
    /*
        Same thing as:
            vector<Student_t> students = { {"sakura", 20}, {"alan", 65}, {"graham", 89} };
            
            
        Same thing as:
            vector<Student_t> students;
            
            students.push_back({"sakura", 20});
            students.push_back({"alan", 65});
            students.push_back({"graham", 89});
    */
    
    // \n is the same as endl but in string
    
    // We need to know the highest students score before the curve hits for the new maximum points possible
    const double highestScore = getStudentsHighestScore(students);
    
    cout << "BEFORE CURVE:\n\n";
    
    printStudentScores(students, maxPossiblePoints);
    
    cout << "\n\n";
    
    
    curveStudentScores(students, maxPossiblePoints);
    
    cout << "AFTER CURVE:\n\n";
    
    printStudentScores(students, highestScore);
    
    cout << "\n\n";
 
    return 0;
}

2. Rectangle computation

Write a program to compute the area and perimeters of the rectangle using struct.

Challenge: Create a function that could double the area of the rectangle and print out the new area.

3. Weekly Temperatures F to C

Write a program to convert a vector of the weekly temperatures in Fahrenheit to Celsius. Formula: c = (Fahrenheit - 32) * 5/9

4. Scores and Player Info

Imagine you are developing a simple tracking system for a game. You want to store the scores for each round of the game using a vector<int>, and separately hold player info using a struct.

  • Create a struct for each player with their name and age
  • Use a vector of integers to track their scores inside the struct
  • Write a function to calculate the average score of each player
  • Write a function to display a player’s information
  • Then, run the function that display the player’s information

General Questions

  1. ~~List two key differences between vectors and arrays in C++. Why might you choose one over the other?~~

  2. How do you add an element to the end of a vector<int>?

  3. Given the code

    struct Point {
        double x, y;
     }

    a. Declare and initialize a Point object representing the origin. b. Write a function that returns the distance from the origin

  4. How would you find out how many elements are in a vector<string> called words?

  5. What happens if you try to access v.at(10) when v is a vector of size 5?

  6. Given a vector of doubles called temperatures, write a loop that prints every element using a range-based for loop.

  7. Write a struct that can store the name and author of this book: The Hobbit by J.R.R. Tolkien with 310 pages.

    Challenge: Include the page numbers in the struct

  8. Give an example of passing a vector to a function by reference so it CAN be modified.

  9. What’s the advantage of passing a large struct to a function by const reference instead of by value?

  10. Debug the code — Identify the error and resolve the issue.

#include <vector>
#include <iostream>
 
using namespace std;
 
void printVector(vector<int> numbers) {
	for (size_t i = 0; i < numbers.size(); i++) {
		cout << numbers[i];
		if (i < numbers.size()-1)
			cout << ", ";
	}
}
 
 
int main() {
	vector<int> nums = {1, 2, 3, 4};
	printVector(numbers);
 
	return 0;
}