SHARE
TWEET

practiceProgram

a guest Nov 13th, 2019 125 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iomanip>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. #include <vector>
  6.  
  7. using namespace std;
  8. void icountSumAvg(vector<double>, int, int &icount, double &sum, double &avg);
  9. void display(int, double, double);
  10. void display(int, double, double, vector<double>, int);
  11. void swap(vector<double>, int);
  12.  
  13. bool testSwap = true; // displays each element if true
  14.  
  15. int main()
  16. {
  17.     ifstream ifile;
  18.     const string FILENAME = "input.txt";
  19.     ifile.open(FILENAME);
  20.     int count = 0;
  21.     double sum = 0, avg = 0;
  22.     if (!(ifile.fail()))
  23.     {
  24.         vector<double> vect;
  25.         double value = 0.0;
  26.         while (!ifile.eof())
  27.         {
  28.             ifile >> value;
  29.             vect.push_back(value);
  30.         }
  31.         int vectSize = vect.size();
  32.  
  33.         icountSumAvg(vect, vectSize, count, sum, avg);
  34.  
  35.         if (testSwap = true)
  36.         {
  37.             swap(vect, vectSize);
  38.             display(count, sum, avg, vect, vectSize);
  39.         }
  40.         else
  41.             display(count, sum, avg);
  42.  
  43.         ifile.close();
  44.     }
  45.     else
  46.         cout << "There was an error opening the file: " << FILENAME << endl;
  47.     system("pause");
  48. }
  49.  
  50. void icountSumAvg(vector<double> values, int size, int &icount, double &sum, double &avg)
  51. {
  52.     for (int i = 0; i < size; i++)
  53.     {
  54.         icount++;
  55.         sum += values[i];
  56.     }
  57.     avg = sum / icount;
  58. }
  59.  
  60. void swap(vector<double> values, int size)
  61. {
  62.     double temp;
  63.     for (int i = 0; i < size; i++)
  64.     {
  65.         for (int j = 0; j < i; j++)
  66.         {
  67.             if (values[j] < values[i])
  68.             {
  69.                 temp = values[i];
  70.                 values[i] = values[j];
  71.                 values[j] = temp;
  72.             }
  73.         }
  74.     }
  75. }
  76.  
  77. void display(int count, double sum, double avg, vector<double> values, int size)
  78. {
  79.     for (int i = 0; i < size; i++)
  80.     {
  81.         cout << "Element " << i + 1 << ": " << values[i] << endl;
  82.     }
  83.     cout <<"Total count:        " << count << endl
  84.         << "Total sum:          " << sum << endl
  85.         << "Average of numbers: " << avg << endl;
  86. }
  87.  
  88. void display(int count, double sum, double avg)
  89. {
  90.     cout <<"Total count:        " << count << endl
  91.         << "Total sum:          " << sum << endl
  92.         << "Average of numbers: " << avg << endl;
  93. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top