Thread: Counting number of occurence of digits in a number

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by oogabooga
    3. You could store all the counts in a single integer by interpreting each decimal position as a count. This limits the max count of any digit to 9. This could be increased by using another base.
    Interesting idea, but what's the point? After all, a variant of this is to use the integer as a bitset, but without any special requirements to the contrary, I think that this whole "cannot use array" business is misguided.

    I can understand requiring that the input number not be stored in an array as that teaches the learner how to break an integer into individual digits (as indicated by the operations listed), but beyond that it sounds like fluff to me, unless the teacher really intended for the whole process to be repeated for each digit (as grumpy seems to think is fine, and given that there are only ten digits, I suppose I can concede that it is).
    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

  2. #17
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by laserlight View Post
    Interesting idea, but what's the point? After all, a variant of this is to use the integer as a bitset, but without any special requirements to the contrary, I think that this whole "cannot use array" business is misguided.

    I can understand requiring that the input number not be stored in an array as that teaches the learner how to break an integer into individual digits (as indicated by the operations listed), but beyond that it sounds like fluff to me, unless the teacher really intended for the whole process to be repeated for each digit (as grumpy seems to think is fine, and given that there are only ten digits, I suppose I can concede that it is).
    Clearly the array of counts is the proper solution. Anything else is more work for both the programmer and the machine.

    I think your take on the situation is accurate. The OP is most likely mistaken. But then again, there's no fathoming the depths of assignment stupidity.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Counting the number of inegers in a number
    By jburn09 in forum C Programming
    Replies: 8
    Last Post: 09-19-2012, 12:52 PM
  2. Replies: 7
    Last Post: 12-04-2011, 10:43 PM
  3. Counting number of digits in a variable
    By Saeid87 in forum C Programming
    Replies: 6
    Last Post: 06-05-2009, 01:13 PM
  4. Counting number of digits?
    By scatterice in forum C Programming
    Replies: 7
    Last Post: 05-12-2009, 01:30 PM
  5. Number of digits in a decimal number
    By maverix in forum C Programming
    Replies: 7
    Last Post: 11-04-2007, 12:12 PM