Writing a function to sort three floats...

Okay, this forum has been a big help, but I'm banging my head on the wall now. I need any help you can offer-just point me in the right direction. Here is my assignment and what I have so far (including a bunch of commented out things I have tried!) Yes, I know I'm not doing what is assigned yet, but I think I am getting there. I don't know how to use pointers here. Help?
Write a function that accepts three floating point values as parameters and sorts them from largest to smallest. The program must use pointers or references and the function will affect (sort) the three variables entered by the user in the calling program (main).
Hint: Look at the three numbers as two pairs: sort the first pair, then the second, then the first again if necessary...



#include <iostream>
float min(float, float, float);
float maximum(float, float, float);
//float mid(float, float, float);
//float sort3(float, float, float);
//float sort2(float, float);
float a, b, c, *pA=0, *pB=0, *pC=0, smallest, largest, middle;
int main()
{
using namespace std;
cout << " This program will sort 3 input numbers from largest to smallest.\n";
cout << "Enter a first number: ";
cin >> a;
cout << "Enter a second number: ";
cin >> b;
cout << "Enter a third number: ";
cin >> c;

cout << "Max number is: " << maximum(a,b,c) << endl;
cout << "Min number is: " << min(a,b,c) << endl;
//cout << "Middle number is: " << mid << endl;
return 0;
}

float maximum( float x, float y, float z )
{
float max = x;
if ( y > max )
max = y;
if ( z > max )
max = z;
return max; //max is largest value
}

float min( float x, float y, float z )
{
float min = x;
if ( y < min )
min = y;
if ( z < min )
min = z;
return min; //min is smallest value
}
//Sort3(a,b,c);
////After calling this function your a, b, and c will be ordered a >= b >= c
//
//void Sort3(float &i, int &j, int &k);
//
////REM: the caller passes in the 3 unsorted values through i,j,k
////The function passes back those 3 values sorted i >= j >= k
//float i,j,k;
//{
//Sort2(i, j);
//Sort2(i, k);
//Sort2(j, k);
//}
//
//void Sort2(float &m, float &n);
////{
//////REM: the caller passes in the 2 unsorted values through m,n
//////The function passes back those 2 values sorted m >= n}
//float tmp
//
//if (m < n)
//
// tmp= m;
// m= n;
// n= tmp;
//}

Your sorting code seems ok. Except that you pass j and k as integers. And that you declare floats i, j, k even though they are function arguments. And that there are ; after your function declarations (when you should be defining them).
Uncomment your code, then try compiling it. If it doesn't compile, tell us the error message. If it doesn't work, tell us the input and the output.
Also, use [code] tags
Last edited on
Okay, I fixed the errors and it compiles now. Apparently, the code is okay, but I still need to output the numbers in order largest to smallest and use pointers in there. Not sure where to go next. Here's the latest code:
#include <iostream>

float min(float, float, float);
float maximum(float, float, float);
//float mid(float, float, float);
float sort3(float, float, float);
float sort2(float, float);
float a, b, c, *pA=0, *pB=0, *pC=0, smallest, largest, middle;
int main()
{
using namespace std;
cout << " This program will sort 3 input numbers from largest to smallest.\n";
cout << "Enter a first number: ";
cin >> a;
cout << "Enter a second number: ";
cin >> b;
cout << "Enter a third number: ";
cin >> c;

cout << "Max number is: " << maximum(a,b,c) << endl;
cout << "Min number is: " << min(a,b,c) << endl;
//cout << "Middle number is: " << mid << endl;
cout << a << b << c << endl;
return 0;
}

float maximum( float x, float y, float z )
{
float max = x;
if ( y > max )
max = y;
if ( z > max )
max = z;
return max; //max is largest value
}

float min( float x, float y, float z )
{
float min = x;
if ( y < min )
min = y;
if ( z < min )
min = z;
return min; //min is smallest value
}
//Sort3(a,b,c);
////After calling this function your a, b, and c will be ordered a >= b >= c
//
void Sort2(float &m, float &n)
{
////REM: the caller passes in the 2 unsorted values through m,n
////The function passes back those 2 values sorted m >= n}
float tmp;

if (m < n)

tmp= m;
m= n;
n= tmp;
}
void Sort3(float &i, float &j, float &k)

