sorting integers in ascending order

So for my first year programming class, we need to write a little program that gets 3 integers in the main function, and then uses another function to sort them in to ascending order, and then use the main function to print them again. I'm sure there's some little dinky thing I'm missing, but I just can't seem to find what the problem is. compiling doesn't give me any errors, but the console only displays the first integer put in, regardless of order, and does nothing with the other 2.

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
51
52
53
54
55
56
57
58
59
60
61
#include<iostream>
#include<conio.h>		//load dem libraries
using namespace std;

double sort3 (double first, double second, double third);		//declare the sort function

double first;
double second;
double third;		//declare variables

int main()

{

	cout << "Hello, please input three integers" << endl;		//ask for 3 integers to be sorted

	cin >> first, second, third;		//input them

	double order = sort3 (first, second, third);		//assign the returned value to a useable value 

	cout << "The order is " << narf;				//output the order

	getch();		//do not killself

}

double sort3 (double first, double second, double third)		//the sort function

{

	if (first <= second && second <= third)		//these are all conditions to see what the order of the integers will be
	{
		return first, second, third;	//123
	}

	else if (first <= third && third <= second)
	{
		return first, third,  second;	//132
	}

	else if (second <= first && first <= third)
	{
		return second, first, third;	//213
	}

	else if (second <= third && third <= first)
	{
		return second, third, first; //231
	}
	
	else if (third <= first && first <= second)
	{
		return third, first, second;	//312
	}

	else if (third <= second && second <= first)
	{
		return third, second, first;	//321
	}

}
a function can only have one return value. When you write return first, second, third; this is actually the same as return third;
okay, so could I do something like...

declare an order variable, and then

1
2
3
4
if (first <= second && second <= third)		
       {
		order = first, second, third;	//123
	}


the rest of the comparisons....
 
return order;


yes?
A variable can only hold one value. You should read into arrays.
Well I know how to do arrays, but at this point in the class we haven't been taught them so I was under the impression we were supposed to figure out how it's done without using them. But I guess doing it wrong is better than it not working. Thanks
what in particular is wrong with this code? I've written programs that have 'return x' in the same function, so long as they are not in the same scope, so that shouldn't be the issue. But the printed text always reads "0the order is: 0" ... do I need to write the program over from scratch, replacing 'double first, second, third' with an array? or is there a quick fix I'm missing? I think the problem is that 'get' is a double, and 'order' is an array, so it can't print out all of the variables, but I can't make 'get' an array because it cant' convert an array to a double (as in sort3)... what to do?

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include<iostream>
#include<conio.h>		//load dem libraries
using namespace std;

double sort3 (double first, double second, double third);		//declare the sort function

double first;
double second;
double third;		//declare variables
int order[3];

int main()

{

	cout << "Hello, please input three integers" << endl;		//ask for 3 integers to be sorted

	cin >> first, second, third;		//input them

	double get = sort3 (first, second, third);		//assign the returned value to a useable value 

	cout << "The order is " <<get;  				//output the order

	getch();		//do not killself

}

double sort3 (double first, double second, double third)		//the sort function

{

	if (first <= second && second <= third)		//these are all conditions to see what the order of the integers will be
	{
		order[0,1,2] = first, second, third;	//123
		return order[0,1,2];
		
	}

	else if (first <= third && third <= second)
	{
		order[0,1,2] = first, third,  second;	//132
		return order[0,1,2];
	}

	else if (second <= first && first <= third)
	{
		order[0,1,2] = second, first, third;	//213
		return order[0,1,2];
	}

	else if (second <= third && third <= first)
	{
		cout << second, third, first; //231
		return order[0,1,2];
	}
	
	else if (third <= first && first <= second)
	{
		order[0,1,2] = third, first, second;	//312
		return order[0,1,2];
	}

	else if (third <= second && second <= first)
	{
		order[0,1,2] = third, second, first;	//321
		return order[0,1,2];
	}


}
Not the best solution and not sure if it's allowed, but you can make first, second and third globals, so you don't need to pass or return them.
they are globals though, aren't they?
One, you can't return an array, and two, a function can only have one return value. You really need to listen to what other people have posted, there's no point in asking for help if you just ignore it.

Also, try a sorting algorithm, it will make your code much simpler and nicer.
Last edited on
Topic archived. No new replies allowed.