# Thread: Quick Questions Max/min & Break/EXIT

1. ## Quick Questions Max/min & Break/EXIT

I've been working on this program and most things are working... However I was wondering if there is an easier way to write my last funtion without using an array... For the sorting. I need them to be sorted least to greatest I think I have it backwards at the moment. Also, the teacher recommended using swap how would I do this? This is the only function holding me up. Also is do my case five and default case statements look ok. Case five should exit and defualt should ask the user to re enter values.

Code

Code:
```// ***************************************
//Doug Miller
//Lab 3, Part 3
//Working with numbers; to compute average!
//****************************************

#include <iostream>
#include <iomanip>

using namespace std;

float Do_Sum (float, float, float);
float Do_Product (float, float, float);
float Do_Average (float, float, float);
void Do_Sort (float&, float&, float&);
void Swap(int& x, int& y);

int main()
{
int option = 0;
float v_1 = 0;
float v_2 = 0;
float v_3 =0;

while (option != 5)
{
cout << "Options" << endl;
cout << "-------" << endl;
cout << "\n\n";
cout << "1.) Sum" << endl;
cout << "2.) Product" << endl;
cout << "3.) Average" << endl;
cout << "4.) Sort" << endl;
cout << "5.) Exit" <<endl;
cout << "\n\n";
cin >> option;

switch (option)
{
case 1:
{
cout << "Please enter three values: " << endl;
cin >> v_1 >> v_2 >> v_3;
cout << "\n\n";
cout << "The sum of these numbers is " << Do_Sum (v_1, v_2, v_3);
cout << "\n\n";
break;
}
case 2:
{
cout << "Please enter three values: " << endl;
cin >> v_1 >> v_2 >> v_3;
cout << "\n\n";
cout << "The product of these numbers is"  << Do_Product (v_1, v_2, v_3);
cout << "\n\n";
break;
}
case 3:
{
cout << "Please enter three values: " << endl;
cin >> v_1 >> v_2 >> v_3;
cout << "\n\n";
cout << "The average of these numbers is " << Do_Average (v_1, v_2, v_3);
cout << "\n\n";
break;
}
case 4:
{
cout << "Please enter three values: " << endl;
cin >> v_1 >> v_2 >> v_3;
cout << "\n\n";
Do_Sort (v_1, v_2, v_3);
cout << v_1 << " " << v_2 << " " << v_3;
cout << "\n\n";
break;
}
case 5:
{
option = 5;
break;
}
default:
{
cout << "Please renter option: " << endl;
cin >> option;
}
}
}

}//END OF MAIN

float Do_Sum (float v_1, float v_2, float v_3)
{
float total = 0;

total = (v_1 + v_2 + v_3);

}//END OF DO SUM

float Do_Product (float v_1, float v_2, float v_3)
{

float product = 0;

product = (v_1 * v_2 * v_3);

return product;

}//END OF DO PRODUCT

float Do_Average (float v_1, float v_2, float v_3)
{
float average = 0;

average = ((v_1 + v_2 + v_3) / 3);

return average;

}//END OF DO AVERAGE

void Do_Sort (float& v_1, float& v_2, float& v_3)
{

if (v_2 < v_1)
{
swap(v_2, v_1);
}
else if (v_3 < v_1)
{
swap (v_3, v_1);
}
else if (v_3 < v_2)
{
swap (v_3, v_2);
}
else if (v_1 < v_1)
{
swap (v_1, v_3);
}

}//END OF SORT

void Swap(int& u, int& v)
{
int temp;
temp = u;
u = v;
v = temp;

} //END OF SWAP```

2. Code:
```	{
v_1 = v_1;
v_2 = v_2;
v_3 = v_3;
}
}//END OF SORT```
This bit is obviously completely un-necessary, as it doesn't actually do ANYTHING [nor does it have to].

More "unneeded".
Code:
```	if (v_1 > v_2 && v_1 > v_3)
{
v_1 = v_1;```
I'm pretty sure that this is broken:
Code:
```		temp = v_1;
v_1 = v_2;

if (v_1 > v_3)  // v_1 is now  the same as v_2.
{
v_2 = v_1;  // So we set v_2 to itself.
v_3 = v_3; // And v_3 to itself.
}
else
{
v_2 = v_3;
v_3 = temp;
}```
--
Mats

3. GCNDoug, did you compile and test your program? If so, copy and paste it when posting instead of rewriting it. I noticed that you wrote "Do_Profuct" which should be "Do_Product". Other typographical errors might have been introduced.

4. I'm actually without a complier right now I'll update the code later; I shouldn't have posted it at its current state. Sorry everyone.

5. Code:
```switch(option)
{
...
case 4:
{
Do_Sort (v_1, v_2, v_3);
cout << v_1, v_2, v_3;
cout << "\n\n";
break;
}
case 5:
{
option = 5;
break;
}
...
}```
That cout statement is incorrect, it should be something like:
Code:
`cout << v_1 << ' ' << v_2 << ' ' << v_3;`
The code for case 5 is redundant. If you reach that code, option is already equal to 5 so why set it to 5 again?

6. I'm actually without a complier right now
You could use the Comeau C++ Online compiler to check if your code at least compiles.

7. And you're right, you do have the sorting function backwards. The first line, for example, should be
Code:
```if (v_1 < v_2 && v_1 < v_3)
{
v_1 = v_1;```
And you shouldn't need that code following it (in green). If v_1 is less than the other two, then it is already set as it needs to be.

Also, what happens if v_3 is the least of the three? That's what your last part should be about doing, not about remaking the values as they are.

Next, when you're passing the variables to the functions, they're not passing them back. Probably the easiest way to do it, since you're needing to return more than one variable, is to simply put the "cout" part of it inside each function instead of doing it in the cases. That way, you pass the variable to to the functions, and they can do whatever they need to with them, and not worry about sending them back.

Finally, there is a much cleaner way to do the sorting function. Instead of rearranging the variable definitions (i.e. "temp = v_2, v_2 = v_3, v_3 = temp"), just output them in order of how they're already set.

8. Thanks everyone I got everything working but the sort (updated my code from the first post). I'm getting there. So close! lol

9. Alright well I made a whole lot more if statements and still can't get it to sort right. Can someone lead me in the right direction?

10. What is your current code?

11. Thanks for the reply you are the best! I updated my first post with the current code. Everything is working but the sort...

12. The problem with your sort is that it only ever swaps exactly once. Take for example: (2, 3, 1). To sort to (1, 2, 3), you need at least two swaps.

13. Would you happen to know where I could go to find some examples for sorting that aren't for arrays? I can't use arrays for this... If i could it would be done by now. I still can't seem to get this

I tried this and it should at least work if they're backwards like 10 9 8
... since it's by refrence
Code:
```	if (v_2 < v_1)
{
swap (v_2, v_1);
}

if (v_3 < v_2)
{
swap (v_3, v_2);
}```
Still won't work though

14. Think of it this way. If you have three numbers to sort in ascending order, first get the smallest number and put it in the first position. Then, if the last number is smaller than the middle number, swap those.

Perhaps something like this:
Code:
```/* make n1 the smaller of n1 and n2 */
if(n1 > n2) swap(n1, n2);

/* make n1 the smaller of n1 and n3, and thus the smallest of all three */
if(n1 > n3) swap(n1, n3);

/* make n3 the largest of n2 and n3, which are the largest and second-largest numbers */
if(n2 > n3) swap(n2, n3);```
I think I got that right . . .

15. You need three if-statments and three swaps, in the trivial case.

--
Mats