Advertisement
SonicDesu

List

Sep 14th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.25 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <iomanip>
  4.  
  5. using namespace std;
  6.  
  7. int main() {
  8.     char choice{};
  9.     bool work{ true };
  10.     vector <int> list{};
  11.     double mean{};
  12.     int sum{};
  13.     int temporary_number_memory;
  14.     do {
  15.         cout << "P - print numbers" << endl;
  16.         cout << "A - add a number" << endl;
  17.         cout << "M - Display mean of the numbers" << endl;
  18.         cout << "S - Display the smallest number" << endl;
  19.         cout << "L - Display the largest number" << endl;
  20.         cout << "Q - Quit" << endl;
  21.         cout << endl << "Enter your choice: ";
  22.         cin >> choice;
  23.         switch (choice) {
  24.         case 'P':
  25.         case 'p':
  26.             if (list.size() > 0) {
  27.                 cout << "Your list:" << endl << "[ ";
  28.                 for (auto values : list) cout << values << " ";
  29.                 cout << "]" << endl;
  30.             }
  31.             else cout << endl << "[] - Your list is empty!" << endl;
  32.             break;
  33.         case 'A':
  34.         case 'a':
  35.             cout << "Enter an integer to add to the list: ";
  36.             cin >> temporary_number_memory;
  37.             list.push_back(temporary_number_memory);
  38.             cout << endl << "Number " << temporary_number_memory << " - Succesfully added to the list" << endl;
  39.             break;
  40.         case 'M':
  41.         case 'm':
  42.             for (auto values : list) sum += values;
  43.             mean = static_cast <double>(sum) / list.size();
  44.             cout << fixed << setprecision(2); // dwa miejsca po przecinku
  45.             cout << "Mean of the list is: " << mean << endl << endl;
  46.             break;
  47.         case 'S':
  48.         case 's':
  49.         {
  50.             int smallest_number{};
  51.             if (list.size() > 0) {
  52.                 smallest_number = list.at(0);
  53.                 for (auto values : list) {
  54.                     if (smallest_number > values) smallest_number = values;
  55.                 }
  56.                 cout << "The smallest number in the list is " << smallest_number << endl << endl;
  57.             }
  58.             else cout << "---------------------------------\nERROR!!!\nThere are no numbers in the list!!!\n";
  59.             break;
  60.         }
  61.         case 'L':
  62.         case 'l':
  63.         {
  64.             int largest_number{};
  65.             if (list.size() > 0) {
  66.                 largest_number = list.at(0);
  67.                 for (auto values : list) {
  68.                     if (largest_number < values) largest_number = values;
  69.                 }
  70.                 cout << "The largest number in the list is " << largest_number << endl << endl;
  71.             }
  72.             else cout << "---------------------------------\nERROR!!!\nThere are no numbers in the list!!!\n";
  73.             break;
  74.         }
  75.         case 'Q':
  76.         case 'q':
  77.             work = false;
  78.             break;
  79.         }
  80.     } while (work);
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement