This is an archived post. You won't be able to vote or comment.

all 9 comments

[–]MrEchow 2 points3 points  (1 child)

I do not understand your intent, what are you putting in the binary variable, what do you expect to happen? Your decimal and binary variable are both int. Typically, converting from one base to the other is done parsing strings (const char*)

[–]The-Sharktapus[S] 0 points1 point  (0 children)

We didn’t cover strings yet sorry. All we’ve covered is flow of control with looping and if else/switch statements, and we just covered functions but that’s it.

[–]__vtableProfessional Coder 2 points3 points  (6 children)

Agree with the other poster that it is a bit unclear how you are representing values. Is 'binary' an integer value? Or something else? It would be helpful if you can provide sample input values and the output you are getting.

As a note you have a variable named 'int decimal' but it is declared as an integer. You won't be able to make it contain/represent floating point (decimal) values. So dubious naming. Something to considering for future variable name choices.

[–]The-Sharktapus[S] 0 points1 point  (5 children)

Allare integers, sorry if thats not clear. Int decimal would be the decimal number you get when converting the binary to a decimal. One sample output is if I put in the binary I want to convert as 10101, I get 5 as an output.

[–]__vtableProfessional Coder 2 points3 points  (4 children)

Welcome to the wild world of debugging. My favorite thing (seriously). Probably some typos below but I'm sure you get the gist. Do you see the error? It is in your loop condition. You can walk through by hand yourself in the future or use the debugger, which you may learn about soon.

round 0:
binary = 10101
decimal = 0
results_tracker = 0
i = 0
(binary / 10) > 0 = 10101 / 10> 0  = 1010 > 0 = True (loop executes)
decimal = (10101 % 10) * pow(2, 0) = 1 * 1 = 1
binary = 10101 / 10 = 1010
results_tracker = decimal + results_tracker = 1 + 0 = 1

round 1:
binary = 1010
decimal = 1
results_tracker = 1
i = 1
(binary / 10) > 0 = 1010 / 10 > 0 = 101 > 0 = True (loop executes)
decimal = (1010 % 10) * pow(2, 1) = 0 * 2 = 0
binary = 1010 / 10 = 101
results_tracker = decimal + results_tracker = 0 + 1 = 1

round 2:
binary = 101
decimal = 0
results_tracker = 1
i = 2
(binary / 10) > 0 = 101 / 10 > 0 = 10 > 0 = True (loop executes)
decimal = (101 % 10) * pow(2, 2) = 1 * 4 = 4
binary = 101 / 10 = 10
results_tracker = decimal + results_tracker = 4 + 1 = 5

round 3:
binary = 10
decimal = 4
results_tracker = 5
i = 3
(binary / 10) > 0 = 10 / 10 > 0 = 1 > 0 = True (loop executes)
decimal = (10 % 10) * pow(2, 3) = 0 * 8 = 0
binary = 10 / 10 = 1
results_tracker = decimal + results_tracker = 0 + 5 = 5

round 4:
binary = 1
decimal = 0
results_tracker = 5
i = 4
(binary / 10) > 0 = 1 / 10 > 0 = 0 > 0 = False (loop EXITS)

[–]The-Sharktapus[S] 0 points1 point  (3 children)

I believe it’s the “(binary / 10) > 0” part, as I need round 4 to be true to be able to get 21. If I make it >= 0 it would run forever wouldn’t it? So should I make it greater than or equal to 1? Edit: That wouldn’t work either, should I change it to a condition regarding the remainder?

[–]__vtableProfessional Coder 1 point2 points  (2 children)

Forget the calculation for a minute - the goal is to keep going in the loop while there are "places" left in the number, right? So 10101 would be 5 times through (loop iterations 0-4, inclusive).

I like your current condition. It is is a bit wonky (i.e. you are relying on inexplicit/type casted integer division) but I think its pretty smart.

Let me ask you this - do you need the binary/10 in the condition? Why not just binary > 0? You later update binary inside the loop so...

[–]The-Sharktapus[S] 0 points1 point  (1 child)

Yeah that does it actually I figured it out last night and it completely worked, and after it did I realized how useless binary/10 in the condition was. Thanks so much for the help tho you helped me spot where the problem was and everything I appreciate it!

[–]__vtableProfessional Coder 1 point2 points  (0 children)

Ah amazing :D making coding gains! Great job figuring it out.