help with recursive method

Reply

Join Date: Oct 2004
Posts: 26
Reputation: SyLk is an unknown quantity at this point 
Solved Threads: 0
SyLk SyLk is offline Offline
Light Poster

help with recursive method

 
0
  #1
Oct 5th, 2006
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.



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;
}
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 30
Reputation: Rayhan Muktader is an unknown quantity at this point 
Solved Threads: 3
Rayhan Muktader Rayhan Muktader is offline Offline
Light Poster

Re: help with recursive method

 
0
  #2
Oct 5th, 2006
The folllowing is some sudo code for addition through recursion:
  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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 74
Reputation: Cudmore is an unknown quantity at this point 
Solved Threads: 5
Cudmore's Avatar
Cudmore Cudmore is offline Offline
Junior Poster in Training

Re: help with recursive method

 
0
  #3
Oct 6th, 2006
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)
  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)
  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
synchronized (theWorld) { System.out.println ("It's all mine..."); }
How many people have code in their Sigs?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC