Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Program in C++ In this lab, we will practice functional programming: Let\'s assu

ID: 652933 • Letter: P

Question

Program in C++

In this lab, we will practice functional programming:

Let's assume we have the following simple class called "Student":

class Student{
public:
   std::string name;
   int age;
   Student(std::string n, int a){
       name = n;
       age = a;       
   }
};

Write a function object class (or lambda function) that keeps a local sum variable and adds any value passed to the () operator. Given a vector of students and using the  for_each function (from the algorithm header, see this link for more details: http://en.cppreference.com/w/cpp/algorithm/for_each), you will compute the sum of the students' ages and then output the average age of the students. Name the function object class Sum. So it will look like the following:

class Sum

{
public:

int sum;
void operator () (Student* s){
// do something here to sum up the ages
   }
};


Furthermore, you will need to write another function object class that checks if a student is older than 30 years old. You will then use the find_if function (from the algorithm header, see this link for more details: http://www.cplusplus.com/reference/algorithm/find_if/), to find the first student that is older than 30. In addition, if there is no such student in the input, you should also indicate it. Since we are doing C++ functional programming, please create a class named "OlderThan30" to implement this functor.

You can either figure out a way to take in student values from input, or you can just hard-code the student values into your code. For example, you can use this code to set up your student vector:

    std::vector<Student *> v;
   Student * s1 = new Student("Luis", 24);
   v.push_back(s1);
   Student * s2 = new Student("Bob", 21);
   v.push_back(s2);
   Student * s3 = new Student("Alice", 33);
   v.push_back(s3);
   Student * s4 = new Student("Jane", 25);
   v.push_back(s4);
   Student * s5 = new Student("Lauren", 23);
   v.push_back(s5);

Sample Run:
./lab11
The average age of the students is :25.2
The first 30+ year old we found in this list is Alice

Explanation / Answer

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main()
{
   int age(0), sex(0), youngest(999), oldest(0), M(0), F(0), age1(0), age2(0), age3(0), age4(0), age5(0), totalage(0), totalcount(0);
   cin >> age >> sex >> youngest >> oldest >> M >> F >> age1>> age2 >> age3 >> age4 >> age5 >> totalage >> totalcount;

   do
   {
       cout << "Enter sex " <<endl;
       cin >> sex;
           {
           if (sex == M || sex == F);
          
           else cout << "Invalid sex entered " << endl;
           break;
           }
       cout << "Enter age of attendee " <<endl;
       cin >> age;

           {if (age < 0)
               cout << "Invalid age " << endl;
           break;
           }
          

           if (age >=0 && age <= 18)
                   ++age1;
          

           if (age >= 19 && age <= 30)
                   ++age2;
          

           if (age >= 31 && age <= 40)
                   ++age3;
          

           if(age >=41 && age <=60)
                   ++age4;
          

           if (age >=61 && age <=100)
                   ++age5;
          
           if (age < youngest)
                   youngest += age;

           if (age > oldest)
                   oldest +=age;

       ++ totalcount;
       ++ totalage;
          

   } while (age > 0 );

   double sum = totalage;

   cout << "The average age was " << sum << endl;

   cout << "The youngest person was " << youngest << endl;

   cout << "The oldest person was " << oldest << endl;

   return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote