|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Recursion function (Anagramming word)
Sorry for my english Hello all my friends and seniors, i had created a programm in c++ (anagrammig of word) it works fine but i cannot understand how exactly recursion is working , i mean oh.. first look at the code . Code:
#include <iostream>
#include <string>
using namespace std;
////////////////////////////////////////////////////////////////
class word
{
private:
int size; //length of input word
int count; //numbers in display
string workStr; //workspace
void rotate(int); //rotate part of workStr
void displayWord(); //display workStr
public:
word(string); //constructor
void anagram(int); //anagram ourselves
};
//--------------------------------------------------------------
//constructor
word::word(string inpStr) : workStr(inpStr), count(0)
{ //initialize workStr
size = inpStr.length(); //number of characters
}
//--------------------------------------------------------------
void word::anagram(int newSize)
{
cout<<"Inside function"<<" "<<newSize<<endl;
if(newSize == 1) //if too small,
return; //go no further
for(int j=0; j<newSize; j++) //for each position,
{
cout<<"Inside loop before recursion"<<endl;
anagram(newSize-1); //anagram remaining
cout<<"After recursion function"<<" "<<newSize<<endl;
if(newSize==2) //if innermost,
displayWord(); // display it
rotate(newSize); //rotate word
cout<<"After rotate function"<<endl;
}
}
//--------------------------------------------------------------
//rotate left all chars from position to end
void word::rotate(int newSize)
{
int j;
int position = size - newSize;
char temp = workStr[position]; //save first letter
for(j=position+1; j<size; j++) //shift others left
workStr[j-1] = workStr[j];
workStr[j-1] = temp; //put first on right
}
//--------------------------------------------------------------
void word::displayWord()
{
cout << workStr << " ";
cout << endl;
}
////////////////////////////////////////////////////////////////
int main()
{
string input;
int length;
cout << "Enter a word: "; //get word
cin >> input;
length = input.length(); //get its length
word theWord(input); //make a word object
theWord.anagram(length); //anagram it
return 0;
} //end main()This programm running fine but in function anagram, i mean inside the loop The size of newSize variable, how can be "2" after recursion function , i mean in this statement "cout<<"After recursion function"<<" "<<newSize<<endl;" This statement is in the anagram function . This Line is my problem how newSize variable become 2 after recursion i cannot understand any help |
| Sponsored Links | ||
|
|
#2
|
|||
|
|||
|
Code:
anagram(newSize-1); You're calling anagram() from inside anagram() with one smaller size. That's how it gets smaller. The variable itself doesn't get changed, as much as when it's called again, the new call is separate because the old call hasn't finished. There's more than one variable of the same name. Last edited by Corona688; 05-31-2012 at 09:45 AM.. |
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Find EXACT word in files, just the word: no prefix, no suffix, no 'similar', just the word | chicchan | UNIX for Dummies Questions & Answers | 6 | 03-30-2012 04:28 PM |
| How to find function name based on word search | spc432 | UNIX for Dummies Questions & Answers | 9 | 07-16-2009 05:05 AM |
| Function Recursion Shift Problem | nuvpal | UNIX for Dummies Questions & Answers | 10 | 03-12-2009 12:36 PM |
| Recursion | joshighanshyam | Programming | 5 | 12-03-2008 11:15 AM |
| Help Help Help in recursion | murtaza | Shell Programming and Scripting | 6 | 03-29-2007 10:26 AM |
|
|