### Three Integers from Least to Greatest

I thought i was off to a good start but the minToMax function only sorts the first to variables. What am i missing?

 ``1234567891011121314151617181920212223242526272829303132333435`` ``````#include using namespace std; void minToMax(int &num1, int &num2, int &num3); // rearrange from small to large int main() { int num1; int num2; int num3; cout << "Enter 3 Numbers:\n"; cin >> num1; cin >> num2; cin >> num3; minToMax(num1,num2,num3); cout << "Those three numbers from smallest to greatest are: " << num1 << ", " << num2 << ", " << num3 << endl << endl; return 0; } void minToMax(int &num1, int &num2, int &num3) { if (num1 > num2) { int temp = num1; num1 = num2; num2 = temp; } }``````
Last edited on
 the minToMax function only sorts the first two variables

Indeed.

You have ensured that num1 <= num2.
Now you could do similar operation to ensure that num1 <= num3.
After these two we do know that num1 has the smallest element.

What remains is to ensure that num2 <= num3.

In other words: you do only the first of three steps.
It only sorts the first 2 because that is all you asked it to do. There is nothing in `void minToMax()` that alters `num3`:

 ``1234567891011121314151617181920`` ``````void minToMax(int &num1, int &num2, int &num3) { if (num1 > num2) { int temp = num1; num1 = num2; num2 = temp; } else ; if(num2 > num 3) { int temp = num2; num2 = num3; num3 = temp; } else ; }``````

To be honest, I think this would look better as a while loop, like so:

 ``1234567891011121314151617`` ``````void minToMax(int &num1, int &num2, int &num3) { while((num1 > num2) && (num1 > num3) && (num2 > num3)) int temp; if(num2 < num1) { temp = num2; num2 = num1; num1 = temp; } else { temp = num2; num2 = num3; num3 = temp; } }``````
Thank you guys so much for the help
Topic archived. No new replies allowed.