![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 |
|
Hobbyist Programmer
Join Date: Nov 2006
Location: 163H
Posts: 213
Rep Power: 2
![]() |
Sorry my misunderstanding. Rushing as always.
Thanx.
__________________
You never test the depth of a river with both feet. The believer is happy. The doubter is wise. Free speech carries with it some freedom to listen. The next generation will always surpass the previous one. It`s one of the never ending cycles of life. |
|
|
|
|
|
#12 | |
|
Programming Guru
![]() ![]() ![]() |
Quote:
Basically, its there to give you time to read the results.
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
|
|
|
#13 | |
|
Expert Programmer
Join Date: Jun 2005
Posts: 825
Rep Power: 4
![]() |
Quote:
|
|
|
|
|
|
|
#14 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
I recommend reading posts #1 for the problem (integers, not strings) and #4 for the solution.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#15 |
|
Programming Guru
![]() ![]() ![]() |
I suppose the dividing by 10 would be easier to follow... either way would work really, no need to convert to a string and return the length, just one out of a few options.
int CountDigits (int src)
{
int count = 0;
while (src > 0)
{
src = src/10;
count++;
}
return count;
}
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
|
|
#16 | |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 754
Rep Power: 3
![]() |
Quote:
__________________
<insert disclaimer here> <insert shameless plug for Visual Studio here> |
|
|
|
|
|
|
#17 |
|
Programming Guru
![]() ![]() ![]() |
Ahhh. True.
If there is a potential for negative values, you could use a call to the abs() function prior to the while block... ... src = abs(src); ...
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
|
|
#18 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 754
Rep Power: 3
![]() |
or just use while(src)
__________________
<insert disclaimer here> <insert shameless plug for Visual Studio here> |
|
|
|
|
|
#19 |
|
Professional Programmer
Join Date: Jan 2007
Location: Cape Town
Posts: 291
Rep Power: 2
![]() |
hey guys! wow lots of replies!
just discovered a problem, it should be: //number of digits
inline int num_digits(int num) {
return floor(log10(num) + 1);
}since log10 of 10,100,1000 etc is one less than the number of digits... so you guess divide by 10 would be optimal? i guess i might as well do that ![]() |
|
|
|
|
|
#20 |
|
Professional Programmer
Join Date: Jan 2007
Location: Cape Town
Posts: 291
Rep Power: 2
![]() |
hey well both work nicely...
but gonna stick with division... thanks for help guys! much appreciated! |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Using maths to split an integer into it's seperate digits? | bivhitscar | C | 12 | Feb 22nd, 2006 11:31 PM |
| Counting the Days.... | Ghost | C# | 2 | Dec 8th, 2005 11:33 PM |
| Counting occurrence of numbers in C | stabule | C | 6 | Nov 15th, 2005 10:49 AM |
| Showing more digits | BebopFusion | C | 4 | Jul 15th, 2005 3:57 PM |
| Help with a counting problem. | AngelX | Visual Basic | 4 | Apr 2nd, 2005 6:04 PM |