Advertisement
Guest User

for brainzzz

a guest
Nov 20th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.64 KB | None | 0 0
  1. // Kitt Rush
  2. // COP2000.001
  3. // Project 7; Averaging temperatures, and displaying amount of averages, amount above and below, and the average itself
  4.  
  5. #include<iostream>
  6. #include<fstream>
  7. using namespace std;
  8.  
  9. // TO DO (according to rubrics yay!)
  10. // Class defined with private vars and public functions
  11. // input file is NovTemps.txt and located properly
  12. // Use an array of counters to count number of days above average, below and at average
  13. // Use a constructor to initialize array elements to 0
  14.  
  15. // Read from file
  16. // Average values
  17. //  Compare NEW num to Avg
  18. //      Keep track of how many are the same as avg, above, and below
  19.  
  20. class Average
  21. {
  22. private:        // Variables go here
  23.     ifstream temps;
  24.  
  25.     // Array; must be able to hold 30
  26.     static const int SIZE = 30;
  27.     int num[SIZE];          // Stores all the values from file
  28.  
  29.     // Integers regarding above, below and average counts
  30.     int aa = 0;                 // Above Average COUNT
  31.     int below = 0;              // Below Average COUNT
  32.     int avgCount = 0;           // Average COUNT
  33.  
  34.     float temp;         // Temporarily read in number
  35.     float avg;              // Calculated average
  36.     float sum;              // Calculate value of all temps
  37.     int count;              // Keeps track of how many values are read from file
  38.  
  39. public:         // Functions go here
  40.     // Methods
  41.     void openFile();        // Bunch of functions/methods that all relate to the file
  42.     void testFile();
  43.     void readFile();
  44.     void closeFile();
  45.  
  46.     void calcAvg();     // Will calculate the average of the temperatures
  47.     void driver();
  48.  
  49.     Average();              // Constructor
  50. };
  51. // ------------------------------------------- Method Definitions -------------------------------------------
  52.  
  53. // Average: Average
  54. // Initializing values
  55. Average::Average()
  56. {
  57.     // Hey what do you know, it's more initializing of the values! ;)
  58.     count = 0;
  59.     sum = 0;
  60.  
  61.     // Array initialization
  62.     for (int i = 0; i < SIZE; i++)
  63.     {
  64.         num[i] = 0;
  65.     }
  66. }
  67.  
  68. // Average: openFile
  69. // Open inFile for processing
  70. void Average::openFile()
  71. {
  72.     temps.open("NovTemps.txt");
  73.    
  74.     // Checking to make sure inFile opened properly
  75.     testFile();
  76. }
  77.  
  78. // Average: testFile
  79. // Test if input file opened properly
  80. void Average::testFile()
  81. {
  82.     if (!temps)
  83.     {
  84.         cerr << "NovTemps.txt didn't open properly." << endl << endl;
  85.         exit(2222);
  86.     }
  87. }
  88.  
  89. // Average: readFile
  90. // Read from inFile to populate array
  91. // Add to sum and count
  92. void Average::readFile()
  93. {
  94.     for (int i = 0; i < SIZE; i++)
  95.     {
  96.         if (i < SIZE)
  97.         {
  98.             temps >> temp;
  99.  
  100.             // Adding to sum
  101.             sum += temp;
  102.  
  103.             count++;
  104.         }
  105.     }
  106.     calcAvg();
  107. }
  108.  
  109. // Average: closeFile
  110. // Closes file after reading in everything
  111. void Average::closeFile()
  112. {
  113.     temps.close();
  114. }
  115.  
  116. // Average: calcAvg
  117. // Calculates average of the values from inFile
  118. void Average::calcAvg()
  119. {
  120.     avg = sum / count;
  121.  
  122.     for (int i = 0; i < SIZE; i++)
  123.         if (i < SIZE)
  124.         {
  125.             {
  126.                 if (temp > avg)
  127.                 {
  128.                     aa++;
  129.                 }
  130.  
  131.                 else if (temp < avg)
  132.                 {
  133.                     below++;
  134.                 }
  135.  
  136.                 else
  137.                 {
  138.                     avgCount++;
  139.                 }
  140.             }
  141.         }
  142.  
  143.     cout << "There are " << count << " numbers.\n";
  144.     cout << "The average temperature is " << avg << ".\n\n";
  145.     cout << "Out of " << count << ", there are " << aa << " above average temperatures, and " << below << " below average temperatures.\n";
  146.     cout << "There are " << avgCount << " temperatures at the average, " << avg << ", temperature.\n\n";
  147. }
  148.  
  149. // Average: driver
  150. // Basically the ordering of functions called and when they execute
  151. void Average::driver()
  152. {
  153.     openFile();
  154.     readFile();
  155.  
  156.     closeFile();
  157. }
  158.  
  159. // ------------------------------------------------- Driver -------------------------------------------------
  160.  
  161. int main()
  162. {
  163.     Average averageObj;
  164.  
  165.     averageObj.driver();
  166.  
  167.     system("pause");
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement