2015-09-29 135 views
1

所需的任務是計算週一露營開始時可以駐紮的連續天數。 露營可以在晴天(1)或多雲(2)天進行,但不能在下雨天(3天)進行。另外,它不應該太冷或太熱。的3個條件,每天以適合於野營是:將布爾函數傳遞給數組

  • 溫度爲至少24

  • 溫度爲至多30

  • 天氣或者是晴天或陰天。

這是我的代碼(由於要求,功能不能被修改並且兩個其他功能必須存在)

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

我試圖輸入的值,使得第一應該有三天的露營時間。然而它總是最多讓我返回1天。是否有可能數不?這樣的數組中的trues?

回答

1

有幾個問題的代碼,一個是你的數組索引是在這裏下車:

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

你跳過元素1,然後佔領了數組的結尾。它應該是:

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

一個建議:

  • 學習如何使用調試器 - 單步調試代碼和檢查變量將有助於查明您的問題時會非常迅速地
+0

對於第一個提示,OP使用的數組與* fixed *提供的代碼相反。 – Jarod42

+0

@ Jarod42:謝謝你指出 - 這是一個非常糟糕的家庭作業! –

1

您需要更改您的for週期,以便在第一個不適宜的一天之後不會中斷。

//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; 
    } 
} 

而且您在dayList跳過指數1。如果一個數組有N個元素,那麼該數組的索引應該是0, 1, ..., N - 1

+0

因爲要求有連續的日子,所以需要'bre​​ak'。 – Jarod42

+0

@Jarod,啊,我錯過了那部分問題。將編輯答案,謝謝 – SingerOfTheFall