Thread: Dissecting numbers

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by laserlight
    I agree. In this case, your version is not what people would be used to,
    But they should be familiar with it, or at least have seen it a few times. Looping between 0 and n-1 is a basic operation, and the most concise/efficient ways of doing it are
    Code:
    for (int i=0; i != n; i++) {}
    upwards, and
    Code:
    for (int i=n; i--;) {}
    downwards. These also allow using an unsigned index, and have the clarity of referring to n instead of n-1. This is a very small amount of information to store in one's bag of tricks, and doesn't require thought once one knows it. (I think the "premature optimization" quote refers to something that requires effort.) But most people, even experienced ones, won't consider doing it unless they've seen it in someone else's code. This is one of those cases where the best tool for the job has fallen through the cracks because of the self-fulfilling prophecy of everyone being afraid of confusing everyone else. The danger of temporary confusion is small compared to the possible benefit of learning an idiom which can be used routinely. This applies to maintaining other people's code as well as learning to code. I could have avoided using the idiom on the grounds that it's not needed on efficiency or range grounds here, but then jmajeremy probably never would have seen it again.

  2. #2
    Registered User
    Join Date
    Nov 2006
    Posts
    86
    you could ask the user to enter that number in a character array, and just do - 48.

    a[0] = 1 , to get the number , do a[0] - 48
    a[1] = 2 " "
    a[2 ] = 3, " "

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by epidemic
    you could ask the user to enter that number in a character array, and just do - 48.

    a[0] = 1 , to get the number , do a[0] - 48
    a[1] = 2 " "
    a[2 ] = 3, " "
    Don't use magic constants... If a[0] == '1' to get a number use a[0] - '0'
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with Rational Numbers (C++)
    By cloudjc in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2008, 04:03 PM
  2. Comparing numbers to a list of numbers held in a text file
    By jmajeremy in forum C++ Programming
    Replies: 3
    Last Post: 11-06-2006, 07:56 AM
  3. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  4. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM