Object Oriented Programming in C++

Course Information

  • Course Code: CSCI 211

  • Lab: Lab 20

  • Instructor: Yun Wang

  • Institution: Queens College

Lab Assignment Overview

  • Task: Write a complete C++ program that performs the following tasks:

    1. Ask the user to enter the number of grading components.

    2. Collect the grade and weight for each component.

    3. If the weights do not sum to 1, print "Invalid Input!" and terminate the program.

    4. Calculate and print the overall grade.

Example Program Execution
  • Input:

    • Number of grading components: 3

    • Grades: 80, 70, 90

    • Weights: 0.2, 0.3, 0.5

  • Output:

    • "Your overall grade is 82"

C++ Code Example

#include <iostream>
#include <vector>
using namespace std;

int main() {
    int n;
    cout << "Enter the number of grading components: ";
    cin >> n;
    vector<int> grades(n);
    vector<double> weights(n);

    double total = 0;
    for(int i = 0; i < n; i++) {
        cout << "Enter the grade for each component: ";
        cin >> grades[i];
    }

    cout << "Enter the weight for each component: ";
    for(int i = 0; i < n; i++) {
        cin >> weights[i];
        total += weights[i];
    }

    if(total != 1) {
        cout << "Invalid Input!" << endl;
        return 0;
    }

    double weighted_grade = 0;
    for(int i = 0; i < n; i++) {
        weighted_grade += (grades[i] * weights[i]);
    }

    cout << "Your overall grade is " << weighted_grade << endl;
    return 0;
}

2D Vectors in C++

Example 1 of 2D Vector Usage
  • Initialization: vector<int> v1;

  • Operations:

    • v1.push_back(1);

    • v1.push_back(2);

  • Copying Vectors:

    • vector<int> v2 = v1; // Copies v1 into v2

    • v2.push_back(3);

    • vector<int> v3(v2); // Copies v2 into v3

    • v3.push_back(4);

  • Iterating Using For-Each Loop (from C++11):

  for(int n : v1) cout << n << " ";
  cout << endl;
  • Creating a 2D Vector:

  vector<vector<int>> v4;
  v4.push_back(v1);
  v4.push_back(v2);
  v4.push_back(v3);
  • Accessing Elements in v4:

    • cout << v4[2][2] << endl;

    • cout << v4[0].size() << endl;

    • cout << v4[1].size() << endl;

    • cout << v4[2].size() << endl;

Example Output from Example 1
  • Normal Console Output:

1 2 
1 2 3 
1 2 3 4 
  • Program Ended with Exit Code: 0

Example 2 of 2D Vector Usage
  • Initialization and Size Setting:

  vector<vector<int>> a(5, vector<int>(4, 7));
  • Creates a vector a with 5 elements, each being a vector of size 4, all initialized to 7.

    • Output Representation of a:

7777
7777
7777
7777
7777
Example 3 of 2D Vector Usage
  • Vector Initialization:

  vector<vector<int>> b = {{1, 2}, {4, 5, 6}, {7, 8, 9, 10}};
  • Manipulating b:

    • b.push_back(vector<int>(5, 12)); // Adds vector {12, 12, 12, 12, 12} to b

    • b[1].push_back(11); // Appends 11 to the second vector

  • Output Representation of b:

1 2 
4 5 6 11 
7 8 9 10 
12 12 12 12 12

Array of Vectors in C++

  • Initialization:

  vector<int> c[3]; 
  • Creates an array of 3 elements, where each element is an empty vector of integers.

    • Operations on c:

  • Pushing Elements:

  c[0].push_back(7);
  c[0].push_back(5);
  c[1].push_back(2);
  c[1].push_back(3);
  c[1].push_back(4);
  c[2].push_back(9);
  c[2].push_back(c[1].back()); // Copies last element of c[1] to c[2]
  • Remove Last Element:

  c[0].pop_back();
  • Iterating through the Array of Vectors:

  for(int i = 0; i < 3; i++) { // row
      for(int j = 0; j < c[i].size(); j++) { // column
           cout << c[i][j] << " ";
      }
      cout << endl;
  }
Example Output from Array of Vectors before and after pop_back()
  • Before pop_back():

7 5 
  • After pop_back():

2 3 4 
9 4 

Programming Task for Vectors

Task Description
  • Write a complete C++ program that:

    1. Asks the user to enter integers until 0 is entered to stop input.

    2. Stores these numbers into one or more vectors.

    3. Prints even and odd numbers separately.

Example Program Execution
  • Input:

Enter some integers until 0 to stop: 2 4 3 1 5 8 9 6 7 0
  • Output:

Evens: 2 4 8 6 
Odds: 3 1 5 9 7 

Quiz on Vectors

Quiz Information
  • Date: Next Wednesday

  • Topics Covered: Vector usage (1D and 2D)

  • Contextual Quiz Example:

    • Statement: "90% of freshmen think that they are above average."

    • Input: Each test case represents the number of students' scores.

    • Output Requirement: Print the ratio of students with scores above average.

Input/Output Example
  • Input Format: Lines representing test cases

  • Example Test Case Input:

5
5 50 50 70 80 100
7 100 95 90 80 70 60 50
3 70 90 80
  • Expected Output:

40.000%
57.143%
66.667%