Sorting integer inputs by order

I have to write a program that takes three integers as input and outputs the integers in order from least to greatest.

Here is a sample run as an example:

Enter three integers: 4, -2, 0
Sorted from least to greatest, the integers are -2, 0, 4


1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

using namespace std;

void order3(int a, int b, int c);

int main()
{
	cout << "Enter three integers: ";
	int a, b, c;
	cin >> a >> b >> c;
	order3(a, b, c);
return 0;
}


I'm not sure how to define the "order3" function in my code. Please help.
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
There is already sort function in STD library

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
    vector<int> vec;

    vec.push_back(2);
    vec.push_back(3);
    vec.push_back(1);

    cout<<"before sorting"<<endl;
    for(int i=0; i<vec.size(); i++)
        cout<<vec.at(i)<<"\t";

    sort(vec.begin(), vec.end());

    cout<<endl<<"after sorting"<<endl;
    for(int i=0; i<vec.size(); i++)
        cout<<vec.at(i)<<"\t";
}
This also works, for an explanation why, see here:

http://www.learncpp.com/cpp-tutorial/64-sorting-an-array-using-selection-sort

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
#include "stdafx.h"
#include <iostream>
#include <algorithm>


using namespace std;

void order3(int a, int b, int c)
{
	const int size = 3;
	int array[size] = { a,b,c };
	for (int count = 0; count < size; ++count)
	{
		int smallestIndex = count;
		for (int currentIndex = count + 1; currentIndex < size; ++currentIndex)
		{
			if (array[currentIndex] < array[smallestIndex])
				smallestIndex = currentIndex;
		}
		std::swap(array[count], array[smallestIndex]);
	}
	for (int index = 0; index < size; ++index)
		std::cout << array[index] << ' ';
}


int main()
{
	cout << "Enter three integers: ";
	int a, b, c;
	cin >> a >> b >> c;
	order3(a, b, c);
	return 0;
}
Last edited on
Topic archived. No new replies allowed.