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

You will be using functions to get a list of integers from a user and then find

ID: 645785 • Letter: Y

Question

You will be using functions to get a list of integers from a user and then find statistics of the given list of integers. You are not allowed to use any built in functions for finding the calculations in this assignment.

The functions required for this assignment will be:

printMenu()-Prints a menu of options to the screen

getList()-Gets list of integers from user using a while loop and returns the list

getMean(userList)-Returns the average of the integers contained in the list given by the user

getMedian(userList)-Returns the median of the integers contained in the list given by the user

printGraph(userList)-Prints a horizontal histogram of the elements in the list

getMin(userList)-Finds the lowest value in the set using a for loop and then prints value to screen

getMax(userList)-Returns the highest value in the set using a for loop

Example output:

Explanation / Answer

#include <iostream>
#include <list>
#include <cmath>
#include <climits>

using namespace std;

void printMenu(){
   cout << "Please choose the statistic that you would like to calculate!" << endl;
   cout << "1. Mean" << endl;
   cout << "2. Median" << endl;
   cout << "3. Min" << endl;
   cout << "4. Max" << endl;
   cout << "5. Graph" << endl;
}

list<int> getList(){
   list<int> l;
   int val;
   cout << "To begin, enter a list of integers or to end the list!" << endl;
   while(true){
       cout << "Enter an integer: ";
       if(cin >> val){
           l.push_back(val);
       }
       else{
           cin.clear();
           cin.ignore(INT_MAX, ' ');
           break;
       }
   }
   return l;
}

double getMean(list<int> userlist){
   double sum = 0;
   list<int>::iterator itr;
   for(itr = userlist.begin(); itr != userlist.end(); ++itr){
       sum += *(itr);
   }
   return sum / userlist.size();
}

double getMedian(list<int> userlist){
   int size = userlist.size();
   list<int>::iterator itr;
   if(size % 2 == 1){
       int i = 0;
       for(itr = userlist.begin(); itr != userlist.end(); ++itr){
           if(i == userlist.size() / 2){
               return *itr;
           }
           ++i;
       }
   }
   else{
       double m;
       int i = 0;
       for(itr = userlist.begin(); itr != userlist.end(); ++itr){
           if(i == userlist.size() / 2 || i == (userlist.size() / 2) - 1){
               m += (*itr);
           }
           ++i;
       }
       return m;
   }
}

int getMin(list<int> userlist){
   return userlist.front();
}

int getMax(list<int> userlist){
   return userlist.back();
}

void printGraph(list<int> userlist){
   int lc = 0;
   int val = userlist.front();
   list<int>::iterator itr;
   for(itr = userlist.begin(); itr != userlist.end(); ++itr){
       if(*(itr) == val){
           lc++;
       }
       else{
           cout << val << ": ";
           for(int i = 0; i < lc; ++i){
               cout << "X";
           }
           cout << endl;
           val = *itr;
           lc = 1;
       }
   }
   cout << val << ": ";
   for(int i = 0; i < lc; ++i){
       cout << "X";
   }
   cout << endl;
}

int main(){
   cout << "Welcome to the List Statistics Calculator" << endl;
   int option;
   list<int> userlist = getList();
   userlist.sort();
   while(true){
       printMenu();
       cout << "Please enter your choice, or 0 to exit: ";
       cin >> option;
       switch(option){
       case 1:
           cout << "The mean of this data set is " << getMean(userlist) << endl;
           break;
       case 2:
           cout << "The median of this data set is " << getMedian(userlist) << endl;
           break;
       case 3:
           cout << "The max of this data set is " << getMax(userlist) << endl;
           break;
       case 4:
           cout << "The min of this data set is " << getMin(userlist) << endl;
           break;
       case 5:
           printGraph(userlist);
           break;
       case 0:
           return 0;
       default:
           cout << "Enter a valid option" << endl;  
       }
   }
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote