3 numbers from big to small and small to big

closed account (G2UR4iN6)
So I'm making a code that will display 3 different numbers.

After putting 3 numbers, the compiler will organize the numbers from biggest to the smallest.

Whenever I put middle, smallest then big number, the answers are correct.

Let's say if I put 77, 50 & 100, the compiler will give me the following answer:
100 > 77 > 50
50 < 77 < 100

However if I put a big, smallest, middle number, I get a different wrong answers.

Here's the 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
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
using namespace std;
int main()
{
	double a, b, c;

	cout << "Please enter the first number: ";
	cin >> a;

	cout << "Please enter the second number: ";
	cin >> b;

	cout << "Please enter the third number: ";
	cin >> c;

	if ((a>b) && (a>c))
	{
		cout << a << ">" << b << ">" << c;
	}
	else if ((b>a) && (b>c))
	{
		cout << b << ">" << a << ">" << c;
	}
	else if ((c>a) && (c>b))
	{
		cout << c << ">" << a << ">" << b;
	}

	cout << "\n";

	if ((a<b) && (a<c))
	{
		cout << a << "<" << b << "<" << c;
	}
	else if ((b<a) && (b<c))
	{
		cout << b << "<" << a << "<" << c;
	}
	else if ((c<a) && (c<b))
	{
		cout << c << "<" << b << "<" << a;
	}
}
// middle small big 


Anyone knows the problem?
You need to check which of the remaining number are bigger.

1
2
3
4
if ((a>b) && (a>c)) // You know a is the biggest
	{
		cout << a << ">" << b << ">" << c; // How do you know c < b?
	}


You will need nested if/else
While @Hengry is correct why not use a bubble sort or selection sort it will give any numbers in acending order if used with an array so just make a function that will make an array off the user input something like this

1
2
3
4
5
6
7
8
int *makeArray(int size)
{
	int *ptr;

	ptr = new int[size];

	return ptr;
}


have that with in a function then assign it in main with something like this someArray = makeArray(num);

As for the selection sort you can make a function like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void arrSelection(int *arr[], int size)
{
     int startScan, minIndex;
     int *minElem;
     
     for (startScan = 0; startScan < (size - 1); startScan++)
     {
         minIndex = startScan;
         minElem = arr[startScan];
         for(int index = startScan + 1; index++)
           {
               minElem = arr[index];
               minIndex = index;
            }
       }
       arr[minIndex] = arr[startScan];
       arr[startScan] = minElem;
     }
}


Something like that should work for selection sort sorry i m doing this on my phone so mind the errors if there are any and to do descending you can just reverse it sorry would put it up but really tired lol have fun and i hope it helped.
Topic archived. No new replies allowed.