1

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?

2 Answers 2

1

You need to change your for cycle so it doesn't break after the first unsuitable day.

//this is the amount of consecutive days that we are currently counting
int currentConsecutiveDays = 0;
//this is the maximum amount of consecutive days in the whole week
int maxConsecutiveDays = 0;
for (int i = 0; i < 7; i++)
{
    if (dayList[i] == true )
    {
        currentConsecutiveDays++;
    }
    //we need the second condition for the case when
    //the consecutive days end up on Sunday.
    else if(dayList[i] == false || i == 6)
    {
        if(currentConsecutiveDays > maxConsecutiveDays)
            maxConsecutiveDays = currentConsecutiveDays;
        currentConsecutiveDays = 0;
    }
}

Also you've skipped the index 1 in the dayList. If an array has N elements, the indexes of that array should be 0, 1, ..., N - 1

Sign up to request clarification or add additional context in comments.

2 Comments

As it is asked to have consecutive days, the break is required.
@Jarod, ahh, I missed that part of the question. Will edit the answer, thanks
1

There are several problems with the code, one being that your array indexing is off here:

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);

You skipped element 1 and then overran the end of the array. It should be:

dayList[0] = isSuitableForCamping(mondayMinTemp, mondayMaxTemp, mondayWeather);
dayList[1] = isSuitableForCamping(tuesdayMinTemp, tuesdayMaxTemp, tuesdayWeather);
dayList[2] = isSuitableForCamping(wednesdayMinTemp, wednesdayMaxTemp, wednesdayWeather);
dayList[3] = isSuitableForCamping(thursdayMinTemp, thursdayMaxTemp, thursdayWeather);
dayList[4] = isSuitableForCamping(fridayMinTemp, fridayMaxTemp,fridayWeather);
dayList[5] = isSuitableForCamping(saturdayMinTemp, saturdayMaxTemp, saturdayWeather);
dayList[6] = isSuitableForCamping(sundayMinTemp, sundayMaxTemp, sundayWeather);

One suggestion:

  • learn how to use your debugger - stepping through the code and inspecting the variables would have helped to identify your bugs very quickly

2 Comments

For the first tips, OP uses array contrary to fixed provided code.
@Jarod42: thanks for pointing that out - what an atrocious homework assignment this is!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.