home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / BODYMASS.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  2KB  |  76 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /** BODYMASS.C
  4.  *  Calculate body mass index (BMI) for given height and weight.
  5.  *  According to U.S. federal guidelines, a BMI of 24 or less is
  6.  *  desirable. Anything higher is considered overweight.
  7.  *  Donated to the public domain, October 17, 1996.
  8.  **/
  9.  
  10. short BodyMassIndex(unsigned short height, unsigned short weight)
  11. {
  12.       /* Returns the Body Mass Index (BMI) for height in inches and
  13.       ** weight in pounds. BMI is weight in kilograms divided by height
  14.       ** in meters squared. Returns -1 if invalid height entered.
  15.       */
  16.  
  17.       /* Define the metric conversion constants... */
  18.  
  19. #define LBperKG    2.2046f
  20. #define INCHperM  39.37f
  21. #define CFACTOR   ((INCHperM * INCHperM) / LBperKG)
  22.  
  23.       /* Make sure height is not 0 and is 'reasonable' (100?) */
  24.  
  25.       if(height < 1 || height > 100)
  26.             return -1;
  27.  
  28.       return (short) ((((float) weight * CFACTOR) / (height * height)) +
  29.                       0.5f);
  30. }
  31.  
  32. #ifdef TEST
  33.  
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36.  
  37. int main(int argc, char **argv)
  38. {
  39.       short bmi, h, w;
  40.  
  41.       if(argc < 3)
  42.       {
  43.             printf("Usage: bodymass height-in-inches weight-in-pounds\n");
  44.             return EXIT_FAILURE;
  45.       }
  46.       h = atoi(argv[1]);
  47.       w = atoi(argv[2]);
  48.       if (h < 20 || h > 100)
  49.       {
  50.             printf("Height %d out of range!\n", h);
  51.             return EXIT_FAILURE;
  52.       }
  53.       if (w < 20)
  54.       {
  55.             printf("Weight %d out of range!\n", w);
  56.             return EXIT_FAILURE;
  57.       }
  58.       bmi = BodyMassIndex(h, w);
  59.       printf("The Body Mass Index for height %d inches "
  60.              "and weight %d pounds is %d\n", h, w, bmi);
  61.       if (bmi < 25)
  62.             printf("Congratulations! Your index is within the "
  63.                    "recommended range.\n");
  64.       else
  65.       {
  66.             while((bmi = BodyMassIndex(h, --w)) > 24)
  67.                   ;
  68.             printf("Your index is above the recommended level of 24.\n"
  69.                    "To reach that level your weight must drop to %d "
  70.                    "pounds.\n", w);
  71.       }
  72.       return EXIT_SUCCESS;
  73. }
  74.  
  75. #endif
  76.