# Dissecting numbers

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 12-31-2006
jmajeremy
Dissecting numbers
In my program the user must enter a three-digit number. I need to set each of the three digits as a separate variable of type int, but I can only set the three-digit number as a single variable. Right now, my temporary solution is to tell the user to give each digit seperately, but I'd like to be able to take the three-digit number and dissect the numbers to create three one-digit numbers held in three different int variables.
Can anyone help?

-Thanks,
Jeremy
• 12-31-2006
Salem
int num = 123;

Now print out what num/10 is, and what num%10 is
• 12-31-2006
C+/-
Here is an idea for an algorithm. It might not be the best solution there is, but it'll probably work:

The original, 3-digit number is called A
Digit1 = A / 100
Digit2 = (A - Digit1 * 100) / 10
Digit3 = (A - Digit1 * 100 - Digit2 * 10)

Edit: too late..
• 12-31-2006
jmajeremy
Thanks alot C+/-, your algorithm works perfectly!
• 12-31-2006
vart
Quote:

Originally Posted by jmajeremy
Thanks alot C+/-, your algorithm works perfectly!

It is too complex
Using Salem's advice you can bring it to:
Code:

```Digit1 = A / 100 Digit2 = (A %100) / 10 Digit3 = A %10```
for example
• 12-31-2006
robatino
Code:

```int digit[3], tmp = num; for (int i=3; i--;) {   digit[i] = tmp%10;   tmp /= 10; }```
Then digit[0] = 1, digit[1] = 2, digit[2] = 3.
• 12-31-2006
laserlight
Eh, robatino, I think you should initialise i to 2, not 3.

EDIT:
No, I saw it wrongly: it does work, since you have a post-decrement. But that is, frankly, a hack that will confuse people, just as it confused me. I think it is easier to understand at a glance the more common idiom:
Code:

```for (int i = 2; i >= 0; --i) {     digit[i] = tmp % 10;     tmp /= 10; }```
• 12-31-2006
robatino
Inside the loop i runs from 2 to 0. The i-- gets executed before the loop starts.
• 12-31-2006
twomers
I think laserlight saw that, but it's not a very good place to put the post-decrement operator! It's just ... out of place compared to the norm. Especially for a beginner...
• 12-31-2006
robatino
In this particular case efficiency isn't an issue, but if one works with large loops, it's the most concise and efficient way to loop from n-1 to 0. Also I like the fact that one can initialize to the actual number of elements n in the array, instead of n-1.
• 12-31-2006
laserlight
Quote:

In this particular case efficiency isn't an issue, but if one works with large loops, it's the most concise and efficient way to loop from n-1 to 0.
Precisely: efficiency is not the issue here, but your version affects clarity, and so we can quote the maxim that premature optimisation is the root of all evil.

Concerning efficiency: I am no expert in this area, but I doubt your version is measurably more efficient anyway. The normal idiom compares with zero, enters the loop body, and then decrements. Your version implicitly compares with zero, decrements, and then enters the loop body.
• 12-31-2006
robatino
Clarity depends on what one is used to - the usual idiom for copying C-style strings
Code:

`  while (*s++ = *t++) ;`
is in my opinion much more cryptic, but it gets commonly used anyway (Kernighan and Ritchie actually recommend it!). As for efficiency, the comparison with zero is probably more efficient in my case, since as I understand it comparisons like <= require a subtraction, as opposed to != which is just a bitwise comparison. Your version can't use !=, since it needs to allow 0 as a possible value (although it could if the loop went up instead of down), and also has to use a signed integral type as the loop variable, which halves the maximum range of the loop (although again this isn't necessary if the loop goes up).

Edit: Sorry, actually your version _could_ use != but only as "i != -1".
• 12-31-2006
vart
Code:

`for (int i=3; i--;)`
Problem with the above pattern that on the first glance it is read as
for (int i=3; i>0 ; i--) due to standard usage of the for loop
only on the third glance the code is read as it is written... And this makes it hard to understand and possible source of bugs when someone comes to modify it...

If I had to write something like that I'll go on the laserlight's solution or:
Code:

```for (int i=3; i>0;) {   i--;   etc }```
But of course if the code is not supposed to be maintained - it is not an issue...
• 12-31-2006
laserlight
Quote:

Clarity depends on what one is used to
I agree. In this case, your version is not what people would be used to, and considering that jmajeremy needed help for such a relatively trivial problem, it is reasonable to expect that jmajeremy lacks experience in C++, and possibly computer programming in general. As such, it makes sense to give examples that are along common lines, especially since the usual idiom is more general (loop from some high index to some low index).

Quote:

As for efficiency, the comparison with zero is probably more efficient in my case, since as I understand it comparisons like <= require a subtraction, as opposed to != which is just a bitwise comparison.
It is possible to do a bitwise comparison to test for such inequalities, of course. Anyway, the compiler may well be able to optimise the code such that it performs no worse than your version.

Quote:

and also has to use a signed integral type as the loop variable, which halves the maximum range of the loop (although again this isn't necessary if the loop goes up).
True, but that does not matter at all here. Even for a 400-bit integer an 8-bit signed char as the loop index would be good enough (though also a little strange, heh).
• 12-31-2006
robatino
Quote:

Originally Posted by laserlight
I agree. In this case, your version is not what people would be used to,

But they should be familiar with it, or at least have seen it a few times. Looping between 0 and n-1 is a basic operation, and the most concise/efficient ways of doing it are
Code:

`for (int i=0; i != n; i++) {}`
upwards, and
Code:

`for (int i=n; i--;) {}`
downwards. These also allow using an unsigned index, and have the clarity of referring to n instead of n-1. This is a very small amount of information to store in one's bag of tricks, and doesn't require thought once one knows it. (I think the "premature optimization" quote refers to something that requires effort.) But most people, even experienced ones, won't consider doing it unless they've seen it in someone else's code. This is one of those cases where the best tool for the job has fallen through the cracks because of the self-fulfilling prophecy of everyone being afraid of confusing everyone else. The danger of temporary confusion is small compared to the possible benefit of learning an idiom which can be used routinely. This applies to maintaining other people's code as well as learning to code. I could have avoided using the idiom on the grounds that it's not needed on efficiency or range grounds here, but then jmajeremy probably never would have seen it again.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last