aspose file tools*
The moose likes Beginning Java and the fly likes Recursion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Recursion" Watch "Recursion" New topic
Author

Recursion

Mohd Fifi
Greenhorn

Joined: Nov 11, 2006
Posts: 3
Write a recursive method to find the sum of the elements in array
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

"easy javaa",
Welcome to the JavaRanch.

We're a friendly group, but we do require members to have valid display names.

Display names must be two words: your first name, a space, then your last name. Fictitious names are not allowed.

Please edit your profile and correct your display name since accounts with invalid display names get deleted, often without warning

thanks,
Dave
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

...and as welcome as you are, we don't do homework for others. If you would like to show us what you have done, we would be happy to help you with your problems.
Mohd Fifi
Greenhorn

Joined: Nov 11, 2006
Posts: 3
I have done the following but i have a problem with the recursive part

public class SumOfArray {

public static void main(String[] args)
{
int a[] ={2,4,5};

System.out.println(Array( a) );
}



public static int Array( int [] a)
{
if(a.length==1)
return a[0];
else
return a[a.length-1]+Array( a-[a.length-1] ) ;

}
}
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18981
    
  40

The problem seems to stem from your attempt at removing a member from the array so that you may use it recursively. Unfortunately, this can't be done easily. You have to create a new array (that is smaller), and copy the members to the new array. (You may also use the System.arraycopy() method to do the copy part)

IMO, I think it is better to change the Array() method to also take a range (start and / or end indexes). This will allow the method to work on a subarray. The recursion would be done by simply changing the range -- removing the need to create more arrays.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Mohd Fifi
Greenhorn

Joined: Nov 11, 2006
Posts: 3
but how can i copy the array and provide a shorter one i have no idea abouut this
Would you please give an example on that

thanks
Mohd
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

you can copy the array into a smaller one, but why not look at a simpler solution first.

The sum of an array is the first item plus the rest. The sum is the first plus ( the second plus the rest) ... etc

Therefore you can pass the current index and the same array each time. you can stop the recursive process when you get to the last item as you can just return it.



Notice how similar this is to your code. (it would also be more defensive to return zero if the index would otherwise cause an ArrayIndexOutOfBoundsException)

Dave
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18981
    
  40

Originally posted by Mohd Fifi:
but how can i copy the array and provide a shorter one i have no idea abouut this
Would you please give an example on that

thanks
Mohd


No special technique. Just use "new" to declare a smaller array, and use a loop to assign the elements -- one element at a time. (Or you may use System.arraycopy() to copy the array)

BTW, I still think that using an index parameter is better.

Henry
[ November 11, 2006: Message edited by: Henry Wong ]
 
 
subject: Recursion