GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 10-May-2005, 05:27
SnackMan78 SnackMan78 is offline
New Member
 
Join Date: Feb 2005
Location: Wash. DC
Posts: 13
SnackMan78 is on a distinguished road

Trouble with finding a vowel using recursion


I have a project in which I am to find all of the vowels entered in a word using recursion. I've written something, and based on the displays I have in the code, I am parsing the data properly, but the recursion part is undoing my counts. What do I need to do (and undo) to make this work as intended?

CPP / C++ / C Code:
#include <iostream>
#include <string>
   using namespace std;

int findVowel(string, int, int);
bool vowelSwitch(char chr);

int main()
{
	string inStr;
	bool foundIt = false;
	int cnt = 0, stringLen = 0;

	cout<<"Enter the string to be searched...";
	cin >> inStr;

	stringLen = inStr.size();

	cnt = findVowel(inStr, stringLen - 1, cnt);

	cout<<"\nThe vowel count is: " << cnt;

	cout<< "\n\n";
	return 0;	
}


int findVowel(string inStr, int stringLen, int cnt)
{
	bool itzaVowel = false;
	int dummyVal = 0;

	if(stringLen < 0)
		return cnt;
	else
	{
		char chr = inStr.at(stringLen);
		itzaVowel = vowelSwitch(chr);
		if(itzaVowel)
		{
			cnt++;
			stringLen--;
			dummyVal = findVowel(inStr, stringLen, cnt);
		}
		else
		{
			stringLen--;
			dummyVal = findVowel(inStr, stringLen, cnt);
		}
	}

	cout<<"\nBefore returning, the cnt: " <<  cnt 
		<<", dummyVal = " << dummyVal;
	return cnt;
}


bool vowelSwitch(char chr)
{
	switch(chr)
	{
		case 'A': case 'a':
		case 'E': case 'e':
		case 'I': case 'i':
		case 'O': case 'o':
		case 'U': case 'u':
			cout<<"\nThis character: " << chr << " is a vowel";
			return true;
		default:
			cout<<"\nThis character: " << chr << " is NOT a vowel";
			return false;
	}
}
  #2  
Old 12-May-2005, 08:34
maprich maprich is offline
Member
 
Join Date: May 2005
Posts: 163
maprich has a spectacular aura aboutmaprich has a spectacular aura about
Ok firstly: why do you insist doing this with recursion? This kind of problem is far more easily solved by using simple iteration (for(){}).

Secondly To your problem: Your problem lies with variables cnt and dummyVal. Its a bit complicated to explain the reason but here is the correction:

Delete the:
CPP / C++ / C Code:
	int dummyVal = 0;

Replace your 2 instances of:
CPP / C++ / C Code:
	dummyVal = findVowel(inStr, stringLen, cnt);
With:
CPP / C++ / C Code:
	cnt = findVowel(inStr, stringLen, cnt);

this should remedy your problem.

Here's what I think about the reason your original code doesn't work: you count the wovels when the recursion goes "downward" but the information is not passed "upward" and the root of your recursion doesn't get it. Remember that recursion is two way process. By root I mean the point of code that first time calls the recursive function.
Pseudo code:
CPP / C++ / C Code:
let the function be:
F() {
  part A;
  if( go_on ) F();
  part B;
  return sth;
}
now the processing go "downward" until go_on==false :
root =
F() {
    part A;
    if -------------->  F() {
                                part A;
                                if ------------> F() {
                                                      part A;
                                                      ..until go_on==false
from now on the process goes "upward"
                                                      part B;
                                <-------------- return sth;}
                                part B;
    <------------------ return sth;}
    part B;
    return sth;
}
  #3  
Old 12-May-2005, 10:23
SnackMan78 SnackMan78 is offline
New Member
 
Join Date: Feb 2005
Location: Wash. DC
Posts: 13
SnackMan78 is on a distinguished road
Thumbs up

Thanks for your help. Your solution worked well.

I agree, using an iterative process like a for/loop would have worked quite well; however, the exercise required using recursion.

Also thanks for your example on the recursion process. I have a couple more problems I need to solve using recursion, so hopefully I can use your example in solving my remaining exercises.
  #4  
Old 12-May-2005, 14:35
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by maprich
Ok firstly: why do you insist doing this with recursion? This kind of problem is far more easily solved by using simple iteration (for(){}).
Maybe because
Quote:
Originally Posted by SnackMan78
I have a project in which I am to find all of the vowels entered in a word using recursion.

Paraphrased:
I have been given a task requiring me to:
a) find all vowels
b) using recursion
;-) ;-)
__________________

Age is unimportant -- except in cheese
 
 

Recent GIDBlogHalfway done! by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Having trouble trying to format C: Nickster64 Computer Software Forum - Windows 2 27-Jul-2004 07:31

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 17:37.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.