That is messed up.
Oh, well. Anyway took me a bit to figure it out, but I wrote out a test program, but I didn't really check it out with a lot of detail. This is the line you need to pay attention to:
Code:
tmp=number%2+number%2*pow(10,position);
Anyway, math:
Code:
56 / 2 = 28 R-0
28 / 2 = 14 R-0
14 / 2 = 7 R-0
7 / 2 = 3 R-1
3 / 2 = 1 R-1
1 / 2 = 0 R-1
So the idea is to get the remainder....
So you want to build it backwards.
Code:
0
00
000
1000
11000
111000
You got the power thing along the right lines. You also have the remainder, diving-by-two thing right. What you're not doing right is saving the previous value of tmp. tmp should be as shown above, more or less.
So anyway, here are some tips:
- Remember to add tmp to whatever result you're calculating... ie. tmp+=..... or tmp = tmp +....
- Don't add number%2 to the result. I think that is messing your calculations, or else I'm missing something.
- The pow() function has a slight problem; it returns a double. Since floats and doubles are approximations and not actual numbers, you'll eventually stumble on weird things. One of those weird things is having 10000.000000 converted to an int come out to 9999, which is probably throwing your calculations way off. I don't know what's going on there (well I sort of do, but it's hard to explain it without realizing how frustrating and silly it sounds) but calling ceil() on the result of pow() in this case corrects it.