#include using namespace std; enum Months {JANUARY, FEBURARY, MARCH, APRIL, MAY}
ID: 3529223 • Letter: #
Question
#include using namespace std; enum Months {JANUARY, FEBURARY, MARCH, APRIL, MAY}; struct Weather { double totalRainfall; double HighTemp; double LowTemp; double AvgTemp; }; const int NUM_OF_MONTHS = 5; Weather arrayStruct[NUM_OF_MONTHS] = { { 10.5, 75.8, 28.6, 0.0}, { 13.4,88.1,37.3, 0.0}, { 11.4,76.2,32.5, 0.0}, { 19.2,73.7,35.7, 0.0}, { 15.3,76.7,40.5, 0.0} }; void displayMonthName(Months); void main() { Weather weather[NUM_OF_MONTHS]; Months month; double TotalofRainfall =0; double HighTp = 0; double LowTp = 0; double monthlyAverageTemp = 0; for(month = JANUARY; month <= MAY; month = static_cast(month + 1)) { cout << " Enter total rainfall for " ; displayMonthName(month); cout << ": "; cin >> weather[month].totalRainfall; cout << " Enter high temperature in this month:"; cin >> weather[month].HighTemp; while (weather[month].LowTemp < -100 || weather[month].LowTemp > 140) { cout << " Enter Low temperature in this month: "; cin >> weather[month].LowTemp; cout << " Enter the Average Temp of the month "; cin >> weather[month].AvgTemp; } cout << endl; TotalofRainfall = TotalofRainfall+ weather[month].totalRainfall; if (HighTp < weather[month].HighTemp) HighTp = weather[month].HighTemp; if (LowTp > weather[month].LowTemp) LowTp = weather[month].LowTemp; // Calculation monthlyAverageTemp = monthlyAverageTemp + (weather[month].HighTemp + weather[month].LowTemp)/2; } // Cal double AvgofRainfall = TotalofRainfall/ NUM_OF_MONTHS; // Cal double avgMonthlyAvgTemp = monthlyAverageTemp / NUM_OF_MONTHS; // Display Rainfall and Temp cout << " The average monthly rainfall: " << AvgofRainfall << endl; cout << " the total rainfall for the year : " << TotalofRainfall << endl; cout << " The highest temperature of the year :" << HighTp << endl; cout << " The lowest temperature of the year :" << LowTp << endl; cout << " The average of all the month temperature of the year :" << avgMonthlyAvgTemp << endl; system ("pause"); } // Function void displayMonthName(Months m) { switch(m) { case JANUARY: cout << " January"; break; case FEBURARY: cout << "Feburary"; break; case MARCH: cout << "March "; break; case APRIL: cout << " April"; break; case MAY: cout << " May"; break; } } How can i work out with the data of Weather arrayStruct[NUM_OF_MONTHS] = { { 10.5, 75.8, 28.6, 0.0}, { 13.4,88.1,37.3, 0.0}, { 11.4,76.2,32.5, 0.0}, { 19.2,73.7,35.7, 0.0}, { 15.3,76.7,40.5, 0.0} };Explanation / Answer
#include<iostream>
#include <cstdlib>
using namespace std;
enum Months {JANUARY, FEBURARY, MARCH, APRIL, MAY};
struct Weather
{
double totalRainfall;
double HighTemp;
double LowTemp;
double AvgTemp;
};
const int NUM_OF_MONTHS = 5;
Weather weather[NUM_OF_MONTHS] = { { 10.5, 75.8, 28.6, 0.0}, { 13.4,88.1,37.3, 0.0},
{ 11.4,76.2,32.5, 0.0}, { 19.2,73.7,35.7, 0.0},
{ 15.3,76.7,40.5, 0.0}
};
void displayMonthName(Months);
int main()
{
Months month;
double TotalofRainfall =0;
double HighTp = 0;
double LowTp = 0;
double monthlyAverageTemp = 0;
for(month = JANUARY; month <= MAY; month = static_cast<Months>(month + 1))
{
TotalofRainfall = TotalofRainfall+ weather[month].totalRainfall;
if (HighTp < weather[month].HighTemp)
HighTp = weather[month].HighTemp;
if (LowTp > weather[month].LowTemp)
LowTp = weather[month].LowTemp;
/* Calculation*/
monthlyAverageTemp = monthlyAverageTemp + (weather[month].HighTemp + weather[month].LowTemp)/2; }
/* Cal*/
double AvgofRainfall = TotalofRainfall/ NUM_OF_MONTHS;
/* Cal*/
double avgMonthlyAvgTemp = monthlyAverageTemp / NUM_OF_MONTHS;
/* Display Rainfall and Temp*/
cout << " The average monthly rainfall: " << AvgofRainfall << endl;
cout << " the total rainfall for the year : " << TotalofRainfall << endl;
cout << " The highest temperature of the year :" << HighTp << endl;
cout << " The lowest temperature of the year :" << LowTp << endl;
cout << " The average of all the month temperature of the year :" << avgMonthlyAvgTemp << endl;
system ("pause");
return 0;
}
/* Function*/
void displayMonthName(Months m)
{
switch(m)
{
case JANUARY: cout << " January"; break;
case FEBURARY: cout << "Feburary"; break;
case MARCH: cout << "March "; break;
case APRIL: cout << " April"; break;
case MAY: cout << " May"; break;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.