C++ Help with writing a program to convert Base 10 (Decimal) to Base 2 (Binary)

Saint Nick

Lifer
Jan 21, 2005
17,722
6
81
I don't know where to start, and yes it is an assignment for a class of mine. I need some tips, I don't need the answer or a complete thing of code. I read through the crap on Wikipedia about it (click here for that). I thought I had it, but it didn't work out the right way. We're supposed to enter numbers up to 2047 (11111111111), but we don't have to write code that checks for that. And it has to output the base 10 numbers to an 11 digit binary number.

I think I have the right start. The

cout << div << "<- div :: mod ->" << mod << endl;

line is just a debugging line I made to show the outputs.

Oh yeah, we can't use while/do statements because we haven't gotten that far. We are limited to if and if/else
 

imported_obsidian

Senior member
May 4, 2004
438
0
0
You keep dividing the number by 2. The remainder (either a 1 or 0) is part of the binary sequence. The whole number left is what you keep dividing by 2 until it equals zero.
 

imported_obsidian

Senior member
May 4, 2004
438
0
0
Here is an example. Start with 35.

35/2 = 17 R1

1

17/2 = 8 R1

11

8/2 = 4 R0

011

4/2 = 2 R0

0011

2/2 = 1 R0

00011

1/2 = 0 R1

100011
 

Saint Nick

Lifer
Jan 21, 2005
17,722
6
81
Okay, I wrote this up, but its putting a "0" as the last digit when I enter "2047" as the input. It should be a "1".

Nevermind, the above code I was talking about was junk.

How come this code doesn't produce what you are producing, obsidian? Its producing "1" for remainder on ((35 % 2) % 2) % 2
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
think about a 2-binary-digt number 3 = 11

Work through your logic.

div = entry / 2;
n1 = div % 2 ;

div = div / 2 ;
n2 = div % 2 ;

that is:
div = 3/2 = 1
n1 = div % 2 = 1

div = 1/2 = 0
n2 = div % 2 = 0

Try 0, 1, 2, 3 as entry and get your logic to work right for all 4 values.

Also have you learned for, do, while loops yet?
 

imported_obsidian

Senior member
May 4, 2004
438
0
0
Originally posted by: jndietz
Okay, I wrote this up, but its putting a "0" as the last digit when I enter "2047" as the input. It should be a "1".

Nevermind, the above code I was talking about was junk.

How come this code doesn't produce what you are producing, obsidian? Its producing "1" for remainder on ((35 % 2) % 2) % 2
Because that logic isn't what I am doing. You are using the modulus to calculate the next modulus. You were on the right track before. You just need to calculate the modulus before advancing dev.
 

Saint Nick

Lifer
Jan 21, 2005
17,722
6
81
Originally posted by: obsidian
Originally posted by: jndietz
Okay, I wrote this up, but its putting a "0" as the last digit when I enter "2047" as the input. It should be a "1".

Nevermind, the above code I was talking about was junk.

How come this code doesn't produce what you are producing, obsidian? Its producing "1" for remainder on ((35 % 2) % 2) % 2
Because that logic isn't what I am doing. You are using the modulus to calculate the next modulus. You were on the right track before. You just need to calculate the modulus before advancing dev.

I think I figured out what you were saying now, but I have to rewrite a ton of crap because I confused the piss out of myself.

If I can't get this to work, I'm going to go back to my old logic that DaveSimmons is talking about.

I shouldn't have to use if or if/else statements, should I?
 

imported_obsidian

Senior member
May 4, 2004
438
0
0
Originally posted by: jndietz
I think I figured out what you were saying now, but I have to rewrite a ton of crap because I confused the piss out of myself.

If I can't get this to work, I'm going to go back to my old logic that DaveSimmons is talking about.

I shouldn't have to use if or if/else statements, should I?
No, I made the program and no if/else statements are needed. You were on the right track before. You just needed to do:

n1 = dev % 2;
dev = dev / 2;
n2 = dev % 2;
...

Not the other way around.
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
Originally posted by: obsidian
No, I made the program and no if/else statements are needed. You were on the right track before. You just needed to do:

n1 = dev % 2;
dev = dev / 2;
n2 = dev % 2;
...

Not the other way around.
. . . which "running" the program on paper with 2-digit numbers would hopefully have showed you.

Sometimes the best way to find a design flaw is to pick the simpler cases and run through the logic using Brain 1.0 instead of the computer.
 

tfinch2

Lifer
Feb 3, 2004
22,114
1
0
Originally posted by: DaveSimmons
Originally posted by: obsidian
No, I made the program and no if/else statements are needed. You were on the right track before. You just needed to do:

n1 = dev % 2;
dev = dev / 2;
n2 = dev % 2;
...

Not the other way around.
. . . which "running" the program on paper with 2-digit numbers would hopefully have showed you.

Sometimes the best way to find a design flaw is to pick the simpler cases and run through the logic using Brain 1.0 instead of the computer.

I thought Brain was still in beta.. :p
 

JustAnAverageGuy

Diamond Member
Aug 1, 2003
9,057
0
76
It's a pity you can't use loops, otherwise this would be really easy.

I suppose you could just copy and paste the inside of that loop 11 times and have a nested if statement check to see if it was equal to one yet every time, but that would seem rather silly.

Of course, you would have to have access to a String datatype for the below function to work correctly :p
 

itachi

Senior member
Aug 17, 2004
390
0
0
i got an idea..

string toBinary(int num, int bits) {
char bit[2] = '0', '1';
string rval(bits);
for(int i = 0; i < bits; i++) if(i != 0) rval[ i ] = bit[(num >> i) & 1];
}

int main() {
...
cout << "#include <iostream>" << endl << "using namespace std;" << endl << endl;
cout << "int main () {" << endl;
// print some input handling code

for(int i = 0; i < 2048; i++) {
if(i != 0) cout << "else ";
cout << "if(num == " << i << ") cout << \"";
cout << toBinary(i, 11) << "\";" << endl;
}
cout << "return 0; // And this is why the loop was invented." << endl << "}" << endl;

return 0;
}
---
have the output redirected to a file and hand that in.. 2048 if statements hahah.
 

Saint Nick

Lifer
Jan 21, 2005
17,722
6
81
I think I got it figured out using my original logic guys, thanks. Here's the source for anyone interested:

Thank you all for your help, it was much appreciated!!!
 

Kyteland

Diamond Member
Dec 30, 2002
5,747
1
81
It's too bad you cant use for loops or recursion. That makes the problem a lot more general.

I had to write something similar for work. It converts any integer type in to a binary string.
 

Kyteland

Diamond Member
Dec 30, 2002
5,747
1
81
This does exactly what you want, but probably doesn't meet your prof's requirements. Have you learned recursion yet? It makes it a simple 1 line function.

Edit: Added a commented function so that it's clear what's going on.
 

Saint Nick

Lifer
Jan 21, 2005
17,722
6
81
Nope, we haven't touched recursion yet. One of the honors kids asked about it, but the professor said we would get to that later.