Results 1 to 3 of 3

Thread: recursion

  1. #1

    Thread Starter
    Hyperactive Member MPrestonf12's Avatar
    Join Date
    Jun 1999
    Location
    NY
    Posts
    330

    Question recursion

    Does anyone have some frame work of how i would write a method that added numbers together in an array using recursion?
    thanks
    Matt

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    Why do you want to use recursion for that?

    A loop would probably be much more appropriate.

  3. #3
    Addicted Member MethadoneBoy's Avatar
    Join Date
    Oct 2001
    Location
    Preferably somewhere between Keira Knightley and Diane Kruger but I'm not fussy
    Posts
    180
    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;
    
    }
    The above code should do it but si is right - a loop would be much easier.

    Hope this helps!
    "'Oh, hello Mr. Crick! What do you think of Jeffrey Archer?' Clip-clip-clip! Oh, come on! Who are you kidding? You wait til I'm mayor, you'll see how tough I am! Christ almighty...."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width