Thread: find the digits

  1. #1
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463

    find the digits

    I want to transform a number into a vector of it's digits. For example, 123 will be 1 2 3.
    I came up with this code:

    Code:
    void transform(const uint & x , deque<uint> & output)
    {
      if ( x != 0 )
        {
          uint x_stripped = (x/10)*10;
          uint digit = x - x_stripped;
          output.push_front(digit);
          transform(x_stripped / 10, output);
        }
    }
    My question is, is there better way to do this? taking x and divide then multiply by 10 is a bit irksome for me.
    Last edited by nimitzhunter; 01-05-2011 at 02:35 PM.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You could try:
    Code:
    void transform(const uint& x , deque<uint>& output)
    {
        do
        {
            output.push_front(x % 10);
            x /= 10;
        }
        while (x != 0);
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Quote Originally Posted by laserlight View Post
    You could try:
    Code:
    void transform(const uint& x , deque<uint>& output)
    {
        do
        {
            output.push_front(x % 10);
            x /= 10;
        }
        while (x != 0);
    }
    laserlight, x/=10 wouldn't work because x is const. Maybe I could create a temp variable instead.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by nimitzhunter
    laserlight, x/=10 wouldn't work because x is const. Maybe I could create a temp variable instead.
    Oh yeah, I did not notice that. Just change to use pass by value since passing by const reference is not likely to gain you anything here.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Quote Originally Posted by laserlight View Post
    Oh yeah, I did not notice that. Just change to use pass by value since passing by const reference is not likely to gain you anything here.
    Thanks alot for your help, laserlight. It works perfectly :-D.
    "All that we see or seem
    Is but a dream within a dream." - Poe

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. Counting letters and digits
    By FeNCinGeR in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2006, 11:39 AM
  3. how do u find 2nd largest number??
    By juancardenas in forum C Programming
    Replies: 8
    Last Post: 02-14-2003, 08:28 AM
  4. Odd/Even Digits in a Number-Help!
    By ProgrammingDlux in forum C++ Programming
    Replies: 2
    Last Post: 02-27-2002, 10:39 PM
  5. Max Digits
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 02-09-2002, 06:28 AM