Counting People Who Can Watch The Movie In C++ - C++ Programming Tutorial
C++ Course / Miscellaneous / Counting People Who Can Watch The Movie In C++

Counting People Who Can Watch The Movie In C++

BLUF: Mastering Counting People Who Can Watch The Movie In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Counting People Who Can Watch The Movie In C++

C++ is renowned for its efficiency. Learn how Counting People Who Can Watch The Movie In C++ enables low-level control and high-performance computing in the tutorial below.

Introduction:

Based on criteria like age restrictions and limited seating capacity at a movie theater, it is possible to determine the maximum number of individuals who can attend a particular screening.

We will address this matter by exploring the process of designing an application to tally qualified users within a group using C++. Initially, we will delve into the requirements for determining eligibility.

Problem Statement:

There are two requirements for watching a film. Hence, this group aims to determine the feasibility of watching a movie:

  • Age Limitation: A movie enforces a specific age limit for viewers.
  • Seating Limitation: The maximum seating capacity of the theater is restricted.

Develop a script that calculates the count of individuals exceeding the minimum permissible age for a movie, based on the total number of viewers needed.

Solution Approach:

In order to help us with the problem, we will work as follows:

  • Input Details: Fetch the relevant information, such as group numbers or the ages of the groups, the allowable age limit, and the greatest number of seats allowed.
  • Filter Eligible People: Establish the number of individuals that qualify for the minimum age requirement set.
  • Take into consideration seating limit: Determine the number of people who are entitled to attend the meeting and check if there are enough seats available. If there are insufficient seats, make the necessary changes.
  • Output: Print the number of people who can watch the movie.
  • Example 1:

Let's consider an example to demonstrate how to tally the number of individuals eligible to view the film in C++.

Example

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

int main() {
    int groupSize, minAge, seatingCapacity;

    // Input group size, minimum age, and seating capacity
    cout << "Enter the number of people in the group: ";
    cin >> groupSize;

    cout << "Enter the minimum age required for the movie: ";
    cin >> minAge;

    cout << "Enter the seating capacity of the theater: ";
    cin >> seatingCapacity;

    // Input ages of the group members
    vector<int> ages(groupSize);
    cout << "Enter the ages of the group members: ";
    for (int i = 0; i < groupSize; i++) {
        cin >> ages[i];
    }

    // Count eligible people based on age
    int eligibleCount = 0;
    for (int age : ages) {
        if (age >= minAge) {
            eligibleCount++;
        }
    }

    // Account for seating capacity
    int peopleWhoCanWatch = min(eligibleCount, seatingCapacity);

    // Output the result
    cout << "Number of people who can watch the movie: " << peopleWhoCanWatch << endl;

    return 0;
}

Output:

Output

Enter the number of people in the group: 6  
Enter the minimum age required for the movie: 18  
Enter the seating capacity of the theater: 4  
Enter the ages of the group members: 16 21 18 17 25 19  
Number of people who can watch the movie: 4

Out of the six individuals, only four have the privilege to utilize the service's age-restricted features, specifically those aged 21, 18, 25, and 19. The console's restriction allows a maximum of four individuals to access and enjoy the service.

Explanation of the code:

  • Input Information: The program accepts inputs, such as the number of users, their ages, the lowest age limit to be accepted, and the theatre capacity.
  • Age Verification: The entire group's ages are verified for meeting the minimum requirements needed.
  • Seating Optimization: The use of the min function protects the theatre seating arrangement by placing a restriction on the number of users permitted into the designated auditorium.
  • Result: The output from the program is the number of people that were computed to be able to view the movie within the stipulated constraints.
  • Example 2:

Let's consider another scenario to demonstrate the process of tallying the individuals eligible to view the film using C++.

Example

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

// Structure to store details of each person
struct Person {
    int id;
    int age;
    bool eligible;
};

// Function to display the report
void displayReport(const vector<Person>& group, int totalSeats) {
    cout << "\nDetailed Report:\n";
    cout << setw(10) << "ID" << setw(10) << "Age" << setw(15) << "Eligible" << setw(15) << "Seat Assigned" << endl;
    cout << string(50, '-') << endl;

    for (size_t i = 0; i < group.size(); i++) {
        cout << setw(10) << group[i].id
             << setw(10) << group[i].age
             << setw(15) << (group[i].eligible ? "Yes" : "No")
             << setw(15) << ((group[i].eligible && i < totalSeats) ? "Yes" : "No") 
             << endl;
    }
    cout << endl;
}

int main() {
    int groupSize, minAge, seatingCapacity;

    // Input group size, minimum age, and seating capacity
    cout << "Enter the number of people in the group: ";
    cin >> groupSize;

    cout << "Enter the minimum age required for the movie: ";
    cin >> minAge;

    cout << "Enter the seating capacity of the theater: ";
    cin >> seatingCapacity;

    // Input ages of the group members
    vector<Person> group(groupSize);
    cout << "Enter the ages of the group members:\n";
    for (int i = 0; i < groupSize; i++) {
        cout << "Person " << (i + 1) << " age: ";
        cin >> group[i].age;
        group[i].id = i + 1;
        group[i].eligible = (group[i].age >= minAge);
    }

    // Filter and sort eligible members by age in descending order (older people get priority)
    sort(group.begin(), group.end(), [](const Person& a, const Person& b) {
        if (a.eligible != b.eligible) return a.eligible > b.eligible; // Eligible people first
        return a.age > b.age; // Older people first
    });

    // Count eligible people
    int eligibleCount = 0;
    for (const Person& person : group) {
        if (person.eligible) eligibleCount++;
    }

    // Calculate how many people can actually watch the movie
    int peopleWhoCanWatch = min(eligibleCount, seatingCapacity);

    // Output the results
    cout << "\nSummary:\n";
    cout << "Total group members: " << groupSize << endl;
    cout << "Minimum age required: " << minAge << endl;
    cout << "Seating capacity: " << seatingCapacity << endl;
    cout << "Number of eligible people: " << eligibleCount << endl;
    cout << "Number of people who can watch the movie: " << peopleWhoCanWatch << endl;

    // Display detailed report
    displayReport(group, peopleWhoCanWatch);

    return 0;
}

