+ 3

I Had Solve My Own Way But I See Others Method which I had added as Comented code I don't Understand Working Can You explain me?

Can Anyone explain me working of Commented Code Working Of Bin function how all rem will print https://sololearn.com/compiler-playground/cz1bcN4P9mUu/?ref=app

31st Oct 2024, 5:25 AM
Shashikant Pandey
Shashikant Pandey - avatar
17 Réponses
+ 4
in bin it is n/2, so; bin(10){ bin(5){ bin(2){ bin(1){ cout<<1; //This is first output } cout<<0; } cout<<1; } cout<<0;//This is last output } The same as cout<<1<<0<<1<<0;
31st Oct 2024, 10:03 AM
Bob_Li
Bob_Li - avatar
+ 4
Bob_Li yes it will not give output untill recursion end but when recursion will end them rem will be print of 1 recursion or last one I hope you understand what I mean I mean like if you see factorial as printing number then am asking 5 will print first or 1 will I mean print will work for last call of function or it will work for first call of function then so.. On
31st Oct 2024, 9:57 AM
Shashikant Pandey
Shashikant Pandey - avatar
+ 3
Bob_Li Thank You Sooo Much Finally 🥰
31st Oct 2024, 10:10 AM
Shashikant Pandey
Shashikant Pandey - avatar
+ 2
Bob_Li Am sorry But you made it more complex for me and second thing I haven't ask for the code I did I ask for the code in comment I mean i had commented sone part that bin function I don't understand working of that bin function so if you can explain then please
31st Oct 2024, 7:31 AM
Shashikant Pandey
Shashikant Pandey - avatar
+ 2
What is a binary number? it is a number system with only 1 and 0. How do you convert our decimal system to binary? You divide it repeatedly by 2 and collect the remainders. why are we using pow? Because we need to put those 1 0 digits we collected into their proper position as a power of 2 10%2=0 remainder goes to 2⁰ 10/2=5 5%2=1 reminder goes to 2¹ 5/2=2 2%2=0 remainder goes to 2² 2/2=1 1%2=1 remainder goes to 2³ 2⁴2³2²2¹2⁰ 1 0 1 0 binary 8+0+2+0 = 10 decimal
31st Oct 2024, 7:45 AM
Bob_Li
Bob_Li - avatar
+ 2
Bob_Li No No These all i understand but am asking How this is working like we call function inside function and before printing rem again function call then how this will work and print all rem as binary am asking about this code not about my code I want to understand working of it just it step by step it's working. void bin(int n) { int rem; if (n<= 1) { cout << n; return; } rem = n % 2; bin(n/ 2); cout << rem; } */
31st Oct 2024, 7:49 AM
Shashikant Pandey
Shashikant Pandey - avatar
+ 2
Bob_Li Yess
31st Oct 2024, 8:01 AM
Shashikant Pandey
Shashikant Pandey - avatar
+ 2
Bob_Li can you please explain working of bin(bin(... bin(n/2))) by giving any example I want to understand how it work how rem will be printed
31st Oct 2024, 8:32 AM
Shashikant Pandey
Shashikant Pandey - avatar
+ 2
Bob_Li You Explain well and i got it also but recursion is fine but when and how cout rem will excited I just want to understand working of that like
31st Oct 2024, 9:06 AM
Shashikant Pandey
Shashikant Pandey - avatar
+ 2
Recursive functions is when you call the function inside itself. The most common example is getting the factorial of a number int factorial( int n ){ if ( n==0) return 1; return n * factorial( n-1 ); } notice that factorial also uses factorial inside the function, but with n-1 instead of n. How can it solve that? The code then sets up a stack of unsolved factorial functions with smaller and smaller n because n-1 is passed to the inner functions. When n==0, it returns 1, which is used by the function before it where n was 2, so that function returns 2*1, which is again used by the one before it where n was 3, so that returns 3*2, which is again used by the function before it where n was 4, so that returns 4*6....and so on until it reaches the outermost factorial function where the result is multiplied by n.
31st Oct 2024, 9:07 AM
Bob_Li
Bob_Li - avatar
+ 2
Bob_Li i think am not able to explain my question am not good enough in English actually I know what recursion is This code will call bin function again and again untill n is greater than 1 but am asking next line of recursion the line in which cout rem how this rem will be printed function called 100 times but how output how rem will be printed 😔
31st Oct 2024, 9:19 AM
Shashikant Pandey
Shashikant Pandey - avatar
+ 2
Bob_Li yes in factorial I understand bcz return*factorial(n-1) that clearly we can see return will multiplly everytime but in code I given first function call then in second like cout rem am sorry but I still didn't get it but okay fine Thank You soo much it's not ur mistake am not enough smart to understand that Thank You ❤
31st Oct 2024, 9:41 AM
Shashikant Pandey
Shashikant Pandey - avatar
+ 1
I added some cout to hopefully make the in-between steps in the while loop visible. https://sololearn.com/compiler-playground/cbayhcUzlR0a/?ref=app
31st Oct 2024, 7:23 AM
Bob_Li
Bob_Li - avatar
+ 1
oh, you mean the code you commented out? it is doing the same thing, except instead of incrementing the int binary variable, it is directly outputting each remainder to cout. Notice that it is a recursive function. so it first builds up a stack of bin(bin(bin(...bin(n/2)...))) until n<=1, then evaluates them recursively. That's why you don't get a reversed result. Think of recursive functions as setting up a line of dominos. After you line them up, tipping the last one topples the one in front of it, setting up a chain effect.
31st Oct 2024, 8:00 AM
Bob_Li
Bob_Li - avatar
+ 1
Shashikant Pandey no one is saying your code is wrong. You always make these odd statements. Perhaps you are using a translating app? I updated the code I posted earlier and added cout to the bin function. I have explained it to the best of my ability. Perhaps more capable members here can give a better explanation.
31st Oct 2024, 9:02 AM
Bob_Li
Bob_Li - avatar
+ 1
so factorial(5) would look like this before it is evaluated: factorial(5){ return 5 * factorial(4){ //Final return value return 4 * factorial(3){ return 3 * factorial(2){ return 2 * factorial(1){ return 1; //This is the first return value } } } } } which would be equivalent to 1*2*3*4*5
31st Oct 2024, 9:36 AM
Bob_Li
Bob_Li - avatar
+ 1
In recursive functions, the code will not give you output until the function stack is completed. That is why the base case is important. It will stop the code generation and start the evaluation.
31st Oct 2024, 9:52 AM
Bob_Li
Bob_Li - avatar