Thomas Ball
Thomas Ball

Reputation: 153

How can I make a recursive function to reverse the output of another recursive function?

I wrote an assignment for class where I have to write a recursive function to output a set of numbers using recursion, pretty easy and I managed to do that. Now I have to write another function that uses recursion to output those same numbers in reverse order, that is where I am stuck, I'm at a loss for how to print out a reverse output for that using recursion. So my question remains, how can I use recursion to print out the reverse output of my first function? I am supposed to be reversing the order of the output, not computing the sequence in reverse.

Also, another question, why is my count counter not working? Thanks for the help.

Code:

#include <iostream>
using namespace std;

void reverseOJP(int refD, int D) {

// ?


}

void lengthOJP (int D, int count) {

cout << endl << "The length of the OJP for " << D << " is " << count << endl;

}

int OJP(int D, int count) {

count++;
if (D == 1) { // Base case

    return count;


} else if ((D % 2) != 0) { // odd numbers

    D = ((D * 3) + 1); // if D is odd

    cout << D << " ";

    OJP(D, count); // Recursive Call

} else { // even

    D /= 2;

    cout << D << " ";

    OJP(D, count);
}

return count;
}

int main() {
int D, count = 0; // variables
int refD;

cout << "Positive integer: " << endl;

D = 12;

// OJP output
cout << "The OJP for " << D << endl;
cout << D << " ";

refD = D; // D reference for output purposes
//OJP(D, count, refD); // OJP call

count = count + OJP (D, count);

cout << "The OJP for " << D << endl;
cout << "1 ";
reverseOJP(refD, D); // call and print reverse OJP
lengthOJP(refD, count); // Once reverse is printed print length


return 0;
}

Desired output:

Enter a positive integer: 12

The OJP for 12: 12 6 3 10 5 16 8 4 2 1

The reverse OJP for 12: 1 2 4 8 16 5 10 3 6 12 

The length of the OJP for 12 is 10

Upvotes: 0

Views: 264

Answers (2)

jgreve
jgreve

Reputation: 1253

As for what count is returning, take a look at the sample output below and see if you can spot where your getting something different from "count" in main() than you expected. It is easier to troubleshoot this sort of thing when you can see what the code is doing, I like adding some debug prints in my code like the following:

#include <iostream>
#include <string>  // added for debug stuff.
using namespace std;
using namespace std;
void reverseOJP(int refD, int D) {
// ?
}

void lengthOJP (int D, int count) {
   cout << endl << "The length of the OJP for " << D << " is " << count << endl;
}



//***** debug stuff ******
int debug_depth = 0; // caller must increment & decrement when entering & leaving funct.

void debug_indent( ) {
   for( int i = 0; i < debug_depth; ++i ) cout << ":   ";
}

void debug_enter( const string & funct_name ) {
   ++debug_depth;
   debug_indent(); cout << ">" << funct_name << "()" << endl;
}

void debug_exit( const string & funct_name ) {
   --debug_depth;
   debug_indent(); cout << "<" << funct_name << "()" << endl;
}
//***** end stuff ******

int OJP(int D, int count) {
   // debug output convention, ">" going into a fnct and "<" leaving it.
   // doing the simple indent makes it easier to see what fnct is doing.
   debug_enter("OJP");
   debug_indent(); cout << "OJP(): my count=" << count << endl;

   count++;
   debug_indent(); cout << "OJP(): after ++, now count=" << count << endl;

   if (D == 1) { // Base case
       debug_indent(); cout << "debug.OJP(): D==1, returning count=" << count << endl;
       debug_exit("OJP");
       return count;
   } else if ((D % 2) != 0) { // odd numbers
       D = ((D * 3) + 1); // if D is odd
       // SAVE:  cout << D << " ";
       debug_indent(); cout << "odd, now D=" << D << endl;
       OJP(D, count); // Recursive Call
   } else { // even
       D /= 2;
       // SAVE: cout << D << " ";
       debug_indent(); cout << "even, now D=" << D << endl;
       OJP(D, count);
   }
   debug_indent(); cout << "debug.OJP(): returning count=" << count << endl;
   debug_indent(); cout << "debug.OJP(): returning count=" << count << endl;
   debug_exit("OJP");

   // *** When you return count here, what is the value going back to main() ?
   return count;
}

