Thread: Recursion UGH!

  1. #16
    Registered User
    Join Date
    Sep 2007
    Location
    Arizona
    Posts
    164
    Well, I got it to compile, FINALLY!!! but I have 1244576 vowels in "test"
    I think I may have something wrong, Ya THINK!
    I have to make a funny, or I am going to cry!!

    I think I am getting a little punchy, I've been at this since 4am, and it is now 7pm!!!

    I have to finish this, it is an online class, where the "instructor" is non existant, and I have a deadline to complete the class - December 18th. With 390 pages of text, 15 programs and 3 more test, I don't think I will make it. I was hoping to finish this last night and the next 5 programs today after reading the covered chapters. I think I am CRAZY!!!
    Last edited by clegs; 11-23-2007 at 08:53 PM.

  2. #17
    Registered User
    Join Date
    Sep 2007
    Location
    Arizona
    Posts
    164
    The latest and not so greatest!!!
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    void displayBanner();
    int vowels(string phrase);
    
    int main()
    {
    	int ptr = 0;
    	displayBanner();
    	cout << "Enter a phrase: ";
    	string phrase;
    	getline(cin, phrase);
    	cout << "\nThe phrase you entered: " << phrase << endl;
    	cout << "\nhas " << vowels(phrase) << " vowels." << endl;
    	return 0;
    }
    
    void displayBanner()
    {
    	cout << "This programs asks for you to enter a string.\n"
    		 << "it then calculates how many vowels it contains\n"
    		 << "using a recursive function.\n\n";
    }
    
    int vowels(string phrase)
    {
    	if (phrase.empty())
    		return 0;
    	if (phrase[0] == 'a' 
    		|| phrase[0] == 'e' 
    		|| phrase[0] == 'i' 
    		|| phrase[0] == 'o' 
    		|| phrase[0] == 'u' 
    		|| phrase[0] == 'A'
    		|| phrase[0] == 'E' 
    		|| phrase[0] == 'I' 
    		|| phrase[0] == 'O' 
    		|| phrase[0] == 'U')
    		return vowels(phrase.erase(0, 1));
    }
    I looked up str.erase() as you suggested, and srt.erase(m, n) "starting at index m, removes the next n characters from str." So, that is what I used and Boo Hoo (I'm sorry,I made a funny so I wouldn't cry, my bad), I was wrong. But, I am not surprised.
    On the bright side, if there is one, the empty string works JUST FINE! HA, I got one right.

    What is a Deathray Engineer? Do they work, the dethrays I mean? Can I borrow it for tonight? I won't have to worry about recursion anymore!
    Last edited by clegs; 11-23-2007 at 08:31 PM.

  3. #18
    Registered User
    Join Date
    Sep 2007
    Location
    Arizona
    Posts
    164
    I've changed my last return statement to "return vowels(phrase) + vowels(phrase.erase(0, 1));"
    and I still get the wierd over-the-top, way too many number of vowels, so is there something else missing that I don't see or haven't thought of?
    And, if I enter a string without any vowels, it returns the same &$*#^*! number!
    What is going on?
    There has to be something really, really small that is missing in all of this!
    Last edited by clegs; 11-23-2007 at 08:41 PM.

  4. #19
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    Two comments

    1. Usually it is an if/else with recursion

    Code:
    int vowels(string phrase)
    {
    	if (phrase.empty())
    
    		return 0;
            else if (phrase[0] == 'a' 
    		|| phrase[0] == 'e' 
    		|| phrase[0] == 'i' 
    		|| phrase[0] == 'o' 
    		|| phrase[0] == 'u' 
    		|| phrase[0] == 'A'
    		|| phrase[0] == 'E' 
    		|| phrase[0] == 'I' 
    		|| phrase[0] == 'O' 
    		|| phrase[0] == 'U')
    		return vowels(phrase.erase(0, 1));
    }
    2. You can shorten this by using toupper() to shorten the or's ...

    3. I would have said....

    Code:
     if (phrase[0] == "")
         return 0;
       else
    
         ....
    Last edited by Mister C; 11-23-2007 at 08:55 PM.
    Mr. C: Author and Instructor

  5. #20
    Registered User
    Join Date
    Sep 2007
    Location
    Arizona
    Posts
    164
    Thanks, I changed my code to if else after the last post, but that didn't change the aberrant number issue.
    And, I thought that once I got the function to work I would use the toupper() or tolower() to shorten the code up. But I can't figure out the bogus number issue.
    Any ideas?
    I've been piddling with the function and the same number shows it's ugly head every time!

    I just included a cout statement on "phrase" between my vowel check and the final return "test" prints every time and a lot, so it is obvious that my phrase string less the first character isn't doing wht I was expecting. So, now I atleast know where to "fix" my bug.

    No that was not the problem.
    Last edited by clegs; 11-23-2007 at 09:20 PM.

  6. #21
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    Why don't you try getting to work non- recursively first- then rewrite using recursion?
    Mr. C: Author and Instructor

  7. #22
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    Here is the start of a non-recursive sol'n
    Code:
    int vowel = 0; /* counts the total vowels */
       int length; /* length of str */
    
       for ( index = 0; index < length; index++ ) {
          if ( str[index] == 'a' || ... ) {
             vowel = vowel + 1;
          }
       }
    Mr. C: Author and Instructor

  8. #23
    Registered User
    Join Date
    Sep 2007
    Location
    Arizona
    Posts
    164
    I WILL try that. Thanks!
    Hopefully that will give me some answers becasue I am not getting far this way.

  9. #24
    Registered User
    Join Date
    Sep 2007
    Location
    Arizona
    Posts
    164
    That was fast easy and right on! I can do that, that is the way I think!

    This is the function as an iterative (and it works):
    Code:
    int vowels(string phrase)
    {
    	int vowel = 0;
    	if (phrase.empty())
    		return 0;
    	for ( int index = 0; index < phrase.length(); index++)
    	{
    		if(phrase[index] == 'a' 
    		|| phrase[index] == 'e' 
    		|| phrase[index] == 'i' 
    		|| phrase[index] == 'o' 
    		|| phrase[index] == 'u' 
    		|| phrase[index] == 'A'
    		|| phrase[index] == 'E' 
    		|| phrase[index] == 'I' 
    		|| phrase[index] == 'O' 
    		|| phrase[index] == 'U')
    		vowel += 1;
    	}
    	return vowel;
    }
    Now for the translation!

  10. #25
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    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.
    Mr. C: Author and Instructor

  11. #26
    Registered User
    Join Date
    Sep 2007
    Location
    Arizona
    Posts
    164
    I will see what I can come up with but this is where I fall down, hard!

  12. #27
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Functionally they are the same, but you may find a switch/case structure easier to write (and possibly read) than the if statement with many conditions ORed together:
    Code:
    switch (phrase[index])
    {
    case 'a':
    case 'e':
    case 'i':
    case 'o':
    case 'u':
    case 'A':
    case 'E':
    case 'I':
    case 'O':
    case 'U':
        ++vowel;
    }
    Also, it probably does not make much of a difference, but we typically pass objects of class types by reference. In this case, the string should be passed by const reference since it is not modified:
    Code:
    int vowels(const string& phrase)
    {
        // ...
    Another thing: again, it does not make a real difference here, but the length() member function of std::string returns a std::string::size_type, not an int. By default, std::string::size_type is defined to a type that resolves to size_t, and size_t in turn is typically defined as unsigned int. As such, it is better to write:
    Code:
    for ( string::size_type index = 0; index < phrase.length(); index++)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #28
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    He means something like...

    Code:
    numChars = str.length()
    for (i = 0;  i < numChars; i++)
      {
         switch(str.at(i))
         {
           case 'a':
           case 'e':
           case 'i':
           case 'o':
           case 'u':
          vowelcount++:
        }
    }
    Mr. C: Author and Instructor

  14. #29
    Registered User
    Join Date
    Sep 2007
    Location
    Arizona
    Posts
    164
    Thanks laserlight, for all the info.

    I am trying to create a recursive function to count the vowels, but couldn't get it to work, so Mister C suggested I write the function iteratively and then translate. I got the iterative function to work but now i am back to struggling with figuring out what the recursive return call is to the function after the vowel check.

    Any suggestions?

  15. #30
    Registered User
    Join Date
    Sep 2007
    Location
    Arizona
    Posts
    164
    This is what I have for my function as a recursive one:
    Code:
    int vowels(string phrase)
    {
    	if (phrase.empty())
    		return 0;
    	else if (phrase[0] == 'a' 
    		|| phrase[0] == 'e' 
    		|| phrase[0] == 'i' 
    		|| phrase[0] == 'o' 
    		|| phrase[0] == 'u' 
    		|| phrase[0] == 'A'
    		|| phrase[0] == 'E' 
    		|| phrase[0] == 'I' 
    		|| phrase[0] == 'O' 
    		|| phrase[0] == 'U')
    		return vowels(phrase) + vowels(phrase.substr(1, phrase.size()));
    }
    And I am still getting a huge, not close to being right number of vowels returned to main. 1244576 vowels! for the word "test"

    however, when I change the phrase to pass by constant reference, I am down to 116 vowels contained in "test" so I am getting closer!
    Last edited by clegs; 11-23-2007 at 10:43 PM.

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, 02:58 AM
  3. Recursion
    By Lionmane in forum C Programming
    Replies: 11
    Last Post: 06-04-2005, 12: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