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

Discussion in 'Software for Windows' started by Saint Nick, Jan 26, 2006.

  1. Saint Nick

    Saint Nick Lifer

    Joined:
    Jan 21, 2005
    Messages:
    17,723
    Likes Received:
    2
    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
     
  2. Loading...

    Similar Threads - writing program convert Forum Date
    Program to help college students write papers Software for Windows Jun 3, 2014
    Trying to give write access to a program Software for Windows Aug 22, 2008
    looing for a the best AVI to DVD writing program Software for Windows Apr 3, 2008
    Free CD and DVD Writing (Burning) Programs Software for Windows Jan 6, 2007
    need help writing small java program Software for Windows Dec 10, 2006

  3. imported_obsidian

    imported_obsidian Senior member

    Joined:
    May 4, 2004
    Messages:
    438
    Likes Received:
    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.
     
  4. imported_obsidian

    imported_obsidian Senior member

    Joined:
    May 4, 2004
    Messages:
    438
    Likes Received:
    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
     
  5. Saint Nick

    Saint Nick Lifer

    Joined:
    Jan 21, 2005
    Messages:
    17,723
    Likes Received:
    2
    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
     
  6. DaveSimmons

    DaveSimmons Elite Member

    Joined:
    Aug 12, 2001
    Messages:
    39,748
    Likes Received:
    309
    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?
     
  7. imported_obsidian

    imported_obsidian Senior member

    Joined:
    May 4, 2004
    Messages:
    438
    Likes Received:
    0
    You need to calculate the modulus part first.
     
  8. imported_obsidian

    imported_obsidian Senior member

    Joined:
    May 4, 2004
    Messages:
    438
    Likes Received:
    0
    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.
     
  9. Saint Nick

    Saint Nick Lifer

    Joined:
    Jan 21, 2005
    Messages:
    17,723
    Likes Received:
    2
    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?
     
  10. Saint Nick

    Saint Nick Lifer

    Joined:
    Jan 21, 2005
    Messages:
    17,723
    Likes Received:
    2
    DaveSimmons: nope, we haven't discussed those in class yet...
     
  11. imported_obsidian

    imported_obsidian Senior member

    Joined:
    May 4, 2004
    Messages:
    438
    Likes Received:
    0
    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.
     
  12. DaveSimmons

    DaveSimmons Elite Member

    Joined:
    Aug 12, 2001
    Messages:
    39,748
    Likes Received:
    309
    . . . 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.
     
  13. tfinch2

    tfinch2 Lifer

    Joined:
    Feb 3, 2004
    Messages:
    22,120
    Likes Received:
    0
    I thought Brain was still in beta.. :p
     
  14. JustAnAverageGuy

    JustAnAverageGuy Diamond Member

    Joined:
    Aug 1, 2003
    Messages:
    9,057
    Likes Received:
    0
    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
     
  15. xtknight

    xtknight Elite Member

    Joined:
    Oct 15, 2004
    Messages:
    12,974
    Likes Received:
    0
    If you can't use loops just use goto. Is that cheating? :p
     
  16. itachi

    itachi Senior member

    Joined:
    Aug 17, 2004
    Messages:
    390
    Likes Received:
    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.
     
  17. Saint Nick

    Saint Nick Lifer

    Joined:
    Jan 21, 2005
    Messages:
    17,723
    Likes Received:
    2
    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!!!
     
  18. Kyteland

    Kyteland Diamond Member

    Joined:
    Dec 30, 2002
    Messages:
    5,747
    Likes Received:
    1
    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.
     
  19. Kyteland

    Kyteland Diamond Member

    Joined:
    Dec 30, 2002
    Messages:
    5,747
    Likes Received:
    1
    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.
     
  20. Saint Nick

    Saint Nick Lifer

    Joined:
    Jan 21, 2005
    Messages:
    17,723
    Likes Received:
    2
    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.