Well, it looks like your code is trying to use a sort of pseudo-binary representation where a series of decimal 1 or 0 digits are used to visually represent a binary value.
The problem is, a number such as 1000000000 (decimal) converts to 111011100110101100101000000000 (binary).
But if we treat this as a decimal number : 111011100110101100101000000000
then it is too big to fit in a longlongint (it requires 97 bits, but a long long int has only 64).
I think it would be better to have the function return a std::string result, which can have unlimited length.
I haven't learnt yet how to use this "std::" thing and i don't know what it means at all. Besides that, what your code does is to put the 1 and 0 in a string¿
Yes, you have learned to use this std:: thing already.
Your original code has std::cin and std::cout
By putting usingnamespace std; you've saved yourself the trouble of typing the std:: each time - which is ok for some simple programs, but is actually bad practice.
Yes, my code puts "1" or "0" in a string. However, it was only by way of an example, Feel free to use the idea and write your own function in a way which you can understand, if you like.
But i have one doubt: I tried to place the 0 and the 1 in a string before asking for help but i found with some trouble:
First is that I declared the string in the recursive function, and because of that every time that the function executed itself, the string was redeclared and lost (I think thats what happens, but I am not sure).
Also that the function had to be "string to_binary ...." therefore a string had to be returned every time but i didnt know how to do that either :p
It is creating a lookup-table. That is, an array with just two elements. It allows the number 0 to be translated to the string "0" and similar for 1 and "1".
The keyword static means that it doesn't need to be re-created each time the function is called, it retains the same value from one call to the next. const is used because we definitely do not want to change the contents of the table.
And what's the difference between your code and this
Well, partly a matter of style, that is personal preference. Though there is an error in the code you posted. It should be like this:
Don't let the std:: put you off, I just copy+pasted the same code, which is why it looks similar. I used the binary & operator to get each digit, but you could get an equivalent result using the % operator. The first method is more efficient, but nothing to lose sleep over.
You could do this without using a lookup table, there are hundreds of possible ways to solve the problem.
Quote: Is it possible that you make the code a bit simpler? Well, it depends where you are sitting. What is simple may depend on (a) what you are already familiar with and (b) what you are permitted to use. C++ already has several built-in functions to do these tasks, and that's what I'd normally use. But doing the job using your own code is probably meant to aid learning, where the main benefit is in the process, rather than the result.