Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 105,761 C++ Programmers for FREE! Ask your question and get quick answers from experts. There are 1,757 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Algorithm for extracting an integers digits?

 
Reply to this topicStart new topic

Algorithm for extracting an integers digits?

seuzy13
post 18 Jul, 2008 - 07:38 PM
Post #1


New D.I.C Head

*
Joined: 18 Jul, 2008
Posts: 4

Hello!

I've been trying to write a library function that will extract a specified digit from an integer. I've always hated trying to find the digits of a number, because just thinking about all the dividing and modulusing makes me want to weep softly in a corner. I did manage to write a little program once that would separate a five digit integer's digits by spaces, but I'm thinking it only works for five digits numbers? Anyway, I only got it to work by fiddling with a million different possibilities, so a clear explanation would be appreciated. I did a google search on this and came up with nothing.

Thanks for your help!

The declaration of the digit extracting function:
CODE

int digit (int num, int digit); //finds the digit of a number; digits are counted from right to left, so for example, the hundreds place
                                 //would be specified using the digit number 3


The program I mentioned:
CODE

//1-36.cpp
//Puts spaces between the digits of a user-inputted integer;
//Author-SB

#include <iostream>

using namespace std;

int main () {
    int num; //user-inputted integer to get digits of

    /*~get numbers~*/
    cout << "Enter a five-digit number: ";
    cin >> num;

    /*~determine and output digits~*/
    cout << num / 10000 << " " << num / 1000 % 10 << " " << num / 100 % 100 % 10 << " "
         << num / 10 % 1000 % 100 % 10 << " " << num % 10000 % 1000 % 100 % 10<< endl;

    /*note to self: there is a definite algorithm to finding digits, and I should definitely write a library function
    to handle this commonly occuring problem, because the algorithm that does exist makes me want to weep softly in
    a corner*/

    return 0; //return successfully
}
User is offlineProfile CardPM

Go to the top of the page


NickDMax
post 18 Jul, 2008 - 09:01 PM
Post #2


2B||!2B

Group Icon
Joined: 18 Feb, 2007
Posts: 2,672



Thanked 26 times

Dream Kudos: 475
My Contributions


to extract a given digit:

take the number and divide it by 10^(digit -1), then find result mod 10.

You need to do a couple of checks:
First off, if digit == 1 then 10^(digit-1) == 0 so you would divide by zero (bad -- end of the universe and all). So for this you need a seperate calculation of finding the mod 10 of the number.

Secondly, barring the above digit should be between 2 and log10(number) + 1 (the total number of digits). The upper limit is not really all that important since it will just generate a zero, though if digit gets too large then you will cause an overflow.
User is offlineProfile CardPM

Go to the top of the page

seuzy13
post 19 Jul, 2008 - 08:08 AM
Post #3


New D.I.C Head

*
Joined: 18 Jul, 2008
Posts: 4

QUOTE(NickDMax @ 18 Jul, 2008 - 09:01 PM) *

to extract a given digit:

take the number and divide it by 10^(digit -1), then find result mod 10.

You need to do a couple of checks:
First off, if digit == 1 then 10^(digit-1) == 0 so you would divide by zero (bad -- end of the universe and all). So for this you need a seperate calculation of finding the mod 10 of the number.

Secondly, barring the above digit should be between 2 and log10(number) + 1 (the total number of digits). The upper limit is not really all that important since it will just generate a zero, though if digit gets too large then you will cause an overflow.


Thanks, that seems to work, except its a sort of "off by one error." For example, I have the arbitrary number 546721. When I ask for the 1000s digit I get 4.

CODE

result=(number / ( 10^(digit-1) ) )%10;

Also, there is no problem with dividing by zero, because if the digit is one, you get ten to the "zeroth" power. Anything to the zeroth power is one, so there would be no problem with that. Or does it happen to work differently in C++?

This post has been edited by seuzy13: 19 Jul, 2008 - 08:20 AM
User is offlineProfile CardPM

Go to the top of the page

NickDMax
post 19 Jul, 2008 - 08:39 AM
Post #4


2B||!2B

Group Icon
Joined: 18 Feb, 2007
Posts: 2,672



Thanked 26 times

Dream Kudos: 475
My Contributions


smile.gif no it does not work differently in C++ that was just late night stupidity on my part.

However 10^(digit-1)... well it turns out the the ^ operator is XOR in C++...

this is what I came up with:
cpp
#include <iostream>
#include <cmath>
using namespace std;

int getDigit(unsigned int number, unsigned int digit);

int main() {
unsigned int num = 123456789;
cout << "The number is: " << num << endl;
for(int i = 0; i < 12; i++) {
cout << "Digit " << i+1 << " = " << getDigit(num,(i+1)) << "\n";
}
cout << endl;
return 0;
}

int getDigit(const unsigned int number, const unsigned int digit) {
if (digit > 0 && digit <= log10(number)+1) {
return (int)(number / pow((double)10, (double)(digit-1))) % 10;
} else {
return 0;
}
}
User is offlineProfile CardPM

Go to the top of the page

seuzy13
post 19 Jul, 2008 - 08:49 AM
Post #5


New D.I.C Head

*
Joined: 18 Jul, 2008
Posts: 4

Haha, cheers to late night stupidity!

Thanks, this works great. Now I can add it to my library and never think about the modulus operator again. tongue.gif
User is offlineProfile CardPM

Go to the top of the page

NickDMax
post 19 Jul, 2008 - 09:53 AM
Post #6


2B||!2B

Group Icon
Joined: 18 Feb, 2007
Posts: 2,672



Thanked 26 times

Dream Kudos: 475
My Contributions


Don't you wish!!!

The modulus operator is actually amazingly handy.
User is offlineProfile CardPM

Go to the top of the page

seuzy13
post 19 Jul, 2008 - 10:33 AM
Post #7


New D.I.C Head

*
Joined: 18 Jul, 2008
Posts: 4

QUOTE(NickDMax @ 19 Jul, 2008 - 09:53 AM) *

Don't you wish!!!

The modulus operator is actually amazingly handy.


Yeah, I know. crazy.gif But at least I'm off the hook for this problem.
User is offlineProfile CardPM

Go to the top of the page

baavgai
post 19 Jul, 2008 - 06:04 PM
Post #8


Dreaming Coder

Group Icon
Joined: 16 Oct, 2007
Posts: 1,471



Thanked 38 times

Dream Kudos: 325

Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions


The answer offered is the same methodology I'd use. However, the two heavy hitting math functions of log10 and pow aren't needed.

This is a far more basic approach.
cpp

int getDigit(unsigned int number, int digit) {
for(int i=0; i<digit-1; i++) { number /= 10; }
return number % 10;
}


Note, you really should understand how modulus works. It's a good tool and you'll be exposed to again.
User is online!Profile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 8/21/08 01:25PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month