Question

In: Computer Science

Directions: Using a vector of integers that you define. Write a C++ program to run a...

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).

Solutions

Expert Solution

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


Related Solutions

Using a vector of integers that you define. Write a C++ program to run a menu...
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...
Directions: Write a C++ program that will create an array of four integers. It will allow...
Directions: Write a C++ program that will create an array of four integers. It will allow the user to enter in four valid scores and store them in the array. ( valid range is 0 - 100) It will compute the letter grade based upon the four scores, namely, A = 90 - 100, B= 80- 89, C = 70-79, D = 60-69, otherwise F. It will display the scores and letter grade to the screen. NOTE: No menu is...
Write a C++ program that inputs a sequence of integers into a vector, computes the average,...
Write a C++ program that inputs a sequence of integers into a vector, computes the average, and then outputs the # of input values, the average, and the values themselves. There are 0 or more inputs values, followed by a negative value as the sentinel; the negative value is not stored and not counted. The following sample input: 10 20 0 99 -1 would produce the following output: N: 4 Avg: 32.25 10 20 0 99 The main program has...
Write a C++ program that inputs a sequence of integers into a vector, computes the average,...
Write a C++ program that inputs a sequence of integers into a vector, computes the average, and then outputs the # of input values, the average, and the values themselves. There are 0 or more inputs values, followed by a negative value as the sentinel; the negative value is not stored and not counted. The following sample input: 10 20 0 99 -1 would produce the following output: N: 4 Avg: 32.25 10 20 0 99 The main program has...
For this assignment, you need to write a parallel program in C++ using OpenMP for vector...
For this assignment, you need to write a parallel program in C++ using OpenMP for vector addition. Assume A, B, C are three vectors of equal length. The program will add the corresponding elements of vectors A and B and will store the sum in the corresponding elements in vector C (in other words C[i] = A[i] + B[i]). Every thread should execute an approximately equal number of loop iterations. The only OpenMP directive you are allowed to use is:...
Write a C++ program that 1) generates a vector containing 10 different random integers with values...
Write a C++ program that 1) generates a vector containing 10 different random integers with values between 1 and 100, then 2) calculates the average value in that vector in floating point format with 1 decimal place. Output the vector values and the average value to cout. Your program output should look like this: Vector values: 3, 78, 55, 37, 8, 17, 43, 60, 94, 1 Average value: 39.6
Given a queue of integers, write an algorithm and the program in c++ that, using only...
Given a queue of integers, write an algorithm and the program in c++ that, using only the queue ADT, calculates and prints the sum and the average of the integers in the queue without changing the contents of the queue.
Write a  program in C++ using a vector to create the following output. Declare a vector named  numbers    -  Don’t...
Write a  program in C++ using a vector to create the following output. Declare a vector named  numbers    -  Don’t specify a size and don’t initialize with values. Starting with 2, pushing these into the back of the vector:   2, 4, 6, 8, 10 vector capacity (array size) changes and is dependent on the compiler. The size of the list is now 5. The vector capacity (array size) is 6. The back element is: 10 The front element is: 2 Now deleting the value at...
In C++, Write the following program using a vector: A common game is the lottery card....
In C++, Write the following program using a vector: A common game is the lottery card. The card has numbered spots of which a certain number are selected at random. Write a Lotto() function that takes two arguments. The first should be the number of spots on a lottery card and the second should be the number of spots selected at random. The function should return a vector object that contains, in sorted order, the numbers selected at random. Use...
For this assignment, write a parallel program in C++ using OpenMP for vector addition. Assume A,...
For this assignment, write a parallel program in C++ using OpenMP for vector addition. Assume A, B, C are three vectors of equal length. The program will add the corresponding elements of vectors A and B and will store the sum in the corresponding elements in vector C (in other words C[i] = A[i] + B[i]). Every thread should execute an approximately equal number of loop iterations. As an input vector A, initialize its size to 10,000 and elements from...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT