Click here to Skip to main content
15,915,777 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
static void what(int[] x, int n)
{

    if (n == 0)
    {
        for (int i = x.Length - 1; i >= 0; i--)

            Console.Write("{0}", x[i]);
        Console.WriteLine();
    }
    else
    {
        x[n - 1] = 0;
        what(x, n - 1);
        x[n - 1] = 1;
        what(x, n - 1);
    }
}


What I have tried:

the output are
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111

i just wanna know how we got them
Posted
Updated 4-Jun-17 18:47pm
v2
Comments
PIEBALDconsult 3-Jun-17 12:25pm    
It works recursively.

 
Share this answer
 
Quote:
How does this function works: when n=4;
It is recursive, the function calls itself.

There is a tool that allow you to see what your code is doing, its name is debugger. Mastering the debugger is not optional, it is mandatory for any programmer, no exception.
It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Debugging C# Code in Visual Studio - YouTube[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
Comments
[no name] 3-Jun-17 14:47pm    
ok, a 5. Even it is a very general answer, but it fits :-)
Patrice T 3-Jun-17 14:51pm    
Thank you.

You can visualise this as a series of callbacks. Your method makes 2 recursive calls. These calls, in turn, make 2 recursive calls. So you can visualise it as method whatA calling methods whatAA and whatAB. Then Method WhatAA calling method whatAAA and whatAAB etc. Do something like this


JavaScript
whatA(2)
  n=2, arr[1]=0
  what AA(1)        //stack push A
   {
    n=1, arr[0]=0
      what AAA(0)    //stack push AA  
      {
        n=0, prints 0,0
        returns
      }              //stack pop  AA
    n=1,arr[0]=1 
      what AAB(0)    // stack push AA
      {
        n=0, prints 0,1
        returns
      }              //stack pop AA
    }                //stack pop A
    n=2, arr[1]=1  
  what AB(1)         //stack push A
   {
     n=1, arr[0]=1
       what ABA(0)   //stack push AB
       {
        prints 1,1
        returns
       }             //stack pop AB
   }                 //stack pop A
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900