Output:

Output

Enter the number of people in the group: 10
Enter the minimum age required for the movie: 13
Enter the seating capacity of the theater: 200
Enter the ages of the group members:
Person 1 age: 22
Person 2 age: 10
Person 3 age: 23
Person 4 age: 32
Person 5 age: 33
Person 6 age: 58
Person 7 age: 62
Person 8 age: 9
Person 9 age: 13
Person 10 age: 18

Summary:
Total group members: 10
Minimum age required: 13
Seating capacity: 200
Number of eligible people: 8
Number of people who can watch the movie: 8

Detailed Report:
        ID       Age       Eligible  Seat Assigned
--------------------------------------------------
         7        62            Yes            Yes
         6        58            Yes            Yes
         5        33            Yes            Yes
         4        32            Yes            Yes
         3        23            Yes            Yes
         1        22            Yes            Yes
        10        18            Yes            Yes
         9        13            Yes            Yes
         2        10             No             No
         8         9             No             No

Explanation of the Code:

  • Person Struct The unique attributes of a person, such as age, ID (person number) and eligibility (age requirement), are embedded in a specific structure called a Person.
  • Sorting Eligible Members The group is sorted in eligible and non-eligible members based on age. The people who qualify the age criteria are at the front of the line and among them older people always get the first priority for empty seats.
  • Detailed Report After the data is processed, a computed report follows that shows each person's age, eligibility and whether a seat was assigned.
  • Flexible and Scalable Programs of this kind elegantly tackle edge cases like insufficient seats or lack of eligible people. Sorting and Priority: Prioritizing older eligible people. Scalability: It handles larger groups and provides better insights about the assignment of seats and eligibility. Detailed Reporting: Adjustable reporting that shows each person's eligibility and seat assignment. Edge Case Handling: Cases such as few eligible viewers and less seating arrangements are handled easily.
  • Sorting and Priority: Prioritizing older eligible people.
  • Scalability: It handles larger groups and provides better insights about the assignment of seats and eligibility.
  • Detailed Reporting: Adjustable reporting that shows each person's eligibility and seat assignment.
  • Edge Case Handling: Cases such as few eligible viewers and less seating arrangements are handled easily.
  • Example 3:

Let's consider another scenario to demonstrate the process of counting individuals eligible to view the film using C++.

Example

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

int main() {
    int groupSize, minAge, seatingCapacity;

    // Input group size, minimum age, and seating capacity
    cout << "Enter the number of people in the group: ";
    cin >> groupSize;

    cout << "Enter the minimum age required for the movie: ";
    cin >> minAge;

    cout << "Enter the seating capacity of the theater: ";
    cin >> seatingCapacity;

    // Input ages and filter eligible people
    vector<int> eligibleAges;
    cout << "Enter the ages of the group members:\n";
    for (int i = 0; i < groupSize; i++) {
        int age;
        cin >> age;
        if (age >= minAge) eligibleAges.push_back(age);
    }

    // Sort eligible people by age in ascending order
    sort(eligibleAges.begin(), eligibleAges.end());

    // Use a stack to manage eligible people (oldest on top)
    stack<int> eligibleStack;
    for (int age : eligibleAges) {
        eligibleStack.push(age);
    }

    // Determine how many people can watch and assign seats
    int peopleWhoCanWatch = min((int)eligibleStack.size(), seatingCapacity);

    // Display results
    cout << "\nNumber of people who can watch the movie: " << peopleWhoCanWatch << endl;

    cout << "Ages of people assigned seats: ";
    for (int i = 0; i < peopleWhoCanWatch; i++) {
        cout << eligibleStack.top();
        eligibleStack.pop();
        if (i < peopleWhoCanWatch - 1) cout << ", ";
    }
    cout << endl;

    return 0;
}

Output:

Output

Enter the number of people in the group: 7
Enter the minimum age required for the movie: 18
Enter the seating capacity of the theater: 4
Enter the ages of the group members:
16 20 18 25 19 17 22
Number of people who can watch the movie: 4
Ages of people assigned seats: 25, 22, 20, 19

Explanation:

  • Filtering Ages: Only the ages of people meeting the minimum age requirement are added to the eligibleAges vector.
  • Sorting: Ages are sorted in ascending order to ensure the stack's top contains the oldest eligible person.
  • Using a Stack: Eligible ages are pushed onto the stack. It ensures the oldest people (highest priority) are at the top of the stack.
  • Seat Assignment: The program pops elements from the stack until the seating capacity is reached, assigning seats to the oldest eligible people.
  • Output: The program outputs the number of people who can watch the movie and their ages in order of seat assignment.
  • Conclusion:

By utilizing this C++ code, individuals can precisely predict the count of individuals meeting the age requirement and seating availability to watch a film. The algorithm is simple yet holds significant practical value in determining eligibility and managing resource constraints across various real-world scenarios.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience