Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
void binary(int n)

{

if(n < 1)
printf("%s\n",A); // Assume A is a global variable

else

{

A[n-1] = '0';

binary(n-1);

A[n-1] = '1';

binary(n-1);

}

}
Can anyone explain how the stack frames are generated for n=2?when n=2 i am getting only 00 and 11 but how are the remaining numbers i.e 10 and 01 generated?
Posted
Comments
Sergey Alexandrovich Kryukov 29-Oct-15 12:07pm    
What makes you thinking that this is related to stack frame? Stack frame is created when you call a function and exists before you return.
The idea to use recursion in this simple problem is plain insane. And you are not even trying to determine bit states. That is, you never use the value on n.
—SA
Sam Dean 29-Oct-15 13:00pm    
this is recursion and recursion uses stack frame.So thats why i asked about stack frame
Richard MacCutchan 30-Oct-15 6:01am    
Stack frames are generated the same way for every function call. The values of your variables are irrelevant. The way to identify what is happening in your code is to step through it with the debugger.

Please see my comment to the question.

I basically answered you about stack; will also add this: a function parameters are put on the stack, and they disappear with the frame when function returns. But of course, you need to learn seriously how stack, stack frames and calls work, this is one of the most fundamental basics of programming. But your question does not provide any adequate sample for the study of it, except the work "recursion", so there is nothing else to explain.

See also: https://en.wikipedia.org/wiki/Call_stack[^].

Now, your real problem is that you don't even try to use the value of n.

All you need to solve your problem is bitwise operators, and a loop:
https://en.wikipedia.org/wiki/Bitwise_operations_in_C[^],
http://www.cprogramming.com/tutorial/bitwise_operators.html[^].

—SA
 
Share this answer
 
Comments
Sam Dean 29-Oct-15 13:06pm    
thanks for your reply Sergey Alexandrovich Kryukov.Its just that every time I am using n=2 and i am trying to get all possible values but I am unable to understand how all the values i.e 00,11,10,01 are churned out.
Sergey Alexandrovich Kryukov 29-Oct-15 14:58pm    
Which par of my explanation is unclear? Well, you have to use shift operator to get to pin position, and bitwise AND to detect if the bit is set or clear.
If this is not clear (see also the tutorial I reference in last link), I'll explain more. Create a mask by shifting 1 to the position of a test bit, then AND it with tested number to see if it is 0 or not. That's all.
—SA
If you don't understand what is doing your code, the best is to see what it is doing with a debugger.
The debugger allow you to execute your code step by step and inspect variable as the code is executed.
You will see where and why your code is not doing what you expect.

Tour code fragment is not complete and we can't compile it to see what it is doing and why it fail.
 
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