# Need some help making a program using floats and functions

• 10-26-2006, 11:54 PM
promethium
Need some help making a program using floats and functions
I need to use a function that accepts three floating point values as parameters and sorts them from largest to smallest. The program must use one or more pointers and the function will affect the three variables input in the calling program (main). I can use 3 if statements. Thanks for any help you can provide me. Im new to floats :)
• 10-27-2006, 02:29 AM
Danny
you can use the max() macro to compare each pair:
float bigger=max(a,b);
float biggest = max(bigger, c);

float smallest = (bigger == a?b:a;)
now you have the three number sorted: smallest, bigger and biggest.
• 10-27-2006, 10:28 AM
Amahdy
Complex way !!! :

I'll point to the smallest with s; bigger with b; biggest with z.
U have 6 metods of arrangments:
1. s b w
2. s w b
3. b s w
4. b w s
5. w s b
6. w b s

use those numerical values as return values from the function;
Assume the three values are a,b and c:
use this: //I'll ignore the equality case.
Code:

```===================================       if(a<b&&a<c)if(b<c)return 1;else return 2; else if(b<a&&b<c)if(a<c)return 3;else return 4; else                  if(c<a)return 5;else return 6; ===================================```
Note that they are only 3 if statements :
the first: if(..)...else if(..)...else...
the second and the third are : if(..)...else...
• 10-27-2006, 02:22 PM
promethium
thanks
thanks alot for the code and your time.
• 10-27-2006, 05:14 PM
jonnin
float * sort3(float &one, float &two, float &three)
{
static float f[3];
float * fp = f; //check, used a pointer in the code

insert what danny said here, assigning fp[0] to smallest, fp[2] to largest, and
fp[1] the middle value.

one = two = three = 0.0; //affect the incoming variables. I *love* useless requirements, dont you?

return fp;
}