© Ray Lischner 2020
R. LischnerExploring C++20https://doi.org/10.1007/978-1-4842-5961-0_29

29. Project 1: Body Mass Index

Ray Lischner1 
(1)
Ellicott City, MD, USA
 

It’s project time! Body mass index (BMI) is a measurement some health-care professionals use to determine whether a person is overweight, and if so, by how much. To compute BMI, you need a person’s weight in kilograms and height in meters. The BMI is simply weight/height2, converted to a unitless value.

Your task is to write a program that reads records, prints the records, and computes some statistics. The program should start by asking for a threshold BMI. Only records with a BMI greater than or equal to the threshold will be printed. Each record needs to consist of a person’s name (which may contain spaces), weight in kilograms, height in centimeters (not meters), and the person’s sex ('M' or 'F'). Let the user enter the sex in uppercase or lowercase. Ask the user to enter the height in centimeters, so you can compute the BMI using integers. You will have to adjust the formula to allow for centimeters instead of meters.

Print each person’s BMI immediately after reading his or her record. After collecting information for everyone, print two tables—one for men, one for women—based on the data. Use an asterisk after the BMI rating to mark records for which the number meets or exceeds the threshold. After each table, print the mean (average) and median BMI. (Median is the value at which half the BMI values are less than the median and half are greater than the median. If the user enters an even number of records, take the mean of the two values in the middle.) Compute individual BMI values as integers. Compute the mean and median BMI values as floating-point numbers, and print the mean with one place after the decimal point.

Listing 29-1 shows a sample user session. User input is in boldface.
$ bmi
This program computes Bogus Metabolic Indicator (BMI) values.
Enter threshold BMI: 25
Enter name, height (in cm), and weight (in kg) for each person:
Name 1: Ray Lischner
Height (cm): 180
Weight (kg): 90
Sex (m or f): m
BMI = 27
Name 2: A. Nony Mouse
Height (cm): 120
Weight (kg): 42
Sex (m or f): F
BMI = 29
Name 3: Mick E. Mouse
Height (cm): 30
Weight (kg): 2
Sex (m or f): M
BMI = 22
Name 4: A. Nony Lischner
Height (cm): 150
Weight (kg): 55
Sex (m or f): m
BMI = 24
Name 5: No One
Height (cm): 250
Weight (kg): 130
Sex (m or f): f
BMI = 20
Name 6: ^Z
Male data
Ht(cm) Wt(kg) Sex  BMI  Name
   180     90  M    27* Ray Lischner
    30      2  M    22  Mick E. Mouse
   150     55  M    24  A. Nony Lischner
Mean BMI = 24.3
Median BMI = 24
Female data
Ht(cm) Wt(kg) Sex  BMI  Name
   120     42  F    29* A. Nony Mouse
   250    130  F    20  No One
Mean BMI = 24.5
Median BMI = 24.5
Listing 29-1.

Sample User Session with the BMI Program

Hints

Here are some hints, in case you need them:
  • Keep track of the data in separate vectors, for example, heights, weights, sexes, names, bmis.

  • Use the native locale for all input and output.

  • Divide the program into functions, for example, compute_bmi to compute the BMI from weight and height.

  • You can write this program using nothing more than the techniques that we have covered so far, but if you know other techniques, feel free to use them. The next set of Explorations will present language features that will greatly facilitate writing this kind of program.

  • The complete source code for my solution is available with the other files that accompany this book, but don’t peek until after you have written the program yourself.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.4.181