Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Kitt Rush
- // COP2000.001
- // Project 7; Averaging temperatures, and displaying amount of averages, amount above and below, and the average itself
- #include<iostream>
- #include<fstream>
- using namespace std;
- // TO DO (according to rubrics yay!)
- // Class defined with private vars and public functions
- // input file is NovTemps.txt and located properly
- // Use an array of counters to count number of days above average, below and at average
- // Use a constructor to initialize array elements to 0
- // Read from file
- // Average values
- // Compare NEW num to Avg
- // Keep track of how many are the same as avg, above, and below
- class Average
- {
- private: // Variables go here
- ifstream temps;
- // Array; must be able to hold 30
- static const int SIZE = 30;
- int num[SIZE]; // Stores all the values from file
- // Integers regarding above, below and average counts
- int aa = 0; // Above Average COUNT
- int below = 0; // Below Average COUNT
- int avgCount = 0; // Average COUNT
- float temp; // Temporarily read in number
- float avg; // Calculated average
- float sum; // Calculate value of all temps
- int count; // Keeps track of how many values are read from file
- public: // Functions go here
- // Methods
- void openFile(); // Bunch of functions/methods that all relate to the file
- void testFile();
- void readFile();
- void closeFile();
- void calcAvg(); // Will calculate the average of the temperatures
- void driver();
- Average(); // Constructor
- };
- // ------------------------------------------- Method Definitions -------------------------------------------
- // Average: Average
- // Initializing values
- Average::Average()
- {
- // Hey what do you know, it's more initializing of the values! ;)
- count = 0;
- sum = 0;
- // Array initialization
- for (int i = 0; i < SIZE; i++)
- {
- num[i] = 0;
- }
- }
- // Average: openFile
- // Open inFile for processing
- void Average::openFile()
- {
- temps.open("NovTemps.txt");
- // Checking to make sure inFile opened properly
- testFile();
- }
- // Average: testFile
- // Test if input file opened properly
- void Average::testFile()
- {
- if (!temps)
- {
- cerr << "NovTemps.txt didn't open properly." << endl << endl;
- exit(2222);
- }
- }
- // Average: readFile
- // Read from inFile to populate array
- // Add to sum and count
- void Average::readFile()
- {
- for (int i = 0; i < SIZE; i++)
- {
- if (i < SIZE)
- {
- temps >> temp;
- // Adding to sum
- sum += temp;
- count++;
- }
- }
- calcAvg();
- }
- // Average: closeFile
- // Closes file after reading in everything
- void Average::closeFile()
- {
- temps.close();
- }
- // Average: calcAvg
- // Calculates average of the values from inFile
- void Average::calcAvg()
- {
- avg = sum / count;
- for (int i = 0; i < SIZE; i++)
- if (i < SIZE)
- {
- {
- if (temp > avg)
- {
- aa++;
- }
- else if (temp < avg)
- {
- below++;
- }
- else
- {
- avgCount++;
- }
- }
- }
- cout << "There are " << count << " numbers.\n";
- cout << "The average temperature is " << avg << ".\n\n";
- cout << "Out of " << count << ", there are " << aa << " above average temperatures, and " << below << " below average temperatures.\n";
- cout << "There are " << avgCount << " temperatures at the average, " << avg << ", temperature.\n\n";
- }
- // Average: driver
- // Basically the ordering of functions called and when they execute
- void Average::driver()
- {
- openFile();
- readFile();
- closeFile();
- }
- // ------------------------------------------------- Driver -------------------------------------------------
- int main()
- {
- Average averageObj;
- averageObj.driver();
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement