Go Back   AnandTech Forums > Software > Software for Windows

Forums
· Hardware and Technology
· CPUs and Overclocking
· Motherboards
· Video Cards and Graphics
· Memory and Storage
· Power Supplies
· Cases & Cooling
· SFF, Notebooks, Pre-Built/Barebones PCs
· Networking
· Peripherals
· General Hardware
· Highly Technical
· Computer Help
· Home Theater PCs
· Consumer Electronics
· Digital and Video Cameras
· Mobile Devices & Gadgets
· Audio/Video & Home Theater
· Software
· Software for Windows
· All Things Apple
· *nix Software
· Operating Systems
· Programming
· PC Gaming
· Console Gaming
· Distributed Computing
· Security
· Social
· Off Topic
· Politics and News
· Discussion Club
· Love and Relationships
· The Garage
· Health and Fitness
· Merchandise and Shopping
· For Sale/Trade
· Hot Deals with Free Stuff/Contests
· Black Friday 2014
· Forum Issues
· Technical Forum Issues
· Personal Forum Issues
· Suggestion Box
· Moderator Resources
· Moderator Discussions
   

Reply
 
Thread Tools
Old 01-26-2006, 04:12 PM   #1
Saint Nick
Lifer
 
Saint Nick's Avatar
 
Join Date: Jan 2005
Location: Land of Ooze
Posts: 17,695
Default 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
__________________
Be kind; everyone is in the midst of a struggle. | heatware (64-0-0)
Saint Nick is offline   Reply With Quote
Old 01-26-2006, 04:23 PM   #2
imported_obsidian
Senior Member
 
Join Date: May 2004
Posts: 438
Default 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.
imported_obsidian is offline   Reply With Quote
Old 01-26-2006, 04:27 PM   #3
imported_obsidian
Senior Member
 
Join Date: May 2004
Posts: 438
Default 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
imported_obsidian is offline   Reply With Quote
Old 01-26-2006, 04:29 PM   #4
Saint Nick
Lifer
 
Saint Nick's Avatar
 
Join Date: Jan 2005
Location: Land of Ooze
Posts: 17,695
Default 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
__________________
Be kind; everyone is in the midst of a struggle. | heatware (64-0-0)
Saint Nick is offline   Reply With Quote
Old 01-26-2006, 04:35 PM   #5
DaveSimmons
Elite Member
 
Join Date: Aug 2001
Location: Bellevue, WA
Posts: 37,234
Default 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?
DaveSimmons is online now   Reply With Quote
Old 01-26-2006, 04:36 PM   #6
imported_obsidian
Senior Member
 
Join Date: May 2004
Posts: 438
Default C++ Help with writing a program to convert Base 10 (Decimal) to Base 2 (Binary)

You need to calculate the modulus part first.
imported_obsidian is offline   Reply With Quote
Old 01-26-2006, 04:37 PM   #7
imported_obsidian
Senior Member
 
Join Date: May 2004
Posts: 438
Default 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.
imported_obsidian is offline   Reply With Quote
Old 01-26-2006, 04:41 PM   #8
Saint Nick
Lifer
 
Saint Nick's Avatar
 
Join Date: Jan 2005
Location: Land of Ooze
Posts: 17,695
Default 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?
__________________
Be kind; everyone is in the midst of a struggle. | heatware (64-0-0)
Saint Nick is offline   Reply With Quote
Old 01-26-2006, 04:45 PM   #9
Saint Nick
Lifer
 
Saint Nick's Avatar
 
Join Date: Jan 2005
Location: Land of Ooze
Posts: 17,695
Default 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...
__________________
Be kind; everyone is in the midst of a struggle. | heatware (64-0-0)
Saint Nick is offline   Reply With Quote
Old 01-26-2006, 04:50 PM   #10
imported_obsidian
Senior Member
 
Join Date: May 2004
Posts: 438
Default 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.
imported_obsidian is offline   Reply With Quote
Old 01-26-2006, 05:29 PM   #11
DaveSimmons
Elite Member
 
Join Date: Aug 2001
Location: Bellevue, WA
Posts: 37,234
Default 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.
DaveSimmons is online now   Reply With Quote
Old 01-26-2006, 05:58 PM   #12
tfinch2
Lifer
 
tfinch2's Avatar
 
Join Date: Feb 2004
Posts: 22,088
Default 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]+$
tfinch2 is offline   Reply With Quote
Old 01-26-2006, 06:58 PM   #13
JustAnAverageGuy
Diamond Member
 
Join Date: Jul 2003
Location: Indiana
Posts: 9,057
Default 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
JustAnAverageGuy is offline   Reply With Quote
Old 01-26-2006, 07:19 PM   #14
xtknight
Elite Member
 
xtknight's Avatar
 
Join Date: Oct 2004
Posts: 12,974
Default 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?
__________________
Main Rig (Ubuntu Linux 9.04)
The LCD Thread : LCD Resource
NEC 20WMGX2/LCD2690WUXi Owner
xtknight is offline   Reply With Quote
Old 01-26-2006, 07:59 PM   #15
itachi
Senior Member
 
Join Date: Aug 2004
Posts: 390
Default 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.
itachi is offline   Reply With Quote
Old 01-26-2006, 09:01 PM   #16
Saint Nick
Lifer
 
Saint Nick's Avatar
 
Join Date: Jan 2005
Location: Land of Ooze
Posts: 17,695
Default 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!!!
__________________
Be kind; everyone is in the midst of a struggle. | heatware (64-0-0)
Saint Nick is offline   Reply With Quote
Old 01-26-2006, 09:26 PM   #17
Kyteland
Diamond Member
 
Kyteland's Avatar
 
Join Date: Dec 2002
Posts: 5,747
Default 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.
Kyteland is offline   Reply With Quote
Old 01-26-2006, 09:42 PM   #18
Kyteland
Diamond Member
 
Kyteland's Avatar
 
Join Date: Dec 2002
Posts: 5,747
Default 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.
Kyteland is offline   Reply With Quote
Old 01-26-2006, 10:09 PM   #19
Saint Nick
Lifer
 
Saint Nick's Avatar
 
Join Date: Jan 2005
Location: Land of Ooze
Posts: 17,695
Default 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.
__________________
Be kind; everyone is in the midst of a struggle. | heatware (64-0-0)
Saint Nick is offline   Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 04:23 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.