Active Topics

 


Reply
Thread Tools
bandora's Avatar
Posts: 1,338 | Thanked: 1,055 times | Joined on Oct 2009 @ California, USA / Jordan
#11
Originally Posted by stlpaul View Post
[...]
Awesome thanks!!

Honestly I can't describe how much I appreciate all your help guys! I keep on saying this over and over again.. This is why I really love this community! Very helpful in everyway.
__________________
FarahFa.com
 

The Following User Says Thank You to bandora For This Useful Post:
Posts: 1,141 | Thanked: 781 times | Joined on Dec 2009 @ Magical Unicorn Land
#12
Originally Posted by Mentalist Traceur View Post
Oh, now if you are stuck trying to figure out what the length of "[string 2]" would be, since that's an integer/float/double variable, I would approach it like this:

You know that the amount of digits in the number determines the length.

You know in decimal counting systems, each digit is one greater multiple of 10, essentially.

For an integer, then, you can figure out how many digits it will be by dividing it by ten until it becomes zero (as C++ will floor decimal results for intergers).

For every power of ten it has, you add one more to your digits counter.

With a little bit of thinking, it should be possible for you to figure out how to convert the above into code. If you can't after a while, I can show you what I've written to do so. (Hint, either a loop, or a series of if/else-if checks with less/great -than comparisons.)

With a little more thinking, you can adapt such approaches to floats/doubles as well.

There's other ways too, of course, usually involving using other fancy C++ classes and stuff, if that's the route you want to go.
You should become a teacher, you have a great way of suggesting solutions without flat-out telling the answer. This counting function sounds exactly like an exercise I did while learning programming.

And be sure it can handle the number 0 (which still takes 1 character to print on-screen). It is an easy one to miss when you're focusing on testing bigger numbers.

Using standard library functions, depending on what you know about the input number type, a perhaps easier mathematical approach could be to use log10(i)+1 but it's still up to you to handle negatives, floats, etc.

Taking the "convert it to a string and count the characters" approach, in C++11, my everyday choice would be to simply do: to_string(i).length()

to_string is implemented in the C++ standard library as a wrapper around snprintf which then converts the result from a c-style character array and returns a std::string. So, following with woody's suggestion, you could pretty easily implement something like that yourself even in an older compiler which lacks C++11 support. This should be able to handle any type of number you throw at it.
 

The Following User Says Thank You to stlpaul For This Useful Post:
Posts: 2,225 | Thanked: 3,822 times | Joined on Jun 2010 @ Florida
#13
Originally Posted by stlpaul View Post
You should become a teacher, you have a great way of suggesting solutions without flat-out telling the answer. This counting function sounds exactly like an exercise I did while learning programming.
Why thank you. Yeah, teaching is something I wouldn't mind doing, unfortunately here in the USA at least, teaching in the general education system means a rather low income and rather long work days (which in itself I'd not mind, but it would prevent me from being able to do other things in the world that I also consider important). And trying to get a long-term teaching job at a university/college typically entails having to publish academic work in journals regularly, as I understand it.

So for the time being I 'teach' like this, in passing, when someone seems like they could use it or are interested.

Originally Posted by stlpaul View Post
And be sure it can handle the number 0 (which still takes 1 character to print on-screen). It is an easy one to miss when you're focusing on testing bigger numbers.
You are absolutely right, I completely forgot to mention that. (I did not forget it in my code when I last wrote such a function up, though.)

Originally Posted by stlpaul View Post
Using standard library functions, depending on what you know about the input number type, a perhaps easier mathematical approach could be to use log10(i)+1 but it's still up to you to handle negatives, floats, etc..
Yeah, as per my big efficiency-zealot personal flaw, I didn't think of logarithms at all, due to the fact that they are typically computationally expensive, as I understand it. But that can indeed be much more intuitive and requiring less thoughts than my proposed methods.
 
pichlo's Avatar
Posts: 6,447 | Thanked: 20,981 times | Joined on Sep 2012 @ UK
#14
Originally Posted by stlpaul View Post
And be sure it can handle the number 0 (which still takes 1 character to print on-screen). It is an easy one to miss when you're focusing on testing bigger numbers.

Using standard library functions, depending on what you know about the input number type, a perhaps easier mathematical approach could be to use log10(i)+1 but it's still up to you to handle negatives, floats, etc.
I like seeing the two above paragraphs together. Just how much is log10(0), pray tell?

Taking the "convert it to a string and count the characters" approach, in C++11, my everyday choice would be to simply do: to_string(i).length()
I'm glad someone has suggested this approach. By far the simplest, plus tried and tested using standard library rather than cooking your own and risk introducing mistakes or forgetting special cases (think numbers like -0.42).
 
Posts: 1,141 | Thanked: 781 times | Joined on Dec 2009 @ Magical Unicorn Land
#15
Originally Posted by pichlo View Post
I like seeing the two above paragraphs together. Just how much is log10(0), pray tell?
Oh! Oh! I believe I covered myself on that by my inclusion of "etc."
 
Reply


 
Forum Jump


All times are GMT. The time now is 11:55.