Need some help making a program using floats and functions


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Need some help making a program using floats and functions

Hybrid View

  1. #1
    Join Date
    Oct 2006
    Posts
    2

    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 :)

  2. #2
    Join Date
    Nov 2003
    Posts
    4,118
    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.
    Danny Kalev

  3. #3
    Join Date
    Oct 2005
    Location
    Maady
    Posts
    1,819

    Arrow

    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...
    Last edited by Amahdy; 10-27-2006 at 10:33 AM.

  4. #4
    Join Date
    Oct 2006
    Posts
    2

    thanks

    thanks alot for the code and your time.

  5. #5
    Join Date
    Dec 2003
    Posts
    3,366
    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;
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center