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"?

This is a discussion on *Extracting and comparing numbers* within the **C++ Programming** forums, part of the General Programming Boards category; And if the reverse is true?
And how would we code "then I would compare the numbers from the next ...

- 03-01-2011 #31

- Join Date
- Oct 2007
- Posts
- 23,544

- 03-01-2011 #32

- Join Date
- Oct 2010
- Posts
- 79

Code:if( current1 > current2 ){ largeNum = current1; smallNum = current2; } if( current2 > current1 ){ largeNum = current2; smallNum = current1; }

- 03-01-2011 #33

- Join Date
- Oct 2007
- Posts
- 23,544

- 03-01-2011 #34

- Join Date
- Oct 2010
- Posts
- 79

It was stored into largeNum and smallNum... Ohh, so do I compare smallNum and largeNum to current1 and current2?

- 03-01-2011 #35

- Join Date
- Oct 2007
- Posts
- 23,544

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 #36

- Join Date
- Oct 2010
- Posts
- 79

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 #37

- Join Date
- Oct 2007
- Posts
- 23,544

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 #38

- Join Date
- Oct 2010
- Posts
- 79

Ok, so with

Code:if( current1 > largeNum ){ largeNum = current1; } if( current2 > largeNum ){ largeNum = current2; }

- 03-01-2011 #39

- Join Date
- Oct 2007
- Posts
- 23,544

- 03-01-2011 #40

- Join Date
- Oct 2010
- Posts
- 79

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; } }

- 03-01-2011 #41

- Join Date
- Oct 2007
- Posts
- 23,544

- 03-01-2011 #42

- Join Date
- Oct 2010
- Posts
- 79

How about this?

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 #43

- Join Date
- Oct 2007
- Posts
- 23,544

- 03-01-2011 #44

- Join Date
- Oct 2010
- Posts
- 79

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 #45

- Join Date
- Oct 2007
- Posts
- 23,544

- Exactly how to get started with C++ (or C) today
- C Tutorial
- C++ Tutorial
- 5 ways you can learn to program faster
- The 5 Most Common Problems New Programmers Face
- How to set up a compiler
- 8 Common programming Mistakes
- What is C++11?
- Creating a game, from start to finish

- How to create a shared library on Linux with GCC - December 30, 2011
- Enum classes and nullptr in C++11 - November 27, 2011
- Learn about The Hash Table - November 20, 2011
- Rvalue References and Move Semantics in C++11 - November 13, 2011
- C and C++ for Java Programmers - November 5, 2011
- A Gentle Introduction to C++ IO Streams - October 10, 2011