The required task is to calculate the number of consecutive days one can camp if camping starts on Monday. Camping can be done on a sunny(1) or cloudy(2) day, but not on a rainy(3) day. Also, it shouldn't too cold or too hot. The 3 conditions for a day to be suitable for camping are:
Temperature is at least 24.
Temperature is at most 30.
Weather is either sunny or cloudy.
Here is my code (due to the requirement, the main function cannot be modified and the two other functions should exist)
bool isSuitableForCamping(int minTemp, int maxTemp, int weather)
{
if (minTemp >= 24 && maxTemp <= 30 && (weather == 1 || weather == 2))
{return true;}
else
{return false;}
}
int getMaxCampingDaysIfStartOnMonday(
int mondayMinTemp, int mondayMaxTemp, int mondayWeather,
int tuesdayMinTemp, int tuesdayMaxTemp, int tuesdayWeather,
int wednesdayMinTemp, int wednesdayMaxTemp, int wednesdayWeather,
int thursdayMinTemp, int thursdayMaxTemp, int thursdayWeather,
int fridayMinTemp, int fridayMaxTemp, int fridayWeather,
int saturdayMinTemp, int saturdayMaxTemp, int saturdayWeather,
int sundayMinTemp, int sundayMaxTemp, int sundayWeather
)
{
int dayCount = 0;
bool dayList[7];
dayList[0] = isSuitableForCamping(mondayMinTemp, mondayMaxTemp, mondayWeather);
dayList[2] = isSuitableForCamping(tuesdayMinTemp, tuesdayMaxTemp, tuesdayWeather);
dayList[3] = isSuitableForCamping(wednesdayMinTemp, wednesdayMaxTemp, wednesdayWeather);
dayList[4] = isSuitableForCamping(thursdayMinTemp, thursdayMaxTemp, thursdayWeather);
dayList[5] = isSuitableForCamping(fridayMinTemp, fridayMaxTemp,fridayWeather);
dayList[6] = isSuitableForCamping(saturdayMinTemp, saturdayMaxTemp, saturdayWeather);
dayList[7] = isSuitableForCamping(sundayMinTemp, sundayMaxTemp, sundayWeather);
for (int i = 0; i < 7; i++)
{
if (dayList[i] == true )
{
dayCount++;
}
else
break;
}
return dayCount;
}
int main()
{
//Get Monday's weather forecast data
int mondayMinTemp, mondayMaxTemp, mondayWeather;
cout << "Monday's minimum temperature: ";
cin >> mondayMinTemp;
cout << "Monday's maximum temperature: ";
cin >> mondayMaxTemp;
cout << "Monday's weather (1:Sunny 2:Cloudy 3:Rainy): ";
cin >> mondayWeather;
cout << endl;
.
.
.
//Get Sunday's weather forecast data
int sundayMinTemp, sundayMaxTemp, sundayWeather;
cout << "Sunday's minimum temperature: ";
cin >> sundayMinTemp;
cout << "Sunday's maximum temperature: ";
cin >> sundayMaxTemp;
cout << "Sunday's weather (1:Sunny 2:Cloudy 3:Rainy): ";
cin >> sundayWeather;
cout << endl;
cout << "If you start camping on Monday, you would be able to camp for "
<< getMaxCampingDaysIfStartOnMonday( mondayMinTemp, mondayMaxTemp, mondayWeather,
tuesdayMinTemp, tuesdayMaxTemp, tuesdayWeather,
wednesdayMinTemp, wednesdayMaxTemp, wednesdayWeather,
thursdayMinTemp, thursdayMaxTemp, thursdayWeather,
fridayMinTemp, fridayMaxTemp, fridayWeather,
saturdayMinTemp, saturdayMaxTemp, saturdayWeather,
sundayMinTemp, sundayMaxTemp, sundayWeather )
<< " day(s) at most!" << endl;
return 0;
}
I tried to input the values so that the first three days are supposed to be available for camping. Yet it always returned me 1 day(s) at most. Is it possible to count the no. of trues in an array in this way?