dividing a multi-digit into single-digits

This is a discussion on dividing a multi-digit into single-digits within the C++ Programming forums, part of the General Programming Boards category; i am trying to figure out how to make a multi-digit number be broken down into single-digits. For example, if ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Location
    Bethpage, NY, USA
    Posts
    1

    dividing a multi-digit into single-digits

    i am trying to figure out how to make a multi-digit number be broken down into single-digits.

    For example, if a=534, how can i make a program that takes that variable and makes the following information true:

    a=534
    b=5
    c=3
    d=4

    i need a line of code that can break it down, if that is possible

    thanks..........

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    I think you can do:

    string number = 123;
    string a = number[0];
    string b = number[1];
    string c = number[2];

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Assigning an integer to a string doesn't work in C++ - at least not in my MS Visual Studio .Net using VC7.

    There are probably three dozen different ways to do this, but one is as h3ro suggests, to convert the number to a string (sstream may help with this), for example.

    Another way is to use modulo and divide by ten a number of times (until there's "nothing left"),

    --
    Mats

  4. #4
    I Write C++ Apps, Sue Me.
    Join Date
    Feb 2006
    Location
    In My Computer
    Posts
    44
    I would suggest h3ro's way. It seems to be the easiest.

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    85
    But don't you need data declared as a string to go by h3ro's way?

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Yes, you can do that with a stringstream. However, I'd go with modulo and division.

    1234 % 10 = 4
    1234 / 10 = 123
    123 % 10 = 3
    123 / 10 = 12
    ...

  7. #7
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by Daved View Post
    Yes, you can do that with a stringstream. However, I'd go with modulo and division.

    1234 % 10 = 4
    1234 / 10 = 123
    123 % 10 = 3
    123 / 10 = 12
    ...
    Wouldn't this also be alot faster than using a std::string and stringstreams?

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    I highly doubt speed would matter in this case. Clear and correct code would be more important things to aim for.

    It is also quite possible that this is an assignment of some sort, and my guess would be that the math way would be what was intended rather than the string way, if it matters.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Neo1 View Post
    Wouldn't this also be alot faster than using a std::string and stringstreams?
    The by far most calculation intensive part of splitting a number into a string is the divide instruction - and if the compiler isn't VERY clever, you'll get two of them, one for the modulo and one for the divide. You can do MANY regular instructions for one divide operation (on a AMD K8 processor, this is still a 12-cycle operation - on a 68000 processor of old, it would take around 150 clock-cycles to do a divide, and the result would only be 16 bits too!). 12 cycles can do 36 or so normal single-cycle instructions on a K8 (which is most of the other instructions in the x86 instruction set).

    --
    Mats

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    div() and ldiv() from <cstdlib> can be used to explicitly compute the quotient and remainder simultaneously (at least if the processor is capable of it, anyway).

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by robatino View Post
    div() and ldiv() from <cstdlib> can be used to explicitly compute the quotient and remainder simultaneously (at least if the processor is capable of it, anyway).
    Good point. I'm not aware of any integer divide-capable processor that doesn't also give the remainder as a side-effect - it is sort of trivial to calculate that when you do the divide - otherwise one would have to implement a separate instruction "mod" that does that, and it's not any more efficient. The same applies, by the way, to the more common versions of integer divide algorithms used on processors without hardware for divide.

    I tested with Visual Studio .Net, and in my test, the compiler did only one div instruction for both the divide and the modulo at once. I believe GCC does the same, which covers most code produced... :-)


    --
    Mats

  12. #12
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,263
    Quote Originally Posted by matsp View Post
    and if the compiler isn't VERY clever, you'll get two of them, one for the modulo and one for the divide.
    It doesn't need to be that clever, really. It's not hard to scan the intermediate code and notice that a particular subexpression is being subjected to both modulo and division. Even in non-optimizing mode, if there are no intervening instructions between the div and mod, the compiler should be able to figure it out.

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by robatino View Post
    div() and ldiv() from <cstdlib> can be used to explicitly compute the quotient and remainder simultaneously (at least if the processor is capable of it, anyway).
    There is the downside that there are versions of this function available only for certain types - div(), ldiv(), lldiv(), imaxdiv() for int, long, long long, and intmax_t, resp. (only the first two being standard C++). This means that if one wants to compute i/j and i%j where i and j are std::size_t, say, it might be better to just use i/j and i%j in close proximity and hope that it gets optimized. Is there a reason why there isn't a templated version in C++, similar to the way that functions from <cmath> such as sqrt() are templated in C++ for different floating-point types, even though in C they're only available for double?

  14. #14
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    If you go the string-way you could also use boost:lexical_cast<string>(int) instead of stringstream. I find it more convinient

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. five digit number with different digits
    By khdani in forum C Programming
    Replies: 11
    Last Post: 05-03-2009, 12:33 PM
  2. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 10:42 AM
  3. newbie programmer - needs help bad.
    By hortonheat in forum C Programming
    Replies: 17
    Last Post: 10-20-2004, 06:31 PM
  4. concatenating single chars to multi char arrays
    By TJJ in forum C Programming
    Replies: 7
    Last Post: 11-20-2003, 04:09 AM
  5. leading zero on single digit int
    By confuted in forum C++ Programming
    Replies: 2
    Last Post: 07-31-2003, 05:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21