I am very confused about the parameters to create arrays, pointers and recursive functions but I have to create the following program in c++: Design a function named sum that, given an integer "a" pointer that points to the first position of an array of numbers integers and an integer "n" with the length of the given array, return the sum of all its values using a recursive function.
Next, design a program that reads an integer n, which represents the number of values to be read next, and then reads n values to be stored in an array. Finally, it shows on the screen the result of the sum of all the values of the array.
Input Example 1
0
Output Example 1
0
Input Example 2
4 1 2 3 4
Output Example 2
10
The basic structure of the program is this:
#include <iostream>
#include <string>
using namespace std;
int sum(int a, int n);
int main(){
int n=0;
cin >> n;
}
int sum(int a, int n){
}
Recursion can be very complicated at first, but practice and consistency in programming will make it look easier every time.
In my opinion I could say that recursion has two important parts.
The base case: Which is usually a conditional(
if
) that indicates/returns the final value of our recursive function. In your case for example you have input data where0
is a final value of your recursion.The body or the recursion: This is perhaps the most complicated part, since this is where we must make the call to our function, implementing the logic to achieve our objective. The call to said function from the same function can be 1,2,3... and as many times as your problem requires (sometime I used 8 in the ACM).
The solution to your problem
Now let's analyze:
Case 1
n=0
a=[]
call to function
sum(a,n)
=>sum([],0)
Explanation:
n
it is equal to 0 it enters the conditional (base case) if and returns0
Conclusion: it works correctly for case 1
Case 2 n=4
a=[2,4,1,3] //I change it only for the example
1st function call
sum(a,n)
=>sum([2,4,1,3],4)
Explanation: n=4 enters the body
returns sum(a,n-1)+a[ n-1] which is equal to: sum([2,4,1,3],3)+a[3]
which is equal to: sum([2,4,1,3],3)+ 3 / /here another call to the same function is made
2nd function call
sum(a,n)
=>sum([1,2,3,4],3)
Explanation: n=3 enters the body
returns sum(a,n-1)+a[n-1] which is equal to: sum([2,4,1,3],2)+ a[2]
which is equal to: sum([2,4,1,3],2)+ 1 //here another call to the same function is made
3rd function call
sum(a,n)
=>sum([1,2,3,4],2)
Explanation: n=2 enters the body
returns sum(a,n-1)+a[n-1] which is equal to: sum([2,4,1,3],1)+ a[1]
which is equal to: sum([2,4,1,3],1)+ 4 //here another call to the same function is made
4th function call
sum(a,n)
=>sum([1,2,3,4],1)
Explanation: n=1 enters the body
returns sum(a,n-1)+a[n-1] which is equal to: sum([2,4,1,3],0)+ a[0]
which is equal to: sum([2,4,1,3],0)+ 2 //here another call to the same function is made
5th function call
sum(a,n)
=>sum([1,2,3,4],0)
Explanation: n=0 enters the base case
returns 0; // end recursion
Try to make it as understandable as possible, I hope it helps you.