74.3k views
1 vote
C++

Complete ComputeSavings()'s recursive case:
If month is odd, call ComputeSavings() to compute the next month's savings as the current month's savings plus 18.
Otherwise, call ComputeSavings() to compute the next month's savings as the current month's savings plus 6.
Ex: If the input is 5 143, then the output is:
Month: 5, savings: 191
#include
using namespace std;
void ComputeSavings(int totalMonth, int month, int savings) {
if (month == totalMonth) {
cout << "Month: " << month << ", savings: " << savings << endl;
}
else {
/* Your code goes here */
}
}
int main() {
int totalMonth;
int savings;
cin >> totalMonth;
cin >> savings;
ComputeSavings(totalMonth, 1, savings);
return 0;
}

1 Answer

4 votes

Final answer:

To compute the next month's savings based on whether the month is odd or even in the ComputeSavings() function, you need to add an if statement inside the else block. If the month is odd, add 18 to the current month's savings. If the month is even, add 6 to the current month's savings.

Step-by-step explanation:

To complete the recursive case of the ComputeSavings() function, we need to add an if statement inside the else block. If the month is odd, we call ComputeSavings() recursively and compute the next month's savings as the current month's savings plus 18. If the month is even, we call ComputeSavings() recursively and compute the next month's savings as the current month's savings plus 6.

else {
if (month % 2 == 1) {
ComputeSavings(totalMonth, month + 1, savings + 18);
}
else {
ComputeSavings(totalMonth, month + 1, savings + 6);
}
}

User Naveen Kumar V
by
8.1k points

No related questions found