-2

Hello can someone please explain the output of the following C++ code espacially the numbers after the output of the first one 43211223334444

Here is the code:

void rek(int i) { 
if (i > 0){
   cout << i; 
   rek(i-1); 
   for (int k=0; k < i; k++) 
      cout << i; }
}
int main(){
 rek(4);
 return 0;
}

Here is the output:

the output is: 43211223334444
Marcel6926
  • 27
  • 2
  • 2
    1. clang format this. 2. Learn to use debugger ASAP 3. Did you noticed for loop? – Marek R Jun 18 '21 at 10:03
  • By the way! Welcome to SO! Please take a look at ["How to ask a good question"](https://stackoverflow.com/help/how-to-ask) it will help you down the road. Also, please, don't forget to mark an answer as accepted if you consider one as such. – rawrex Jun 18 '21 at 11:10
  • 1
    [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/995714), [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – phuclv Jul 11 '21 at 10:18

5 Answers5

5

All the trouble comes from the fact that your output didn't introduce any separation in values cout << i; prints.

You actually getting the following at first:

4, 3, 2, 1 

We got this out of these two statements:

// ...
cout<< i;
rek(i-1);
// ...

Where each call to the rek(i) prints its current i and then calls rek(i-1), which in turn before proceed through the function body has to print its i-1 and then call rek((i-1)-1)... And so on, until it hits the i < 0 case.

The remaining part is brought to you by the output of 1 one time, 2 two times, 3 three times, and 4 four times. All because of the for loop:

for (int k=0; k < i; k++) // print the value of i, i times
      cout << i;

Thus, you essentially have the following:

4
3
2
1 // We hit base case on next rek() call, so no print out of there
1 // Here the for loop comes in
2 2
3 3 3  
4 4 4 4

By the way, please note that the placement of brace(s) in your code is somewhat counterintuitive.

rawrex
  • 4,044
  • 2
  • 8
  • 24
3

We can write the dynamic stack development down manually. Below I have pseudo code with an indentation of four per stack level. The commands are on the left, the resulting output, in that order, on the right. I have replaced i with the respective number to make even more transparent what's going on.

rek(4)                                                              
    cout << 4;                                      4
    rek(3)
        cout << 3;                                  3
        rek(2)
            cout << 2;                              2
            rek(1)
                cout << 1;                          1
                rek(0)
                    if(i>0) // fails, rek(0) returns
                rek(1) continues
                for (int k=0; k < 1; k++)
                    cout << 1;                      1
                rek(1) returns
            rek(2) continues
            for (int k=0; k < 2; k++)
                cout << 2;                          2
                cout << 2;                          2
            rek(2) returns
        rek(3) continues
        for (int k=0; k < 3; k++)   
            cout << 3;                              3
            cout << 3;                              3
            cout << 3;                              3
        rek(3) returns
    rek(4) continues
    for (int k=0; k < 4; k++)   
        cout << 4;                                  4
        cout << 4;                                  4
        cout << 4;                                  4
        cout << 4;                                  4
    rek(4) returns
main() continues
return 0;
Peter - Reinstate Monica
  • 15,048
  • 4
  • 37
  • 62
1

Lets break your output string: "43211223334444" into 2 parts:

  1. Part 1: "4321" This is the result of

    cout << i; 
    rek(i-1); 
    

You are printing i and recursively calling same function by passing the (i-1) as argument and function performs the operation till (i > 0). This prints the numbers from 4 to 1, i.e. "4321"

  1. Part 2 "1223334444" This is the result of code section:

    for (int k=0; k < i; k++) 
       cout << i;
    

This section gets called in reverse order from number 1 to 4. This code section basically prints the number i, for the i times.

For i=1 it prints: 1
For i=2 it prints: 22
For i=3 it prints: 333
For i=4 it prints: 4444

That makes the string: "1223334444"

Hope this explains you the total output string: "43211223334444"

Ashish Khurange
  • 903
  • 7
  • 17
-1

you are doing recursive function and then a for-loop and you're function is going deep in the recursion and then the for loop is working from the deepest point of the recursion that's why you see this output

-1

You start with going "down in the recursive tree", first you complete printing the numbers from i=4 to i=1 and therefore you print: 4321. After that you go "up the recursive tree" and start printing from i=1 to i=4 using the loop you wrote, therefore the order is now for i=1 you print 1 and for i=2 you print 22 which result in printing 1223334444 at the end of the line.

Udi
  • 172
  • 1
  • 6