Results 1 to 2 of 2
  1. #1
    csanch11 is offline Member
    Join Date
    Oct 2013
    Posts
    63
    Rep Power
    0

    Default Problem Understanding RECURSIVE METHODS

    I'm trying to understand the concept behind this recursive method called rangeSum. This method sums a range of array elements with recursion. I tried summing the elements of 2 through 5, and I tried writing down what actually happens when java executes the program, but what I get when I try to figure it out by paper is different from what netbeans gives me. Here is a snapshot of my scratch work as well as my source code. Netbeans gives me "The sum of elements 2 through 5 is 18" when I try running it but it I get 12 when I do the recursion on paper. I know that the source code is correct because it's out of the book but what am I doing wrong when I try figuring it out by hand?

    XML Code:
    package recursivecall;
    
    import java.util.Scanner;
    
    /**
     * Author: <<Conrado Sanchez>> Date: Task:
     */
    public class RecursiveCall {
    
        public static void main(String[] args) {
    
            int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9};
            System.out.println("The sum of elements 2 through 5 is " + rangeSum(numbers, 2, 5));
    
        } // end of main()
    
        /**
         * The rangeSum method calculates the sum of a specified range of elements
         * in array.
         * @param start Specifies the starting element.
         * @para end Specifies the ending element.
         * @return The sum of the range.
         */
        private static int rangeSum(int[] array, int start, int end) {
            // base case 
            if (start > end) {
                return 0;
            } else {
                return array[start] + rangeSum(array, start + 1, end);
            }
        }
    
    } // end of RecursiveCall class
    Attached Thumbnails Attached Thumbnails Problem Understanding RECURSIVE METHODS-recursion.jpg  
    Last edited by csanch11; 10-03-2014 at 07:49 AM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Problem Understanding RECURSIVE METHODS

    That last bit is wrong, since 5 is not > 5.
    So it will return the value in array[6] and then go once more into rangeSum(6), so 6 > 5 and return 0.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. problems with boolean and recursive methods
    By mflb94 in forum New To Java
    Replies: 4
    Last Post: 09-27-2012, 11:40 AM
  2. Replies: 3
    Last Post: 05-12-2011, 06:07 AM
  3. understanding of a recursive method
    By Yakg in forum New To Java
    Replies: 3
    Last Post: 03-02-2011, 07:03 PM
  4. Accessor & Mutator methods understanding..
    By 6thDAY in forum New To Java
    Replies: 3
    Last Post: 08-14-2010, 08:27 PM
  5. Understanding this recursive function
    By LifeWithJava in forum New To Java
    Replies: 3
    Last Post: 12-30-2008, 06:26 AM

Posting Permissions

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