0

I have been trying really hard to understand this question.

Question is:- In a mathematics class, Teacher ask Alice to find the number of all n digit distinct integers which is formed by the two distinct digits a and b but there is a rule to form n digit integer.

Rule: She has to form n digit integer by using two digits a and b without consecutive b.

Input Format:- The first line contains T, the number of test cases. Further T lines contains the value n which is the number of digit in the integer.

Code:-

#include<bits/stdc++.h>

using namespace std;

void classAssignment(int n,string ans, int &count){

     if(ans.length()==n){

     cout<<ans<<endl;
    
     count++;

     return;}

    
    classAssignment(n,ans+"a",count);

    if(ans.length()==0 || ans.at(ans.length()-1)!='b'){

    classAssignment(n,ans+"b",count);}


}

int main() {

    int t;

    cin>>t;

    for(int i=0;i<t;i++){

        int count=0;

        int n;

        cin>>n;

        classAssignment(n,"",count);

        cout<<"Total paths are "<<count<<endl;}

    return 0;}

Output:

aaa
aab
aba
baa
bab
Total paths are 5

Now I am unable to understand how this code generates this output?? How are these 2 recursive calls are working to get this output??

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Nidhi P
  • 11
  • 2
  • Same way as a recursive function that only has one call to itself. I'd either play computer with a pencil and paper, writing down each step with its variables and nesting; or step through the code to watch what's happening. – Dave Newton Feb 17 '22 at 16:13
  • [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) Any class, tutorial or book that teaches you to use that header should be looked at with suspicion. – Some programmer dude Feb 17 '22 at 16:18
  • As for your problem, there are two common ways to untangle it: Use pen and paper to follow along the code. Write up each call to `classAssignment` on a new piece of paper and put it on top of the previous call. When the function returns remove the top paper. Putting the paper on top mimics the call, and removing the top paper mimics its return. Another way is to use a *debugger* to step through your code statement by statement, while monitoring variables and their values. – Some programmer dude Feb 17 '22 at 16:22

1 Answers1

0

That's what the debugger is for my friend. The debugger allows you to step through every line of code one by one, and thus, helps us to understand code and also point out errors.

So basically to understand what the recursive calls are doing, let's take an example of the first call. Now the function is first invoked by:

classAssignment(n, "", count);

Remember, ans = "" right now.

Now, the first recursive call:

classAssignment(n,ans+"a",count);

Now when this function is called, ans = "a" (for the recursive function). Now the recursive function calls the above line again, and passes ans + "a" (which means "a" + "a" = "aa") as one of the 2 arguments. Now, ans.size() == n(2), so it is printed out, and both the recursively called functions return, one after the other. Something like this goes for the second recursive call as well.

And if you're wondering for the count variable, all recursive/non-recursive are modifying the same variable, as it's passed by reference.

It may still not be clear but that's the best I could give :). For more clear understanding, you must use the debugger.

By the way, you should really look up to the reasons Why should I not #include <bits/stdc++.h>?

The Coding Fox
  • 1,488
  • 1
  • 4
  • 18