Thread: converting decimal to binary in stack

  1. #1
    Registered User
    Join Date
    May 2009
    Location
    kuwait
    Posts
    9

    converting decimal to binary in stack

    hello .. how can i write a function that convert a decimal number to binary in a stack?
    i need a simple implementation
    1)read the number
    2) loop fpr checking if the number >0
    3)print it

    pop and push are allowed
    thanks alot

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Do you know how to do this without a stack?

    Assuming you do, having extracted a single bit, the only thing you can do with an otherwise empty stack is push()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2009
    Location
    kuwait
    Posts
    9
    can you help me with a code "with stack" please?
    assume that my function prototype is

    void convert()

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Are you using std::stack<int> as your stack, or do you have to write your own.

    > can you help me with a code "with stack" please?
    Since the interesting part is only two lines in a loop, not yet (not at least until you've shown a bit of effort). Posting something which doesn't work (and I don't mean posting any old rubbish either).

    If you know how to do it normally, then think about the "it comes out backwards" problem, which is where the stack comes in (google LIFO)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    34
    Understand what >> and & operators do. Then you'll be pretty much finished with the problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting binary string to decimal
    By Sharke in forum C Programming
    Replies: 3
    Last Post: 03-30-2009, 12:18 AM
  2. binary - decimal
    By curlious in forum C++ Programming
    Replies: 5
    Last Post: 07-31-2003, 02:25 PM
  3. decimal to binary
    By kurz7 in forum C Programming
    Replies: 8
    Last Post: 07-10-2003, 12:03 AM
  4. What am I doing wrong, stack?
    By TeenyTig in forum C Programming
    Replies: 2
    Last Post: 05-27-2002, 02:12 PM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM