In: Computer Science
Directions:
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:
1. 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.
2. 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.
3. 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 .
4. Write a function called getAverageAge that takes in a vector of integers as a parameter, computes, and returns the average age.
5. Write a function called getYoungestAge that takes in a vector of integers as a parameter, computes, and returns the youngest age.
6. 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).
7. 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.
8. Add comments wherever necessary.
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
General Requirements:
1. Please make sure that you're conforming to specifications (program name, print statements, expected inputs and outputs etc.). Not doing so will result in a loss of points. This is especially important for prompts.
2. If we have listed a specification and allocated point for it, you will lose points if that particular item is missing from your code, even if it is trivial.
3. No global variables (variables outside of main() ) unless they are constants.
4. All input and output must be done with streams, using the library iostream
5. You may only use the iostream, iomanip, and vector libraries. Including unnecessary libraries will result in a loss of points.
6. NO C style printing is permitted. (Aka, don't use printf). Use cout if you need to print to the screen.
7. When you write source code, it should be readable and well-documented (comments).
Screenshot
Program
//Header files for I/o and vector
#include <iostream>
#include<vector>
using namespace std;
//Function prototypes
int menu();
int getValidAge();
void displayAges(vector<int>& ages);
void addAge(vector<int>& ages);
int getAverageAge(vector<int> ages);
int getYoungestAge(vector<int> ages);
int getNumStudentsVote(vector<int> ages);
vector<int>
RemoveStudentsLessThanSelectedAge(vector<int> ages);
int main()
{
//Create a vector
vector<int> ages;
//Welcome message
cout << "Welcome to the students age in class
program!" << endl;
//User menu
int opt = menu();
//Loop until exit
while (opt != 7) {
//Switch into corresponding
option
switch (opt)
{
case 1:
displayAges(ages);
break;
case 2:
addAge(ages);
break;;
case 3:
cout
<<"Average age = "<< 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;
default:
break;
}
opt = menu();
}
cout << "Process finished with exit code 0"
<< endl;
}
/*
Function to get user option
ParamIn: None;
Param Out: opt, user choice
Description:Display user options and prompt for
option
Take user choice and check error, then loop until
Correct option and return option
*/
int menu() {
int opt;
cout << "1) Display the ages\n"
<< "2) Add an age\n"
<< "3) Display the average
age\n"
<< "4) Display the youngest
age\n"
<< "5) Display the number of
students who can vote\n"
<< "6) Remove all students
less than a given age\n"
<< "7) Quit\n";
cout << "Select an option (1..7)..";
cin >> opt;
while (opt < 1 || opt>7) {
cout << "Select an option
(1..7)..";
cin >> opt;
}
return opt;
}
/*
Function: getVallidAge()
ParamIn: None
Param Out: None
Description: Prompt user for an age
check >=0 and <=120
Otherwise loop until correct
and return age
*/
int getValidAge() {
int age;
cout << "Please enter in the age...\n";
cin >> age;
while (age < 0 || age>120) {
cout << "Please enter in a
valid age (1-120) ...\n";
cin >> age;
}
return age;
}
/*
Function: displayAge
ParamIn: Reference passed vector
Param Out: None
Description: Loop hrough vector and display ages
*/
void displayAges(vector<int>& ages) {
cout << "Student ages:\n";
for (int i = 0; i < ages.size(); i++) {
cout << ages[i] << "
";
}
cout << endl;
}
/*
Function: addAge
ParamIn: Reference vector
ParamOut: None
Description: Call getValidAge function to get a valid age
and add into vector
*/
void addAge(vector<int>& ages) {
ages.push_back(getValidAge());
cout << "Age added\n";
}
/*
Function: getAverageAge
ParamIn: age vector
ParamOut: Average
Description: Function loop through ages
Return average age, if empty vector retun 0
*/
int getAverageAge(vector<int> ages) {
if (ages.size() == 0) {
return 0;
}
else {
int avg = 0;
for (int i = 0; i < ages.size();
i++) {
avg +=
ages[i];
}
return avg / ages.size();
}
}
/*
Function: GetYoungestAge
ParamIn: Age vector
ParamOut: Return youngest age
Desription: If empty vector return 0
Otherwise loop through the vector to get youngest age
*/
int getYoungestAge(vector<int> ages) {
if (ages.size() == 0) {
return 0;
}
else {
int age = ages[0];
for (int i = 1; i < ages.size();
i++) {
if (age >
ages[i]) {
age = ages[i];
}
}
return age;
}
}
/*
Function: getNumStudentsVote
ParamIn: Age vector
ParamOut: Return number ages >=18
Desription: If empty vector return 0
Otherwise loop through the vector to get number
*/
int getNumStudentsVote(vector<int> ages) {
if (ages.size() == 0) {
return 0;
}
else {
int cnt = 0;
for (int i = 0; i < ages.size();
i++) {
if
(ages[i]>=18) {
cnt++;
}
}
return cnt;
}
}
/*
Function: RemoveStudentsLessThanSelectedAge
ParamIn: Age vector
ParamOut: new vector
Desription: Prompt for an age
If empty vector return itself
Otherwise loop through the vector to get
ages >= prompted age
*/
vector<int>
RemoveStudentsLessThanSelectedAge(vector<int> ages) {
int age;
cout << "Please enter in the age...\n";
cin >> age;
cout << "Students removed\n";
if (ages.size() == 0) {
return ages;
}
else {
vector<int> newAges;
for (int i = 0; i < ages.size();
i++) {
if
(ages[i]>=age) {
newAges.push_back(ages[i]);
}
}
return newAges;
}
}
---------------------------------
Output
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...
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...
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...
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 18 24 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)..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:
18 24
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:
18 24
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:
18 24 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