In: Computer Science
Using a vector of integers that you define.
Write a C++ program to run a menu driven program with the following choices:
1) Display the ages
2) Add an age
3) Display the average age
4) Display the youngest age
5) Display the number of students who can vote
6) Remove all students less than a given age
7) Quit
Make sure your program conforms to the following requirements:
2. Write a function called getValidAge that allows a user to enter in an integer and loops until a valid number that is >= 0 and < 120 is entered. It returns the valid age. (5 points).
3. Write a function called displayAges that takes in a vector of integers as a parameter and displays the ages in the format in the sample run below. (10 points).
4. Write a function called AddAge that takes in a vector of integers by reference as a parameter, asks the user to input a valid age, and adds it to the vector of integers . (15 points).
5. Write a function called getAverageAge that takes in a vector of integers as a parameter, computes, and returns the average age. (15 points).
6. Write a function called getYoungestAge that takes in a vector of integers as a parameter, computes, and returns the youngest age. (15 points).
7. Write a function called getNumStudentsVote that takes in a vector of integers as a parameter, computes, and returns the number of ages in the vector that are >= 18. (15 points).
8. Write a function called RemoveStudentsLessThanSelectedAge that takes in a vector of integers as a parameter, asks the user for an age, creates a new vector of integers that only contains the ages in the parameter vector which are >= the age selected by the user and returns the new vector. (20 points).
9. Add comments wherever necessary. (5 points)
NOTE: You must take care of the case when the vector is empty and an operation is being performed on it. In such cases the program should display a 0 for the given result.
Sample Runs:
NOTE: not all possible runs are shown below.
Welcome to the students age in class program!
1) Display the ages
2) Add an age
3) Display the average age
4) Display the youngest age
5) Display the number of students who can vote
6) Remove all students less than a given age
7) Quit
Select an option (1..7)..1
Student ages:
1) Display the ages
2) Add an age
3) Display the average age
4) Display the youngest age
5) Display the number of students who can vote
6) Remove all students less than a given age
7) Quit
Select an option (1..7)..3
Average age = 0
1) Display the ages
2) Add an age
3) Display the average age
4) Display the youngest age
5) Display the number of students who can vote
6) Remove all students less than a given age
7) Quit
Select an option (1..7)..4
Youngest age = 0
1) Display the ages
2) Add an age
3) Display the average age
4) Display the youngest age
5) Display the number of students who can vote
6) Remove all students less than a given age
7) Quit
Select an option (1..7)..5
Number of students who can vote = 0
1) Display the ages
2) Add an age
3) Display the average age
4) Display the youngest age
5) Display the number of students who can vote
6) Remove all students less than a given age
7) Quit
Select an option (1..7)..6
Please enter in the age...
5
Students removed
1) Display the ages
2) Add an age
3) Display the average age
4) Display the youngest age
5) Display the number of students who can vote
6) Remove all students less than a given age
7) Quit
Select an option (1..7)..2
Please enter in the age...
4
Age added
1) Display the ages
2) Add an age
3) Display the average age
4) Display the youngest age
5) Display the number of students who can vote
6) Remove all students less than a given age
7) Quit
Select an option (1..7)..2
Please enter in the age...
24
Age added
1) Display the ages
2) Add an age
3) Display the average age
4) Display the youngest age
5) Display the number of students who can vote
6) Remove all students less than a given age
7) Quit
Select an option (1..7)..2
Please enter in the age...
18
Age added
1) Display the ages
2) Add an age
3) Display the average age
4) Display the youngest age
5) Display the number of students who can vote
6) Remove all students less than a given age
7) Quit
Select an option (1..7)..2
Please enter in the age...
12
Age added
1) Display the ages
2) Add an age
3) Display the average age
4) Display the youngest age
5) Display the number of students who can vote
6) Remove all students less than a given age
7) Quit
Select an option (1..7)..1
Student ages:
4 24 18 12
1) Display the ages
2) Add an age
3) Display the average age
4) Display the youngest age
5) Display the number of students who can vote
6) Remove all students less than a given age
7) Quit
Select an option (1..7)..3
Average age = 14
1) Display the ages
2) Add an age
3) Display the average age
4) Display the youngest age
5) Display the number of students who can vote
6) Remove all students less than a given age
7) Quit
Select an option (1..7)..4
Youngest age = 4
1) Display the ages
2) Add an age
3) Display the average age
4) Display the youngest age
5) Display the number of students who can vote
6) Remove all students less than a given age
7) Quit
Select an option (1..7)..5
Number of students who can vote = 2
1) Display the ages
2) Add an age
3) Display the average age
4) Display the youngest age
5) Display the number of students who can vote
6) Remove all students less than a given age
7) Quit
Select an option (1..7)..6
Please enter in the age...
15
Students removed
1) Display the ages
2) Add an age
3) Display the average age
4) Display the youngest age
5) Display the number of students who can vote
6) Remove all students less than a given age
7) Quit
Select an option (1..7)..1
Student ages:
24 18
1) Display the ages
2) Add an age
3) Display the average age
4) Display the youngest age
5) Display the number of students who can vote
6) Remove all students less than a given age
7) Quit
Select an option (1..7)..3
Average age = 21
1) Display the ages
2) Add an age
3) Display the average age
4) Display the youngest age
5) Display the number of students who can vote
6) Remove all students less than a given age
7) Quit
Select an option (1..7)..4
Youngest age = 18
1) Display the ages
2) Add an age
3) Display the average age
4) Display the youngest age
5) Display the number of students who can vote
6) Remove all students less than a given age
7) Quit
Select an option (1..7)..5
Number of students who can vote = 2
1) Display the ages
2) Add an age
3) Display the average age
4) Display the youngest age
5) Display the number of students who can vote
6) Remove all students less than a given age
7) Quit
Select an option (1..7)..-8
Select an option (1..7)..8
Select an option (1..7)..1
Student ages:
24 18
1) Display the ages
2) Add an age
3) Display the average age
4) Display the youngest age
5) Display the number of students who can vote
6) Remove all students less than a given age
7) Quit
Select an option (1..7)..2
Please enter in the age...
-8
Please enter in a valid age (1-120) ...
130
Please enter in a valid age (1-120) ...
55
Age added
1) Display the ages
2) Add an age
3) Display the average age
4) Display the youngest age
5) Display the number of students who can vote
6) Remove all students less than a given age
7) Quit
Select an option (1..7)..1
Student ages:
24 18 55
1) Display the ages
2) Add an age
3) Display the average age
4) Display the youngest age
5) Display the number of students who can vote
6) Remove all students less than a given age
7) Quit
Select an option (1..7)..7
Process finished with exit code 0
// do comment if any problem arises
// code
#include <iostream>
#include <vector>
using namespace std;
// this function returns valid age
int getValidAge()
{
int age;
cout << "Please enter in the age..\n";
cin >> age;
if (age >= 0 && age < 120)
{
return age;
}
cout << "Please enter a valid age!\n";
return getValidAge();
}
// this function displays ages
void displayAges(vector<int> ages)
{
cout << "Student ages: \n";
for (auto it = ages.begin(); it != ages.end(); it++)
cout << *it << " ";
cout << endl;
}
// this function adds age to vector
void AddAge(vector<int> &ages)
{
int age = getValidAge();
cout << "Age added\n";
ages.push_back(age);
}
// this function returns average of ages
int getAverageAge(vector<int> ages)
{
if (ages.size() == 0)
return 0;
int total = 0;
for (auto it = ages.begin(); it != ages.end(); it++)
total += *it;
return total / ages.size();
}
// this function returns youngest age
int getYoungestAge(vector<int> ages)
{
if (ages.size() == 0)
return 0;
int youngest = ages[0];
for (auto it = ages.begin(); it != ages.end(); it++)
{
if (youngest > *it)
{
youngest = *it;
}
}
return youngest;
}
// this function returns number of students who can vote
int getNumStudentsVote(vector<int> ages)
{
int n = 0;
for (auto it = ages.begin(); it != ages.end(); it++)
{
if (*it >= 18)
n++;
}
return n;
}
// this function Remove Students Less Than Selected Age
vector<int> RemoveStudentsLessThanSelectedAge(vector<int> ages)
{
int age = getValidAge();
vector<int> newages;
for (auto it = ages.begin(); it != ages.end(); it++)
{
if (*it > age)
{
newages.push_back(*it);
}
}
cout << "Students removed\n";
return newages;
}
int get_choice()
{
cout << "1) Display the ages\n";
cout << "2) Add an age\n";
cout << "3) Display the average age\n";
cout << "4) Display the youngest age\n";
cout << "5) Display the number of students who can vote\n";
cout << "6) Remove all students less than given age\n";
cout << "7) Quit\n";
cout << "Select an option (1..7)..";
int choice;
cin >> choice;
while (choice > 7 || choice < 1)
{
cout << "Select an option (1..7)..";
cin >> choice;
}
return choice;
}
int main()
{
cout << "Welcome to the students age in class program!\n";
vector<int> ages;
while (true)
{
int choice = get_choice();
if (choice == 7)
break;
switch (choice)
{
case 1:
displayAges(ages);
break;
case 2:
AddAge(ages);
break;
case 3:
cout << "Average = " << getAverageAge(ages) << endl;
break;
case 4:
cout << "Youngest age = " << getYoungestAge(ages) << endl;
break;
case 5:
cout << "Number of students who can vote = " << getNumStudentsVote(ages) << endl;
break;
case 6:
ages = RemoveStudentsLessThanSelectedAge(ages);
break;
}
}
}
Output is too long but this code has been tested ans works fine.