Binary Recursion Sum of an array C++ I have my array int oddarr[5] = { 3, 5, 7,
ID: 641350 • Letter: B
Question
Binary Recursion Sum of an array C++
I have my array
int oddarr[5] = { 3, 5, 7, 9, 11};
and im supposed to use a binary recursion function to sum them up. This is my function:
int recursiveBinarySum(int arr[], int start, int end){
if (end == 1)
return arr[start];
return recursiveBinarySum(arr, start, ceil(end / 2)) + recursiveBinarySum(arr, (start + ceil(end / 2)), floor(end / 2));
}
but when I call it recursiveBinarySum(oddarr, 0, 5) << std::endl; I do not get the correct answer. Instead of getting 35 i get 24. What's wong with this?
Explanation / Answer
int recursiveBinarySum(int arr[], int start, int end){
if (start >= end)
return arr[start];
return recursiveBinarySum(arr, start, start + ((end - start) / 2)) + recursiveBinarySum(arr, (start + ((end - start) / 2) + 1), end);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.