PDA

Click to See Complete Forum and Search --> : recursion


MPrestonf12
May 5th, 2004, 08:56 AM
Does anyone have some frame work of how i would write a method that added numbers together in an array using recursion?
thanks

si_the_geek
May 5th, 2004, 09:13 AM
Why do you want to use recursion for that? :confused:

A loop would probably be much more appropriate.

MethadoneBoy
May 5th, 2004, 11:51 AM
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;

}



The above code should do it but si is right - a loop would be much easier.

Hope this helps!