////REM: the caller passes in the 3 unsorted values through i,j,k
////The function passes back those 3 values sorted i >= j >= k
//float i,j,k;
{
Sort2(i, j);
Sort2(i, k);
Sort2(j, k);
}




So, at the end of main add
1
2
Sort3(a, b, c);
cout << "sorted: " << a << " " << b << " " << c;

your assignment wrote:
The program must use pointers or references
You know, float &i is a reference..
Okay, that makes sense. But now if I run it I get
1>c:\users\laura\desktop\practice\practice\practice.cpp(23) : error C3861: 'Sort3': identifier not found
And -duh! I don't know what I was thinking about the poiners & references. Somehow I was thinking I had to do both...
You have to declare functions before using them. Add void Sort3(float &i, float &j, float &k); before main().
Okay, I did that. I thought the way I had it before (I was using float instead of void) was okay, but I fixed it. Now it compiles but gives me a run-time error #3 saying the variable tmp is being used without being initialized. I tried putting it with a b and c as a float, but it didn't like that either....you are soooo helping me. Hopefully it is over soon!
Just thought I would try putting the cleaned up code here so you can see it. Thanks again.
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
#include <iostream>

float min(float, float, float);
float maximum(float, float, float);
void Sort3(float &i, float &j, float &k);
float sort2(float, float);
float a, b, c;
float tmp=0;


int main()
{
using namespace std;
cout << " This program will sort 3 input numbers from largest to smallest.\n";
cout << "Enter a first number: ";
cin >> a;
cout << "Enter a second number: ";
cin >> b;
cout << "Enter a third number: ";
cin >> c;

cout << "Max number is: " << maximum(a,b,c) << endl;
cout << "Min number is: " << min(a,b,c) << endl;

Sort3(a, b, c);
cout << "Sorted: " << a << " " << b << " " << c;
 
return 0;
}

float maximum( float x, float y, float  z)  
{  
float max = x;  
if ( y > max )  
max = y;  
if ( z > max )  
max = z;     
return max; //max is largest value  
}

float min( float x, float y, float  z )  
{  
float min = x;  
if ( y < min )  
min = y;  
if ( z < min )  
min = z;     
return min; //min is smallest value
}
Why only half of the code? I don't see anything wrong here, but maybe if you posted the full code, I would.
Ugh...sorry. The problem I'm having is with "tmp." I keep trying to initialize it in different ways, but it's not working.

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>

float min(float, float, float);
float maximum(float, float, float);
void Sort3(float &i, float &j, float &k);
float sort2(float, float);
float a, b, c;
//float tmp=0;


int main()
{
using namespace std;
float tmp=0;
cout << " This program will sort 3 input numbers from largest to smallest.\n";
cout << "Enter a first number: ";
cin >> a;
cout << "Enter a second number: ";
cin >> b;
cout << "Enter a third number: ";
cin >> c;

cout << "Max number is: " << maximum(a,b,c) << endl;
cout << "Min number is: " << min(a,b,c) << endl;

Sort3(a,b,c);
cout << "Sorted: " << a << " " << b << " " << c;
 
return 0;
}

float maximum( float x, float y, float  z)  
{  
float max = x;  
if ( y > max )  
max = y;  
if ( z > max )  
max = z;     
return max; //max is largest value  
}

float min( float x, float y, float  z )  
{  
float min = x;  
if ( y < min )  
min = y;  
if ( z < min )  
min = z;     
return min; //min is smallest value
}

void Sort2(float &m, float &n) 
{
float tmp;

if (m < n)

	tmp= m;
	m= n;
	n= tmp;
}
void Sort3(float &i, float &j, float &k)

{
Sort2(i, j);
Sort2(i, k);
Sort2(j, k);
 
}
Oh yes. I see it now. You code:
1
2
3
if (m < n) tmp= m;
m= n;
n= tmp;

tmp is initialized only if m < n. Wrap all three lines in {}.
Thank you thank you thank you...phew
Topic archived. No new replies allowed.