int main() {
   int D, count = 0; // variables
   int refD;
   cout << "Positive integer: " << endl;
   D = 12;
   // OJP output
   cout << "The OJP for " << D << endl;
   cout << D << " ";
   refD = D; // D reference for output purposes
   //OJP(D, count, refD); // OJP call
   cout << "main(): before OJP(), count=" << count << endl;
   count = count + OJP (D, count);
   cout << "main(): now, count=" << count << endl;
   cout << "The OJP for " << D << endl;
   cout << "1 ";

   reverseOJP(refD, D); // call and print reverse OJP
   lengthOJP(refD, count); // Once reverse is printed print length
   return 0;
}

example output:

Positive integer: 
The OJP for 12
12 debug.main(): before OJP(), count=0
:   >OJP()
:   OJP(): my count=0
:   OJP(): after ++, now count=1
:   even, now D=6
:   :   >OJP()
:   :   OJP(): my count=1
:   :   OJP(): after ++, now count=2
:   :   even, now D=3
:   :   :   >OJP()
:   :   :   OJP(): my count=2
:   :   :   OJP(): after ++, now count=3
:   :   :   odd, now D=10
:   :   :   :   >OJP()
:   :   :   :   OJP(): my count=3
:   :   :   :   OJP(): after ++, now count=4
:   :   :   :   even, now D=5
:   :   :   :   :   >OJP()
:   :   :   :   :   OJP(): my count=4
:   :   :   :   :   OJP(): after ++, now count=5
:   :   :   :   :   odd, now D=16
:   :   :   :   :   :   >OJP()
:   :   :   :   :   :   OJP(): my count=5
:   :   :   :   :   :   OJP(): after ++, now count=6
:   :   :   :   :   :   even, now D=8
:   :   :   :   :   :   :   >OJP()
:   :   :   :   :   :   :   OJP(): my count=6
:   :   :   :   :   :   :   OJP(): after ++, now count=7
:   :   :   :   :   :   :   even, now D=4
:   :   :   :   :   :   :   :   >OJP()
:   :   :   :   :   :   :   :   OJP(): my count=7
:   :   :   :   :   :   :   :   OJP(): after ++, now count=8
:   :   :   :   :   :   :   :   even, now D=2
:   :   :   :   :   :   :   :   :   >OJP()
:   :   :   :   :   :   :   :   :   OJP(): my count=8
:   :   :   :   :   :   :   :   :   OJP(): after ++, now count=9
:   :   :   :   :   :   :   :   :   even, now D=1
:   :   :   :   :   :   :   :   :   :   >OJP()
:   :   :   :   :   :   :   :   :   :   OJP(): my count=9
:   :   :   :   :   :   :   :   :   :   OJP(): after ++, now count=10
:   :   :   :   :   :   :   :   :   <OJP()
:   :   :   :   :   :   :   :   :   debug.OJP(): returning count=9
:   :   :   :   :   :   :   :   <OJP()
:   :   :   :   :   :   :   :   debug.OJP(): returning count=8
:   :   :   :   :   :   :   <OJP()
:   :   :   :   :   :   :   debug.OJP(): returning count=7
:   :   :   :   :   :   <OJP()
:   :   :   :   :   :   debug.OJP(): returning count=6
:   :   :   :   :   <OJP()
:   :   :   :   :   debug.OJP(): returning count=5
:   :   :   :   <OJP()
:   :   :   :   debug.OJP(): returning count=4
:   :   :   <OJP()
:   :   :   debug.OJP(): returning count=3
:   :   <OJP()
:   :   debug.OJP(): returning count=2
:   <OJP()
:   debug.OJP(): returning count=1
<OJP()
debug.main(): now, count=1
The OJP for 12
1 
The length of the OJP for 12 is 1

Upvotes: 0

jofel
jofel

Reputation: 3405

To reverse the output, just reorder int OJP(int D, int count):

Print the current number after, instead of before, you make the recursive calls (i.e. just before the return instruction):

void reverseOJP(int D, int refD) {
  if (D == 1) { // Base case
    return ;
  } else if ((D % 2) != 0) { // odd numbers
    D = ((D * 3) + 1); // if D is odd

    reverseOJP(D, D); // Recursive Call
    cout << refD << " ";

 } else { // even
    D /= 2;
    reverseOJP(D, D);
    cout << refD << " ";
 }

}

The listed code is just slightly adapted copy-paste from OJP and not yet optimized.

Upvotes: 0

Related Questions