0
Can someone tell me why won't the program work right
12 odpowiedzi
+ 3
Let's break this down:
This is the function which converts a decimal to a binary number. It receives an unsigned integer as argument and returns a string:
string decToBin(unsigned n) {
first it creates an empty string:
string str {};
then it starts a do-while loop. do-while loops are just like while loops, but go into it at least 1 time (as opposed to while loops which only start when the condition is met):
do{
inside the loop it does a bitwise and with the received number and the digit 1. This actually takes the bit value of the number and not the decimal. If the 1st digit of n is 1 it adds a 1 to the string, else a 0.
str += to_string(n&1);
Now the bits of the number are shifted to the right. If it was 5 (binary: 0101) it is now 2(binary 0010)
n >>= 1;
since the number is now 2 and 2>0 it will do the same steps an additional time and repeats this until the binary form of the input is 0
} while(n > 0);
Now we have the final string, but in the opposite order. This reverses the string:
reverse(str.begin(), str.end());
And finally returns the end value:
return str;
}
If it's not clear let me know then I can make a specific example.
edit: You should give the best answer to Bebida Roja. I just dissected his/her code.
+ 5
Now that you have a solution, here's re: your code (your question + recursing):
if(a%2==0){
return 0; // note 1
binary(value1/2); // note 2
}
1. Returning is committed; the code after a return never runs (+smart compilers will remove any dead code from the final binary)
2. If this was reachable, you never use the answer so it gets thrown away.
When recursing, this is one way to return a sequence (pseudocode; this is meant to be read conceptually):
recurse(x) {
if x==0 return x // stop recursing
return str(x) + recurse(x-1) // dig deeper
}
print recurse(5)
The 'returns' are an unbroken chain ...(return recurse(return recurse(return x)) so the value is not discarded and reaches the outside.
Sample approaches:
1. print (cout) immediately (and recurse deeper only if you still have value > 0)
2. update a global variable (same stop condition as #1, different output method)
3. concatenate to a local variable whatever is returned from deeper, then return the new local var
These all kindof make me cringe because a loop is better suited from a resource-use perspective. Still... it's not "wrong" if your goal is to learn recursion :)
// more direct pseudocode for approach 1
recurse(x):
if(x/2 > 0) recurse(x/2)
decide to print 0 or 1 (using mod 2)
return;
recurse(32) // 100000
+ 5
thank you all for the help and have a good day ( ^ω^)
+ 4
Bebida Roja thx for the code but isn't this a bit advanced I tried to understand it but its higher than the level that I reached in c++
+ 3
thank you again ( ^ω^)
+ 3
no this is cristal clear
+ 2
Something like this?
https://code.sololearn.com/cDH6xwsFf0Fi/?ref=app
+ 2
The algorithm goes like this:
You check if the last bit of the number is set, if it is, we know that it has a reminder of 1 when divided by 2, so we append a 1 to the container that contains the binary digits. We divide by two to get rid of the last bit, and repeat until the number we are dividing by 2 is a 0.
+ 2
Thanks for the explanation Alex, I felt like there was too much to say, but you synthesized it.
+ 1
What exactly are you trying to do?
+ 1
to change from decimal to binary
+ 1
thats a simple try out since im still new to c++