Recursion UGH!

This is a discussion on Recursion UGH! within the C++ Programming forums, part of the General Programming Boards category; clegs I gave you the recursive sol'n The base case involves the empty string, which contains zero vowels. For other ...

  1. #31
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    clegs I gave you the recursive sol'n

    The base case involves the empty string, which contains zero vowels. For other cases, examine the first letter and add one, if necessary, to the result of applying the procedure to the substring consisting of all letters except the first.

    Code:
    if (string[0] == "")
      return 0;
    else
       //finish it...
    Mr. C: Author and Instructor

  2. #32
    Registered User
    Join Date
    Sep 2007
    Location
    Arizona
    Posts
    164
    Mister C:
    I tried your statment for the base case:
    Code:
    if (phrase[0] == "")
    	return 0;
    I receive two errors while compiling!

    c:\Documents and Settings\...\T3Q24_Vowels.cpp(29): error C2040: '==' : 'int' differs in levels of indirection from 'const char [1]'

    c:\Documents and Settings\...\T3Q24_Vowels.cpp(29): error C2446: '==' : no conversion from 'const char *' to 'int'

    so, I reverted back to the phrase.empty() becasue it not only doesn't have vowels it doesn't have anything. phrase.empty() works as a base case, but the rest doesn't.

    However I changed the string to a pass by reference and changed the OR list of vowel checks to case statement and I now get just one vowel no matter what the string contains. I also changed the return to use the phrase.substr() to knock off the first character in the string, or that was my intent.
    the new code is:
    Code:
    int vowels(const string& phrase)
    {
    	if (phrase.empty())
    		return 0;
    	else 
    	switch(phrase[0])
    	{
    		case 'a': case 'A': 
    		case 'e': case 'E': 
    		case 'i': case 'I': 
    		case 'o': case 'O': 
    		case 'u': case 'U': 
    		return vowels(phrase) + vowels(phrase.substr(1, phrase.size()));
    	}
    }
    I appreciate the help and am trying all the suggestions.
    Thanks!
    Last edited by clegs; 11-23-2007 at 10:16 PM.

  3. #33
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    Here is one way to do it in Java:

    Code:
    int countVowels(String s) {
    if (s.length() == 0) return 0;
    int tailResult = countVowels(s.substring(1));
    switch (s.charAt(0)) {
    case a: case e: case i: case o: case u:
    return tailResult+1;
    default:
    return tailResult;
    }
    }
    Mr. C: Author and Instructor

  4. #34
    Registered User
    Join Date
    Sep 2007
    Location
    Arizona
    Posts
    164
    Thanks!
    I have copied that into my list of possibilities I have been collecting.
    I understand that code so I will try to translate that into C++.
    I really do appreciate all the help, really!
    I have been at this code for 15 hours!

    I was able to get the palindrome code to work about 1 1/2 hours ago, so that was a success.

    And, now this one is also!!!
    After 15 1/2 hours I have a functioning code.
    The palindrome was a much easier function to understand than the vowel counting, because counting the vowels was the hang up for me.

    Thank you Mister C, laserlight, and MacGyver. After looking at the JAVA version and putting it together with the palindrome working code and taking the suggestions from you all, I am finished, YAHOO!
    Last edited by clegs; 11-23-2007 at 11:24 PM.

  5. #35
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Congratulations.

  6. #36
    Registered User
    Join Date
    Sep 2007
    Location
    Arizona
    Posts
    164
    MacGyver, Thanks a bunch!
    I am taking a bow, and actually starting my reading for the next chapter - Stacks!
    I have a good three hours left for studying, and, I know nothing about them at this point!
    Thank you all again!
    Cindy

    Oh, yea! I hope I don't see recursion again in the languages I have coming up. I have three more, so there is a good chance I will. UGH! Maybe by then the light will have at least flickered a little.
    Last edited by clegs; 11-23-2007 at 11:29 PM.

  7. #37
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    I know sleep is overrated, but you should consider taking at least a small break occasionally. Otherwise, your chances of making mistakes increases, and you really don't want that to happen.

    Stacks are simple. They are a collection of items, but you are only able to mainly push an item onto the stack or pop the last item off. Some stacks get a little more complicated than that, but that is the main idea.

    Back to recursion, you will most likely encounter it a lot, especially when you get into more advanced data types, such as trees. The trick is to practice it a lot until you're used to it. Think of almost any problem that requires a loop to solve, and think about how you can solve such problems recursively.

  8. #38
    Registered User
    Join Date
    Sep 2007
    Location
    Arizona
    Posts
    164
    Sleep, that is close to a four-letter word for me these days!

    Glad to hear stacks are simple, I deal well with simple. We are alike, stacks and me. HA!
    And, about that recursion statement! Thanks! You might as well have hit me with that deathray of yours.

    Keep it handy, K!

    Thanks again, I feel much less stressed now that I have been able to move on to the next chapter.

    This board has kept me ?sane?. Until this class is over, or I have flunked for not completing it on time, I'll Be Back!

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Template Recursion Pickle
    By SevenThunders in forum C++ Programming
    Replies: 20
    Last Post: 02-05-2009, 09:45 PM
  2. convert Recursion to linear can it be done
    By umen242 in forum C++ Programming
    Replies: 2
    Last Post: 10-15-2008, 03:58 AM
  3. Recursion
    By Lionmane in forum C Programming
    Replies: 11
    Last Post: 06-04-2005, 01:00 AM
  4. a simple recursion question
    By tetra in forum C++ Programming
    Replies: 6
    Last Post: 10-27-2002, 10:56 AM
  5. To Recur(sion) or to Iterate?That is the question
    By jasrajva in forum C Programming
    Replies: 4
    Last Post: 11-07-2001, 09:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21