In: Computer Science
c++
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.1C, Day 1/12/2018
Average temperature = 7.7 C
If you have any doubts, please give me comment...
#include<iostream>
#include<iomanip>
using namespace std;
class Weather{
public:
int day;
int month;
int year;
float temperature;
static float total;
Weather(){
cout<<"Enter day, month, year, temperature => ";
cin>>day>>month>>year>>temperature;
total += temperature;
}
static void displayLowestTemperature(Weather *w, int n){
int lowest = 0;
for(int i=0; i<n; i++){
if(w[lowest].temperature>w[i].temperature){
lowest = i;
}
}
cout<<"Lowest temperature = "<<w[lowest].temperature<<"C, Day "<<w[lowest].day<<"/"<<w[lowest].month<<"/"<<w[lowest].year<<endl;
}
static void displayAverageTemperature(int n){
double avg = total/n;
cout<<"Average temperature = "<<avg<<" C"<<endl;
}
};
float Weather::total = 0;
int main(){
int n;
cout<<"How many days => ";
cin>>n;
Weather *w = new Weather[n];
cout<<setprecision(1)<<fixed;
Weather::displayLowestTemperature(w, n);
Weather::displayAverageTemperature(n);
return 0;
}