944,004 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 4909
  • Java RSS
Oct 5th, 2006
0

help with recursive method

Expand Post »
can someone explain how i could go about applying this recursive method to a string of integers from a text file.

so far i have read the string into an int array in my main using a tokenizer and im unsure of how i should manipulate the array now.

also does the int list[] parameter in my method need to be initialized even though its being passed to the actual array parameter in the main?

my objective here is to recursively add the array of integers.
any good advice would be appreciated.



Quote ...
static int sumarray(int list[],int size)
{
int currentTotal;
int total;
if ( size == 0 )
total = 0;
else
currentTotal = sumarray( list, size - 1 );
total = currentTotal + list[size - 1];
return total;
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
SyLk is offline Offline
27 posts
since Oct 2004
Oct 5th, 2006
0

Re: help with recursive method

The folllowing is some sudo code for addition through recursion:
Java Syntax (Toggle Plain Text)
  1. int sum_recursion(array, size)
  2. {
  3. if (size equals to 1) then ( return 0)
  4. else ( array[size -1] + sum_recursion(array, --size) );
  5. }
You also have to type cast the the string numbers to integers while adding them, (if you already haven't done so). You should look in you text book if you don't know how to type cast. Hope this qualifies as good advice and good luck.
Last edited by Rayhan Muktader; Oct 5th, 2006 at 4:13 pm.
Reputation Points: 28
Solved Threads: 3
Light Poster
Rayhan Muktader is offline Offline
30 posts
since Oct 2006
Oct 6th, 2006
0

Re: help with recursive method

I hardly understand what you're doing or why, but here's the recursive solution.. Recursion is unnecessary and stack-dangerous, by the way. (StackOverflowErrors are ugly). Using a StringTokenizer is also unnecessary. I've included two examples, one is Your way, and the other is My way.

Your Way (Recursive and using a StringTokenizer)
Java Syntax (Toggle Plain Text)
  1. public static void main (String[] args) {
  2. String IntegerString = "1 2 3 4 5 6 7 8 9 10";
  3.  
  4. StringTokenizer Tokenizer = new StringTokenizer (IntegerString);
  5.  
  6. int[] ints = new int[Tokenizer.countTokens()];
  7.  
  8. for (int i = 0; i < ints.length; i++)
  9. try { ints[i] = Integer.parseInt (Tokenizer.nextToken()); }
  10. catch (Exception ex) { ints[i] = 0; /* PARSE ERROR */ }
  11.  
  12. System.out.println ( "The sum of the integers is: " + Summarize (ints, ints.length) );
  13. }
  14.  
  15. public static int Summarize (int[] ints, int curpos) {
  16. return (--curpos > 0 ? Summarize (ints, curpos) : 0) + ints[curpos];
  17. }
  18.  
  19. // OR
  20.  
  21. public static int Summarize2 (int[] ints, int curpos) {
  22. if (--curpos < 0) return 0;
  23. return ints[curpos] + Summarize (ints, curpos);
  24. }

My Way (Quicker, Fewer imports, No recursion)
Java Syntax (Toggle Plain Text)
  1. public static void main (String[] args) {
  2. String IntegerString = "1 2 3 4 5 6 7 8 9 10";
  3.  
  4. String[] Tokens = IntegerString.split ("\\s+");
  5.  
  6. int[] ints = new int[Tokens.length];
  7.  
  8. for (int i = 0; i < ints.length; i++)
  9. try { ints[i] = Integer.parseInt (Tokens[i]); }
  10. catch (Exception ex) { ints[i] = 0; /* PARSE ERROR */ }
  11.  
  12. System.out.println ( "The sum of the integers is: " + Summarize (ints) );
  13. }
  14.  
  15. public static int Summarize (int[] ints) {
  16. int Total = 0;
  17. for (int i : ints) Total += i;
  18. return Total;
  19. }
Last edited by Cudmore; Oct 6th, 2006 at 6:55 pm. Reason: Improvement
Reputation Points: 20
Solved Threads: 6
Junior Poster in Training
Cudmore is offline Offline
74 posts
since Nov 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Escaping
Next Thread in Java Forum Timeline: algorithm problem with recursion





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC