Please help with writing the following code. I’m using C++ and vim. Include plenty of comments. Thanks!
Don’t use any loops when writing your recursive functions. If you do, there’s a very good chance your function won’t be truly recursive. Also don’t use any static variables. You are free to use helper methods on either of the projects. Project 9.a Write a recursive function named summer 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.
Expert Answer
C++ CODE |
#include<iostream>
using namespace std; /* * Summer function */ double summer(double arr[], int size) { if(size <= 0) return 0; // recursively calling the summer function with decrementing the size by 1 and adding the lst element return arr[size-1] + summer(arr, size-1); } int main() { double array[] = {10, 90, 80, 50, 50, 40, 20, 10}; // defining the array summer(array, 10); double sum = summer(array, 10); // summing for whole length of array cout<<“Sum is: “<<sum; sum = summer(array, 2); // summing for first 2 elements cout<<“nSum is: “<<sum; return 0; } |
OUTPUT