Calculate max value of variables

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.
Are you talking about sorting 3 values in ascending or descending order?
Order 3 values and calculate which one is highest put it first 2nd and 3rd.
And where is it that you are stuck? Show your code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>

using namespace 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;
	}
	else if (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.
The code looks good to me. Try to expand it to one more variable. It is not that difficult. Go for it.
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>

using namespace 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));
Last edited on
Well, tried but don't know how to order them. Ok, thanks anyway.
as a beginner, this is a good opportunity to learn about various sorting algorithms:

http://en.wikipedia.org/wiki/Heap_sort
http://en.wikipedia.org/wiki/Bubble_sort
http://en.wikipedia.org/wiki/Quick_sort

even better, implement them all in C++ and make sure that you get the same result from all three

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().
ok,
non-deterministic
sounds quite scary when you are sorting

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

http://en.wikipedia.org/wiki/Stable_sort#Stability

edit: I suspect we are way beyond the capabilities of the OP - hopefully, the OP will get something out of this discussion
Last edited on
Topic archived. No new replies allowed.