0

I am trying to create a function that will take an int and separately return the leftmost digit and the rest of the number.

int idigitizer(int *number) { int i = 1; int head = 0; int tmp = 0; tmp = *number; while (tmp > 9) { if ((tmp/i) < 10) { head = tmp/i; *number = *number - (head*i); return head; } else { i = i*10; } } number = 0; return tmp; } 

idigitizer returns the leftmost part of the number and *number will carry the rest. I will have a loop in my main that will keep calling idigitizer until all the digits of the number get separated. The thing is I don't know how to handle zeroes and how to make this process terminate correctly when it is done with the last digit. Any help is welcome. Thanks in advance.

EDIT : To make it clearer. I don't want the possible zeroes in the middle of a number to get lost. If i get the number 100047 as input I want idigitizer to return:

return - *number 100047 1 00047 0 0047 0 047 0 47 4 7 7 
7
  • 1
    possible duplicate of Identify the digits in a given number. Commented Dec 27, 2011 at 17:04
  • 1
    What do you need to return from idigitizer(1000056)? Commented Dec 27, 2011 at 17:04
  • You did not tell us what is this code that you have pasted in the post and what its relationship is to your question. Commented Dec 27, 2011 at 17:05
  • I would expect : 1 0 0 0 0 5 6 as the final product. When I get the first 1 I can't find of a way to keep the rest of the number as 000056. Commented Dec 27, 2011 at 17:06
  • @MikeNakis The code I pasted returns the leftmost digit and puts in the *number the rest of the number. Commented Dec 27, 2011 at 17:07

3 Answers 3

2

I would use something like this instead:

#include <stdio.h> #include <stdlib.h> #include <math.h> int getFirstDigit( int number ); int getRestOfNumber( int number ); int getNumberOfMissingZeros (int number, int restOfNumber); int main(int argc, char* argv[]){ int x = 500574; int firstDigit; int restOfNumber; int n; /* loop index */ firstDigit = getFirstDigit(x); restOfNumber = getRestOfNumber(x); printf("The first digit of %d is %d.\n",x,firstDigit); printf("The rest of the number is "); for( n = 0; n<getNumberOfMissingZeros(x,restOfNumber); ++n ){ printf("0"); } printf("%d.",restOfNumber); return EXIT_SUCCESS; } int getFirstDigit( int number ){ return number / (int)floor( pow(10,floor(log10( (double)number ))) ); } int getRestOfNumber( int number){ return number % (int)floor( pow(10,floor(log10( (double)number ))) ); } int getNumberOfMissingZeros (int number, int restOfNumber){ int digitsOriginally; int digitsInRestOfNumber; digitsOriginally = floor(log10( (double)number ) ); digitsInRestOfNumber = floor(log10( (double)restOfNumber ) ); return digitsOriginally - digitsInRestOfNumber - 1; } 

The magic is in the expression (int)floor( pow(10,floor(log10( (double)number ))) ); This gets the size of the value, like for 52330, it would return 10000, since there are five digits in 52330. This makes it easy to extract the highest digit.

Sign up to request clarification or add additional context in comments.

10 Comments

Why do I get all those "unidentified reference to log10 floor pow trunk" errors ?
No idea. Which compiler are you using? Also, I don't think I'm answering your question since you edited it. This will not keep the zeros.
You need to link the math library probably. Put "-lm" on your link line.
I used gcc. Everything compiled and ran.
I'm using gcc as well. I will try what Craig Wright proposed.
|
1

Given the precisely worded constraints of the problem: a function that will take an int and separately return the leftmost digit and the rest of the number it cannot be done, because it is impossible to represent the leading zeros in an integer, so passing back the "rest of the number" is bound to cause permanent loss of information

As the robot in Lost in Space would say, "It does not compute."

6 Comments

Do you mean that it is actually impossible to separate the digits of an int beginning from the left or do you mean that my wording is poor?
Well, the two form a closure, don't they? It is impossible to separate the digits of an int beginning from the left, so your wording must be poor. Of course it is not necessarily your wording; it could be that your wording is fine, but your understanding of how you want to accomplish your end goal is flawed. Or maybe this is a homework assignment and the teacher is wrong. I do not know.
What are you talking about? Are you referring to the fact that a function in C can't have two return values?
@Eternal_Light Well, it is not impossible just because I say it is impossible. Wait and see if someone comes up with some idea. But the way I see it, it is going to have to be magic. And you already know the reason: it is impossible to represent the "leading zeros" in an integer, so passing back the "rest of the number" is bound to cause permanent loss of information.
Ok, I now see what you're hinting at. But you should really just have said this in your answer...
|
0

Putting an appropriate loop around something like this should work if I understand the question correctly.

//! Given a number return the least significant digit. //! Return true if all went well. False otherwise. (Clearly we //! can handle errors better.) /*! \param[in/out] n The number to strip the least significant digit from. \param[out] d The least significant digit of n. */ bool digitize(int& n, int& d) { if ( n == 0 ) return false else { d = n % 10; n = n / 10; return true; } } 

2 Comments

Nah, he want most significant digit. And the rest :-)
Okay, fine. It's C++, but the idea of using modulus is what is important.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.