In: Computer Science
C++ Assignment.
Design the Weather class that contains the following members:
Data members to store:
- a day (an integer)
- a month (an integer)
- a year (an integer)
- a temperature (a float)
- a static data member that stores the total of all temperatures (a float)
Member functions:
- a constructor function that obtains a day, month, year and temperature from the user. This function should also accumulate/calculate the total of all temperatures (i.e., add the newly entered temperature to the total).
- a static function that computes and displays the day which has the lowest temperature. Note: An array of Weather objects and the size of the array will be passed to this function.
- a static function that computes and displays the average temperature. This function should use a parameter, if necessary.
Design the main( ) function, which instantiates/creates any number of objects of the Weather class as requested by the user (i.e., creates a dynamic array of Weather objects). main( ) should also call appropriate functions to compute and display the day that has the lowest temperature, as well as the average temperature. Your program should include all necessary error checking.
A sample run of this program could be as follows:
How many days => 4
Enter day, month, year, temperature => 29 11 2018 15.6
Enter day, month, year, temperature => 30 11 2018 8.7
Enter day, month, year, temperature => 1 12 2018 3.1
Enter day, month, year, temperature => 2 12 2018 3.5
Lowest temperature = 3.1 C, Day 1/12/2018
Average temperature = 7.7 C
#include <iostream>
#include <iomanip>
#include <vector>
#include <sstream>
using namespace std;
class Weather
{
private:
int day, month, year;
float temperature;
static float totalTemperature;
public:
Weather()
{
int d, m, y;
float temp;
cout << "Enter day, month, year, temperature => ";
cin >> d >> m >> y >> temp;
this->day = d;
this->month = m;
this->year = y;
this->temperature = temp;
totalTemperature += this->temperature;
}
int getDay(){ return this->day; }
int getMonth(){ return this->month; }
int getYear(){ return this->year; }
float getTemperature(){ return this->temperature; }
float getTotalTemperature(){ return totalTemperature; }
string toString()
{
stringstream ss;
ss << this->temperature << " C, Day " << this->day << "/" << this->month << "/" << this->year;
return ss.str();
}
};
float Weather::totalTemperature = 0;
// function prototype
static void findDayWithLowestTemperature(vector<Weather> weathers, int nDays);
static float getAverageTemperature(vector<Weather> weathers, int size);
int main()
{
vector<Weather> weathers;
int nDays;
cout << endl << "How many days => ";
cin >> nDays;
for(int i = 0; i < nDays; i++)
{
Weather w;
weathers.push_back(w);
}
findDayWithLowestTemperature(weathers, nDays);
cout << setprecision(1) << fixed << "Average temperature = " << getAverageTemperature(weathers, nDays) << " C" << endl;
return 0;
}
static void findDayWithLowestTemperature(vector<Weather> weathers, int size)
{
float min = weathers[0].getTemperature();
int minIndex = -1;
for(int i = 0; i < size; i++)
{
if(weathers[i].getTemperature() < min)
{
min = weathers[i].getTemperature();
minIndex = i;
}
}
cout << "Lowest temperature = " << weathers[minIndex].toString() << endl;
}
static float getAverageTemperature(vector<Weather> weathers, int size)
{
float sum = 0.0;
sum += weathers[0].getTotalTemperature();
return (sum / size);
}
**************************************************************** SCREENSHOT ***********************************************************