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"?
Printable View
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"?
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.Code:if( current1 > current2 ){
largeNum = current1;
smallNum = current2;
}
if( current2 > current1 ){
largeNum = current2;
smallNum = current1;
}
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?)!
It was stored into largeNum and smallNum... Ohh, so do I compare smallNum and largeNum to current1 and current2?
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?
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?
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.
Ok, so with
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?Code:if( current1 > largeNum ){
largeNum = current1;
}
if( current2 > largeNum ){
largeNum = current2;
}
Didn't you just post the logic required to find both max AND min a while ago?
Ok, I figured it out for the most part :
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?Code:while (num != 0){
current1 = num % 10;
num /= 10;
cout <<"curr:" << current1 << endl;
if( current1 > largeNum ){
largeNum = current1;
}
if( current1 < smallNum ){
smallNum = current1;
}
}
Well, you could make sure the loop executes at least once. For example, a do ... while loop.
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;
}
Sure, it works. You can also verify that if you replace while with do while, it will also work.
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'.
First, formulate the logic.