|
|
 |
01-26-2006, 04:12 PM
|
#1
|
|
Lifer
Join Date: Jan 2005
Location: Land of Ooze
Posts: 17,327
|
C++ Help with writing a program to convert Base 10 (Decimal) to Base 2 (Binary)
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
|
|
|
01-26-2006, 04:23 PM
|
#2
|
|
Senior Member
Join Date: May 2004
Posts: 438
|
C++ Help with writing a program to convert Base 10 (Decimal) to Base 2 (Binary)
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.
|
|
|
01-26-2006, 04:27 PM
|
#3
|
|
Senior Member
Join Date: May 2004
Posts: 438
|
C++ Help with writing a program to convert Base 10 (Decimal) to Base 2 (Binary)
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
|
|
|
01-26-2006, 04:29 PM
|
#4
|
|
Lifer
Join Date: Jan 2005
Location: Land of Ooze
Posts: 17,327
|
C++ Help with writing a program to convert Base 10 (Decimal) to Base 2 (Binary)
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
|
|
|
01-26-2006, 04:35 PM
|
#5
|
|
Elite Member
Join Date: Aug 2001
Location: Bellevue, WA
Posts: 35,483
|
C++ Help with writing a program to convert Base 10 (Decimal) to Base 2 (Binary)
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?
|
|
|
01-26-2006, 04:36 PM
|
#6
|
|
Senior Member
Join Date: May 2004
Posts: 438
|
C++ Help with writing a program to convert Base 10 (Decimal) to Base 2 (Binary)
You need to calculate the modulus part first.
|
|
|
01-26-2006, 04:37 PM
|
#7
|
|
Senior Member
Join Date: May 2004
Posts: 438
|
C++ Help with writing a program to convert Base 10 (Decimal) to Base 2 (Binary)
Quote:
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.
|
|
|
01-26-2006, 04:41 PM
|
#8
|
|
Lifer
Join Date: Jan 2005
Location: Land of Ooze
Posts: 17,327
|
C++ Help with writing a program to convert Base 10 (Decimal) to Base 2 (Binary)
Quote:
Originally posted by: obsidian
Quote:
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?
|
|
|
01-26-2006, 04:45 PM
|
#9
|
|
Lifer
Join Date: Jan 2005
Location: Land of Ooze
Posts: 17,327
|
C++ Help with writing a program to convert Base 10 (Decimal) to Base 2 (Binary)
DaveSimmons: nope, we haven't discussed those in class yet...
|
|
|
01-26-2006, 04:50 PM
|
#10
|
|
Senior Member
Join Date: May 2004
Posts: 438
|
C++ Help with writing a program to convert Base 10 (Decimal) to Base 2 (Binary)
Quote:
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.
|
|
|
01-26-2006, 05:29 PM
|
#11
|
|
Elite Member
Join Date: Aug 2001
Location: Bellevue, WA
Posts: 35,483
|
C++ Help with writing a program to convert Base 10 (Decimal) to Base 2 (Binary)
Quote:
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.
|
|
|
01-26-2006, 05:58 PM
|
#12
|
|
Lifer
Join Date: Feb 2004
Posts: 21,489
|
C++ Help with writing a program to convert Base 10 (Decimal) to Base 2 (Binary)
Quote:
Originally posted by: DaveSimmons
Quote:
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..
__________________
unban ^[a-zA-Z0-9]+$
|
|
|
01-26-2006, 06:58 PM
|
#13
|
|
Diamond Member
Join Date: Jul 2003
Location: Indiana
Posts: 9,057
|
C++ Help with writing a program to convert Base 10 (Decimal) to Base 2 (Binary)
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
__________________
"There are two sides to every story, and then there is the truth." - Lexisnexus80
|
|
|
01-26-2006, 07:19 PM
|
#14
|
|
Elite Member
Join Date: Oct 2004
Posts: 12,974
|
C++ Help with writing a program to convert Base 10 (Decimal) to Base 2 (Binary)
If you can't use loops just use goto. Is that cheating?
|
|
|
01-26-2006, 07:59 PM
|
#15
|
|
Senior Member
Join Date: Aug 2004
Posts: 390
|
C++ Help with writing a program to convert Base 10 (Decimal) to Base 2 (Binary)
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.
|
|
|
01-26-2006, 09:01 PM
|
#16
|
|
Lifer
Join Date: Jan 2005
Location: Land of Ooze
Posts: 17,327
|
C++ Help with writing a program to convert Base 10 (Decimal) to Base 2 (Binary)
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!!!
|
|
|
01-26-2006, 09:26 PM
|
#17
|
|
Diamond Member
Join Date: Dec 2002
Posts: 5,747
|
C++ Help with writing a program to convert Base 10 (Decimal) to Base 2 (Binary)
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.
__________________
Don't assume malice for what stupidity can explain.
|
|
|
01-26-2006, 09:42 PM
|
#18
|
|
Diamond Member
Join Date: Dec 2002
Posts: 5,747
|
C++ Help with writing a program to convert Base 10 (Decimal) to Base 2 (Binary)
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.
__________________
Don't assume malice for what stupidity can explain.
|
|
|
01-26-2006, 10:09 PM
|
#19
|
|
Lifer
Join Date: Jan 2005
Location: Land of Ooze
Posts: 17,327
|
C++ Help with writing a program to convert Base 10 (Decimal) to Base 2 (Binary)
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.
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 09:02 AM.
|