tags:

views:

148

answers:

6

I'm just mucking around with C as a learner, and wrote this little function...

char *getPlaceSuffix(int number) {

    static char *suffixes[] = {"st", "nd", "rd", "th"};

    if (number >= 11 && number <= 13) {
        return suffixes[3];
    } else {
        while (number > 10) {
            number -= 10;   
        }

        if (number >= 1 && number <= 3) {
            return suffixes[number - 1];
        } else {
            return suffixes[3];
        }               
    }
}   

I tweeted the link, and Konrad Rudolph informed me my method of getting the least significant number was O(n) and not very efficient.

Unfortunately, it’s O(n) for very large number – to make it O(logn), adjust the while loop for higher powers of 10 …

Source

I'm not too familiar with Big O notation, but I get the gist that O(n) isn't too efficient?

As you can see from my code sample, I deduct 10 until the number is one digit long, so I can compare it to see which suffix is appropriate. I had a quick play with division and modulus but couldn't figure it out.

So, my question is, what is the best way to get the least significant digit in a number?

I'm still learning, so please go easy on me :)

Thanks!

+14  A: 
number % 10

should work.

mouviciel
Whoops - a bit embarrassing on my behalf!
alex
+5  A: 

"had a quick play with division and modulus but couldn't figure it out."

 number = number % 10

will do it

And if we're really bothered about efficiency for this code (why?) then

char *getPlaceSuffix(int number) {

    static char *suffixes[] = {"th", "st", "nd", "rd", "th",  "th", "th", "th", "th", "th"};
    int h = number %100;
    int d = number %10
    return (h == 11 or h == 12 or h == 13)? suffixes[0]:suffixes[d];
} 
Paul
“why?” – is that a trick question? If not: there’s a big difference between an algorithm that performs in O(n) and one that performs in O(1). Just try calling the code in a loop with high numbers and observe the UI’s responsiveness go under. As for your optimization, apart from the fact that it only shaves off a constant factor (as opposed to reducing the asymptotic complexity), it will yield the wrong result for inputs 11, 12 and 13.
Konrad Rudolph
@Konrad: wrong results? The complaints department is on the 11st floor, take it up with them. It'll be the 112nd complaint they've had this week.
Steve Jessop
"Just try calling the code in a loop " Yes, but how likely is that? the OP is just learning the language, and striving for maximal efficiency here is Premature Optimization, IMO. n%10 is O(1) on nearly all processors, in any case, so there is no "asymptopic efficiency" to worry about (as long as "number" is an int).
Paul
Got me on the 11/12/13,though :) Fixed (also fixed for one-hundred-and-eleventh, etc)
Paul
@Paul: I’d argue strongly that using an asymptotically better algorithm that isn’t conceptually any harder to implement (nor longer) is *not* a premature optimization by any sensible definition of the term, in particular not when designing a general-purpose function. An optimization is premature only if it has a trade-off.
Konrad Rudolph
n%10 is O(1) for int. That's as good as you're going to get.
Paul
Thanks for your answer Paul. I'm just curious - is repeating yourself like that (`th`...) a C idiom? My background in other languages has always had me following the DRY principle.
alex
I don't think it's C or non-C really. I have a slight tendency to prefer data-driven stuff to imperative stuff, but mostly in this case it's a reduction in complexity.
Paul
@Paul Thanks for getting back to me. I am rewriting my function now :)
alex
+3  A: 

Modulus:

number = number % 10;
kanaka
+4  A: 
Steve Jessop
I forget what modulus I tried, but it turns out [I've already used modulus in my C functions](http://alexanderdickson.com/blog/2010/08/convert-any-number-to-any-base-in-c/). I must of been having one of those moments were I forgot to use my brain properly :P. Thanks very much for your answer.
alex
A: 

Can you just take the number and mod it by 10 and that's your least significant number? When you are deducting by 10 until you can't anymore, you're exactly what modulus was meant for.

shoebox639
A: 

Another way will be to sprintf to write the number to a buffer, read the last char.

Jayan