C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 11th, 2007, 03:33 AM
chaostheory chaostheory is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2007
Posts: 10 chaostheory User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 23 m 45 sec
Reputation Power: 0
Finding the number of digits

Im trying to write a function in which you pass an integer as an argument and you get the number of digits that the integer contains back. Here is what I have, but the code is flawed:

int getsize(int x)
{
int n=1;

for(;
{
if(x%pow(10,n)==0)
return n;
n++;
}

}

I havent tested it out, but the most obvious flaw is that if the integer is a multiple of 10, then x%10 will give 0 which will terminate the loop prematurely.

Can someone point out which way I should head to get this function working? I can do the code on my own.

Reply With Quote
  #2  
Old May 11th, 2007, 04:14 AM
requinix's Avatar
requinix requinix is offline
Still alive
Dev Shed God 16th Plane (12500 - 12999 posts)
 
Join Date: Mar 2007
Location: Washington, USA
Posts: 12,869 requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)  Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 5 Months 1 Week 5 Days 6 h 19 m 22 sec
Reputation Power: 8977
Send a message via AIM to requinix Send a message via MSN to requinix Send a message via Yahoo to requinix Send a message via Google Talk to requinix
...

Two paths here:
1) You discover that floor(log10(abs(x)))+1 will give you the number of digits in x, or

2) Make a loop that until integer x==0, you divide x by 10 and increment a counter
Comments on this post
IOI-RLZ agrees: log is indeed good...

Reply With Quote
  #3  
Old May 11th, 2007, 04:28 AM
WebDunce WebDunce is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2005
Location: Northwest Florida
Posts: 208 WebDunce User rank is Corporal (100 - 500 Reputation Level)WebDunce User rank is Corporal (100 - 500 Reputation Level)WebDunce User rank is Corporal (100 - 500 Reputation Level)WebDunce User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 Days 22 h 8 m 28 sec
Reputation Power: 10
Quote:
Originally Posted by requinix
...

2) Make a loop that until integer x==0, you divide x by 10 and increment a counter


i think it should be loop until x < 1, right?

(nevermind, both conditions work...as long as x is of type integer)

Last edited by WebDunce : May 11th, 2007 at 04:35 AM.

Reply With Quote
  #4  
Old May 11th, 2007, 05:25 AM
chaostheory chaostheory is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2007
Posts: 10 chaostheory User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 23 m 45 sec
Reputation Power: 0
Quote:
Originally Posted by requinix
...

Two paths here:
1) You discover that floor(log10(abs(x)))+1 will give you the number of digits in x, or

2) Make a loop that until integer x==0, you divide x by 10 and increment a counter


Whoa! Thank you. The log thing is cool. Thanks man.

Reply With Quote
  #5  
Old May 11th, 2007, 04:05 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,406 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 9 h 47 m 28 sec
Reputation Power: 4080
The log10() thing won't work for negative numbers or 0 though. You could get around this by using:
(a) Check for 0 and return 1
(b) Check for - sign and then convert the number to positive and find its length and add 1 to it (for the - sign)

Another way would be to use the sprintf() function, which returns the number of characters that were used in the string:
Code:
char buf[30];
int i = 1234;
int len = sprintf(buf, "%d", i);
__________________
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne

Reply With Quote
  #6  
Old May 12th, 2007, 03:04 AM
clifford's Avatar
clifford clifford is offline
Contributing User
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Aug 2003
Location: UK
Posts: 4,824 clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 21 h 1 m
Reputation Power: 1800
Quote:
Originally Posted by Scorpions4ever
(b) Check for - sign and then convert the number to positive and find its length and add 1 to it (for the - sign)


Or just use abs(x) as the argument to log10() as requinix did in his original suggestion. Your solution (both of them) counts the sign as a digit for negative numbers. It is not a digit, and counting it as such may not be the intention, it is certainly not what was asked.

If performance were an issue sprintf() is probably far slower.

Clifford

Clifford

Reply With Quote
  #7  
Old May 12th, 2007, 03:55 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,905 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 4 Days 1 h 9 m 41 sec
Reputation Power: 1774
Logarithms are not exactly cheap either.

