# Extracting and comparing numbers

Show 80 post(s) from this thread on one page
Page 3 of 4 First 1234 Last
• 03-01-2011
Elysia
And if the reverse is true?
And how would we code "then I would compare the numbers from the next pair to the 1st pair and see which one is the biggest and smallest"?
• 03-01-2011
.C-Man.
Code:

```if( current1 > current2 ){                         largeNum = current1;                         smallNum = current2;                 }                 if( current2 > current1 ){                         largeNum = current2;                         smallNum = current1;                 }```
To check which one is the larger number and smaller number of the current pair... I don't know how to compare to the next pair though.
• 03-01-2011
Elysia
But remember that you already checked which of the numbers in the first pass that was biggest and smallest and remembered that result (how did you remember it?)!
• 03-01-2011
.C-Man.
It was stored into largeNum and smallNum... Ohh, so do I compare smallNum and largeNum to current1 and current2?
• 03-01-2011
Elysia
Exactly. So we're almost done.
We have two cases... the first pass and the nth pass.
Can you puzzle them together? It's possible.
Hint: SmallNum and BigNum must of course be initialized before we can compare to them. So what should they be initialized to?
• 03-01-2011
.C-Man.
largeNum should be initialized to 0 and smallNum to 9... but why would we need to initialize them if we are making them current1 and current2 from this first pair?
• 03-01-2011
Elysia
Well, we have two cases.
In the first iteration, we want to compare current1 to current2 and assign BigNum from the biggest of current1 or current2.
In the nth iteration, we want to compare BigNum to current1 and current2 and assign BigNum to current1 or current2, if any if bigger than BigNum.
You could code both cases. Or you could integrate them into one case if you want. It just requires some thinking. Either way, it's up to you.
• 03-01-2011
.C-Man.
Ok, so with
Code:

```if( current1 > largeNum ){                         largeNum = current1;                                         } if( current2 > largeNum ){                         largeNum = current2;                 }```
I can find what the largest num is, but how do I integrate smallNum into this... or should I make different if statements for smallNum?
• 03-01-2011
Elysia
Didn't you just post the logic required to find both max AND min a while ago?
• 03-01-2011
.C-Man.
Ok, I figured it out for the most part :
Code:

```while (num != 0){                                 current1 = num % 10;                 num /= 10;                 cout <<"curr:" << current1 << endl;                 if( current1 > largeNum ){                         largeNum = current1;                                         }                 if( current1 < smallNum ){                         smallNum = current1;                 }                                 }```
I decided that having curr2 was pointless because since smallNum and largeNum are initialized I can just compare them directly to curr1. However, the only issue I have now is when 'num' is 0. Then it only initializes the numbers and smallNum is printed out as 9 every time. How can I change this?
• 03-01-2011
Elysia
Well, you could make sure the loop executes at least once. For example, a do ... while loop.
• 03-01-2011
.C-Man.
Code:

```if( num != 0 ){                 while (num != 0){                                 current1 = num % 10;                 num /= 10;                 cout <<"curr:" << current1 << endl;                 if( current1 > largeNum ){                         largeNum = current1;                                         }                 if( current1 < smallNum ){                         smallNum = current1;                 }                                 }         }         else{                 largeNum = 0;                 smallNum = 0;         }```
• 03-01-2011
Elysia
Sure, it works. You can also verify that if you replace while with do while, it will also work.
• 03-01-2011
.C-Man.
Also, if you don't mind, can you help me with one more thing? In this code I also need to find out how to find the total of all numbers extracted from 'num'.
• 03-01-2011
Elysia
First, formulate the logic.
Show 80 post(s) from this thread on one page
Page 3 of 4 First 1234 Last