0

Sum of integers of an array using Recursion method ??

i want some help to build a Recursive function which takes an input array then returns the sum of its elements (without using loops) EX: size =3 elements= 10, 20, 30 sum = 60

6th Mar 2017, 9:49 PM
Fake Mask
4 Respuestas
6th Mar 2017, 10:17 PM
Tashi N
Tashi N - avatar
+ 1
Think of this: The sum of the elements of an array is equal to the first (or last, or any) element plus the sum of the rest of elements.
7th Mar 2017, 5:55 AM
Álvaro
0
def sumRec(list): if list: return list[0] + sumRec(list[1:]) else: return 0
6th Mar 2017, 9:57 PM
Jehad Al-Ansari
Jehad Al-Ansari - avatar
0
//not in any language but easily translatable : int sum(int tab[n],int n,int id){ if(id==n){ return 0; } return tab[id]+sum(tab,n,id+1); }
6th Mar 2017, 10:38 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar