Hey guys,

So I am trying to make a program that will prompt the user to enter 3 integers (int1, int2, int3), then sort the integers into ascending order. I have the program reading the integers, but I can't get it to sort. I know I can use the sort function but I don't know how to do it. Here is what I have:

#include <iostream>

using namespace std;
int main()
{
	cout<<"Enter an integer: ";
	int int1;
	cin>>int1;
	cout<<"Enter another integer: ";
	int int2;
	cin>>int2;
	cout<<"Enter a last integer: ";
	int int3;
	cin>>int3;
}

Thanks in advance!

Its a lot easier to sort them if you use an array of 3 integers instead of 3 individual variables.

int arry[3] = {0};
for(int i = 0; i < 3; i++)
{
   cout << "Enter an integer\n";
   cin >> arry[i];
}
// now you can easily sort the array using one of the 
// many sorting algorithms.  The bubble sort is the easiest
// to code.  Google for it and you will find it.

OK. So I can use if statements in a function to sort these. So I think I understand the concept of if statements, and I understand the coding to an extent.

sort(int1, int2, int3)
{
if (":argument")
"do this"
else if ("argument")
"do this"
}

But how do I code it in this particular case?

void swap(int* int1, int* int2)
{
    int temp = *int1;
    *int2 = *int1;
    *int1 = temp;
}


void sort(int int1, int int2, int int3)
{
if( int1 > int2)
{
   swap(&int1, &int2);
}
// now do something similar for the other integers
// Do NOT use else statements.

Edited 7 Years Ago by Ancient Dragon: n/a

I'm not sure I understand this. What are the * for? We have not learned this.

Edited 7 Years Ago by cole davidson: n/a

Don't worry about that. Just do it with if statements.

For example to sort 2 variable you can do this :

int n = 0;
int m = 0;
cin >> n >> m;

if(n < m )
   cout << n << " " << m;
else
cout<< m << " " << n;

For 3 variables, the concept is similar.

if( num1 < num2 )
	{
		if(num2 < num3 ){
			cout << num1 << num2 << num3 << endl;
		}
		else if(num1 < num3){
			cout << num1 << num3 << num2 << endl;
		}
		else cout << num3 << num1 << num2 << endl;
	}
	else if( num2 < num3 )
	{
		if(num3 < num1){
			cout<< num2 << num3 << num1 << endl;
		}
		else if(num2 < num1 ){
			cout << num2  << num1 << num3 << endl;
		}
		else cout << num1 << num2 << num3 << endl;
	}
	else if( num3 < num1 )
	{
		if(num1 < num2){
			cout<< num3 << num1 << num2 << endl;
		}
		else if(num3 < num2 ){
			cout << num3  << num2 << num1 << endl;
		}
		else cout << num2 << num3 << num1 << endl;

	}
	else
	{
		cout<<"default\n" << num1 << num2 << num3;
		
	}

But there is an easier way . Check the input when you get the data.

int num1(0);
	int num2(0);
	int num3(0);

	int min(0);
	int mid(0);
	int max(0);

	cout << "Enter 3 numbers : ";
	cin >> num1;
	min = max = num1;
	cin >> num2;
	if(num2 > min)
		max = num2;
	else min = num2;
	
	cin >> num3;

	if(num3 > max){
		mid = max;
		max = num3;
	}
	else if(num3 < min){
		mid = min;
		min = num3;

	}
	else mid = num3;

	cout << min << " " << mid <<" "<< max << endl;

	cout<<"\n\n\n";
Comments
how would I modify this if I had a 4th variable?

Thank you so much! You're my hero! And thank you to all the other people that helped me too! You guys are great! But this is the one I could get to work... probably because I am such a noob! :P Thanks!

This question has already been answered. Start a new discussion instead.