Code:
#include <stdio.h>

int intlen ( int a ) {
    int len = 0;
    if ( a < 0 ) a = -a;
    while ( a >= 10000 ) { len += 4; a /= 10000; }
    while ( a >= 100   ) { len += 2; a /= 100; }
    while ( a >= 10    ) { len ++;   a /= 10; }
    return len + 1;
}

int main ( ) {
    printf("%d\n", intlen(0) );
    printf("%d\n", intlen(9) );
    printf("%d\n", intlen(42) );
    printf("%d\n", intlen(123456789) );
    return 0;
}


$ ./a.exe
1
1
2
9
Comments on this post
clifford agrees: Smart
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

Reply With Quote
  #8  
Old May 12th, 2007, 06:34 AM
clifford's Avatar
clifford clifford is offline
Contributing User
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Aug 2003
Location: UK
Posts: 4,824 clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 21 h 1 m
Reputation Power: 1800
Quote:
Originally Posted by salem
Logarithms are not exactly cheap either.
Nice solution. I benchmarked them all, and got some interesting results.

I used the following code:
Code:
#include <limits.h>
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <stdlib.h>

int intlen1 ( int a ) 
{
    int len = 0;
    if ( a < 0 ) a = -a;
    while ( a >= 10000 ) { len += 4; a /= 10000; }
    while ( a >= 100   ) { len += 2; a /= 100; }
    while ( a >= 10    ) { len ++;   a /= 10; }
    return len + 1;
}

int intlen2( int a )
{
    if( a == 0 )
    {
        return 1 ;
    }
    else
    {
        if ( a < 0 ) a = -a;
        return (int)log10((double)a) + 1 ;
    }
}

int intlen3( int a )
{
    char buf[30] ;
    int len = sprintf(buf, "%d", a);
    if( a < 0 )
    {
        len-- ;
    }

    return len ;
}

#define ITERATIONS 10000000
int main()
{
    int i ;
    static int test_data[ITERATIONS] ;
    clock_t end ;
    clock_t start ;
    volatile int checksum ;

    for( i = 0; i < ITERATIONS; i++ )
    {
        test_data[i] = rand() << 17 | (rand() & 0xffff8000) << 1 | (rand() & 0x1) ;
    }

    checksum = 0 ;
    start = clock() ;
    for( i = 0; i < ITERATIONS; i++ )
    {
        for( i = 0; i < ITERATIONS; i++ )
        {
            checksum += intlen1( i ) ;
        }
    }
    end = clock() ;
    printf( "Integer algorithm:\t%d, %d\n", end - start, checksum ) ;

    checksum = 0 ;
    start = clock() ;
    for( i = 0; i < ITERATIONS; i++ )
    {
        checksum += intlen2( i ) ;
    }
    end = clock() ;
    printf( "Log10 algorithm:\t%d, %d\n", end - start, checksum ) ;

    checksum = 0 ;
    start = clock() ;
    for( i = 0; i < ITERATIONS; i++ )
    {
        checksum += intlen3( i ) ;
    }
    end = clock() ;
    printf( "String algorithm:\t%d, %d\n", end - start, checksum ) ;
}


And MS VC++ 8, native Win32 code.


Debug build
----------------
Integer algorithm: 1562, 68888890
Log10 algorithm: 1844, 68888890
String algorithm: 8937, 68888890

Release build
----------------
Integer algorithm: 156, 68888890
Log10 algorithm: 719, 68888890
String algorithm: 4640, 68888890


The string algorithm does not benefit much from optimisation. Salem's solution is obviously superior.

Note I changed the floor() call to a simple cast to int in my version. This has a significant beneficial effect on execution time. Note also the cast to double, this is to disambiguate from the single precision overload in C++ compilation. If you cast to float as I originally did, you get the wrong answer for 7 digit values close to 10,000,000 (this is what the checksum was there for - to make sure they produced the same result).

It is interesting because I have in the past and would have in this case, suggested the log10 method. It remains easy to implement (and remember) and is reasonably fast on a system with an FPU at least. Since I generally do embedded systems development and have no FPU, I'll have to remember Salem's solution!)

Clifford

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Finding the number of digits

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap