Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Recursion

 
Mohd Fifi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Write a recursive method to find the sum of the elements in array
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21197
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mohd Fifi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21197
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic