C Practive question Thank you for your help The following co
C++ Practive question. Thank you for your help!
The following code defines a class called WeatherForecaster and a struct called ForecastDay. Write the method to find the maximum high temperature stored in the ForecastDay array. The method should print the day and the temperature. If more than one day has the maximum high temperature, print the first day found with that temperature.
struct ForecastDay{
std::string day;
int highTemp;
int lowTemp;
};
class WeatherForecaster
{
public:
WeatherForecaster( );
~WeatherForecaster( );
void addDayToData(ForecastDay);
void printDaysInData( ); //prints the unique dates in the data
void maxHighTemp( );
private:
int arrayLength;
int index;
ForecastDay yearData[984]; //data for each day
};
WeatherForecaster::WeatherForecaster()
{
//ctor
arrayLength = 100;
index = 0;
}
void WeatherForecaster::addDayToData(ForecastDay fd){
yearData[index] = fd;
index++;
}
//Your code goes here.
Answer:
Solution
#include <iostream>
using namespace std;
struct ForecastDay
{
std::string day;
int highTemp;
int lowTemp;
};
class WeatherForecaster
{
public:
WeatherForecaster( );
~WeatherForecaster( );
void addDayToData(ForecastDay);
void printDaysInData( ); //prints the unique dates in the data
void maxHighTemp( );
private:
int arrayLength;
int index;
ForecastDay yearData[984]; //data for each day
};
WeatherForecaster::WeatherForecaster()
{
//ctor
arrayLength = 100;
index = 0;
}
WeatherForecaster::~WeatherForecaster()
{
//ctor
arrayLength = 0;
index = 0;
}
void WeatherForecaster::addDayToData(ForecastDay fd)
{
yearData[index] = fd;
index++;
}
void WeatherForecaster::maxHighTemp()
{
ForecastDay maxForeCast=yearData[0] ;
for(int i=1; i<index; i++)
{
if(maxForeCast.highTemp<yearData[i].highTemp)
maxForeCast=yearData[i] ;
}
cout<<\"Max Day :\"<<maxForeCast.day<<\" Temperature :\"<<maxForeCast.highTemp<<endl;
}
int main()
{
ForecastDay day1= {\"Tuesday\",54,11};
ForecastDay day2= {\"MONDAY\",47,18};
WeatherForecaster weatherforeCast;
weatherforeCast.addDayToData(day1);
weatherforeCast.addDayToData(day2);
weatherforeCast.maxHighTemp();
return 0;
}
OUTPUT:
Max Day :Tuesday Temperature :54


