Does anyone have some frame work of how i would write a method that added numbers together in an array using recursion?
thanks
Printable View
Does anyone have some frame work of how i would write a method that added numbers together in an array using recursion?
thanks
Why do you want to use recursion for that? :confused:
A loop would probably be much more appropriate.
The above code should do it but si is right - a loop would be much easier.Code:public static void addElements ( int[] Array, int pointer, int addition ) {
if ( pointer == Array.length ) return addition;
else {
addition += Array[pointer];
pointer ++;
//recursion occurs here
addElements ( Array, pointer, addition );
}
return addition;
}
Hope this helps!