Don\'t try to use any loops when writing a recursive function. If you do, there\
ID: 3759945 • Letter: D
Question
Don't try to use any loops when writing a recursive function. If you do, there's a very good chance your function won't be truly recursive.
Write a recursive function named "winter" that takes two parameters - an array of doubles and the size of the array - and returns the sum of the values in the array. The size parameter does not have to be the actual size of the array. It will be at the top level, but at the lower recursive levels it can be the size of the sub-array being worked on at that level.
The file must be called: winter.cpp.
Explanation / Answer
double winter(dounle xList[], double x, int lengthOfArray)
{
if (lengthOfArray == 0) {
// base case
return 0;
} else {
int value = xList[lengthOfArray-1];
if (value < x) {
// process the rest of the array and add our result
return value + winter(xList, x, lengthOfArray - 1);
} else {
// process the rest of the array
return winter(xList, x, lengthOfArray - 1);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.