Go Back   The UNIX and Linux Forums > Top Forums > Programming


Programming Post questions about C, C++, Java, SQL, and other programming languages here.

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 05-30-2012
Registered User
 
Join Date: Mar 2012
Posts: 30
Thanks: 1
Thanked 0 Times in 0 Posts
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  
Old 05-31-2012
Mead Rotor
 
Join Date: Aug 2005
Location: Saskatchewan
Posts: 16,407
Thanks: 492
Thanked 2,538 Times in 2,421 Posts

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
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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



All times are GMT -4. The time now is 07:37 PM.