Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 10th, 2007, 11:17 AM   #11
pegasus001
Hobbyist Programmer
 
pegasus001's Avatar
 
Join Date: Nov 2006
Location: 163H
Posts: 213
Rep Power: 2 pegasus001 is on a distinguished road
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.
pegasus001 is offline   Reply With Quote
Old Apr 10th, 2007, 11:42 AM   #12
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
Quote:
Originally Posted by pegasus001 View Post
Sorry if this question seems stupid but why do you have to write this :
std::cin >> y;
I put that in there so when you ran the compiled program, the results wouldn't come and go as soon as the "window" opened. I would have put in system("PAUSE"); but that isn't very portable.

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."
Infinite Recursion is offline   Reply With Quote
Old Apr 10th, 2007, 8:33 PM   #13
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 825
Rep Power: 4 The Dark is on a distinguished road
Quote:
Originally Posted by rwm View Post
ok so this works:

//number of digits
inline int num_digits(int num) {
	return ceil(log10(num));
}

but lots of warnings! :/

thanks guys!
Try it when num = 100
The Dark is offline   Reply With Quote
Old Apr 10th, 2007, 10:46 PM   #14
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Apr 10th, 2007, 11:55 PM   #15
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
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."
Infinite Recursion is offline   Reply With Quote
Old Apr 11th, 2007, 12:03 AM   #16
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 754
Rep Power: 3 Jimbo is on a distinguished road
Quote:
Originally Posted by Infinite Recursion View Post
I suppose that would be easier...

int CountDigits (int src)
{
    int count = 0;
    while (src > 0)
    {
          src = src/10;
          count++;
    }
    return count;
}
This code assumes positive values. The OP didn't say anything about negatives, but I'm assuming they're valid input as well.
__________________
<insert disclaimer here>
<insert shameless plug for Visual Studio here>
Jimbo is offline   Reply With Quote
Old Apr 11th, 2007, 12:10 AM   #17
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
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."
Infinite Recursion is offline   Reply With Quote
Old Apr 11th, 2007, 12:26 AM   #18
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 754
Rep Power: 3 Jimbo is on a distinguished road
or just use while(src)
__________________
<insert disclaimer here>
<insert shameless plug for Visual Studio here>
Jimbo is offline   Reply With Quote
Old Apr 11th, 2007, 3:00 AM   #19
rwm
Professional Programmer
 
Join Date: Jan 2007
Location: Cape Town
Posts: 291
Rep Power: 2 rwm is on a distinguished road
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
rwm is offline   Reply With Quote
Old Apr 11th, 2007, 3:09 AM   #20
rwm
Professional Programmer
 
Join Date: Jan 2007
Location: Cape Town
Posts: 291
Rep Power: 2 rwm is on a distinguished road
hey well both work nicely...

but gonna stick with division...

thanks for help guys!

much appreciated!
rwm is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:41 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC