How can I calculate the max value of variables. Let's say I have three different variables which are later assigned and is there some command which will order them from the maximum to minimum and reverse. Thanks.
#include <iostream>
usingnamespace std;
int main()
{
int apples;
cout << "Enter the value of apples : " << endl;
cin >> apples;
cout << "The value of apples is " << apples << "." << endl;
int grapes;
cout << "Enter the value of grapes : " << endl;
cin >> grapes;
cout << "The value of grapes is " << grapes << "." << endl;
if (apples > grapes)
{
cout << "Apples have greater value than grapes." << endl;
}
elseif (apples == grapes)
{
cout << "Grapes have equal value with apples." << endl;
}
else
{
cout << "Grapes have greater value than apples." << endl;
}
return 0;
}
Simple program so that you input the values for grapes and apples and then sort them. Well, here are two variable but guess it can later be done with more.
It was better with two values because it needed only that much code because with 3 values the code will expand with a lot more if functions. I tried something far from perfect just some simple demonstration with 3 variables. I don't know how it picks any of the functions and why it doesn't pick the other.
#include <iostream>
usingnamespace std;
int main()
{
int apples;
cout << "Enter the value of apples : " << endl;
cin >> apples;
cout << "The value of apples is " << apples << "." << endl;
int grapes;
cout << "Enter the value of grapes : " << endl;
cin >> grapes;
cout << "The value of grapes is " << grapes << "." << endl;
int bananas;
cout << "Enter the value of bananas : " << endl;
cin >> bananas;
cout << "The value of bananas is : " << bananas << endl;
if (apples > grapes && apples > bananas)
{
cout << "Apples have greater value than grapes and bananas." << endl;
}
if (grapes > apples && grapes > bananas)
{
cout << "Grapes have greater value than bananas and apples." << endl;
}
if (bananas > apples && bananas > grapes)
{
cout << "Bananas have greater value than grapes and apples." << endl;
}
if (apples == grapes)
{
cout << "Apples and grapes have equal value." << endl;
}
if (apples == bananas)
{
cout << "Apples and bananas have equal value." << endl;
}
if (bananas == grapes)
{
cout << "Bananas and grapes have equal value." << endl;
}
return 0;
}
Have you tried nesting a couple of ternary conditional expression operators ?:
I`m not saying that this may not be frowned on by some! a=(x>y?x:(a>z?a:z));
Well, Quicksort is non-deterministic, so Quicksort might yield different results in some cases. This is why the STL provides two sorts: std::sort() and std::stable_sort().
perhaps a better term is that Quicksort is could be unstable, which could be unsettling, but if you look at the case example, for most cases, having an unstable sort is not a problem