Exercises 1 to 5 calculate BMI (Body Mass Index), an index related to obesity, with input of height and weight. Final program is composed of a few functions. Start with Exercise 1 step by step.
Below is a source code to calculate BMI with input of height and weight. In the source code, a function, "bmi_cal", is defined to calculate BMI. Write the source code, save it as "func1.c", and confirm whether it can run properly.
/*
Calculation of BMI (Body Mass Index)
*/
#include <stdio.h>
double bmi_cal(double h, double w) {
// BMI = weight(kg) / height(m) / height(m)
return w/h/h;
}
int main(void) {
double height, weight, bmi;
printf("Input height(m) and weight(kg) ");
scanf("%lf%lf", &height, &weight);
// Calculation of BMI
bmi = bmi_cal(height, weight);
printf("%5.2f m, %6.2f kg: BMI = %6.2f\n", height, weight, bmi);
return (0);
}
(Execution result) $ $ ./a.out Input height(m) and weight(kg) 1.65 70 1.65 m, 70.00 kg: BMI = 25.71 $
Below is a source code to calculate BMI with input of height and weight. In the source code, a function, "bmi_cal", is defined to calculate BMI. Write the source code, complement the expression hidden by "******" and save it as "func2.c". Confirm whether it can run properly.
/*
Calculation of BMI (Body Mass Index) and Standard weight
*/
#include <stdio.h>
double bmi_cal(double h, double w) {
// BMI = weight(kg) / height(m) / height(m)
return w/h/h;
}
double std_weight(double h) {
// Standard weight (kg) = height(m) * height(m) * 22
return h*h*22;
}
int main(void) {
double height, weight, bmi, sweight;
printf("Input height(m) and weight(kg) ");
scanf("%lf%lf", &height, &weight);
// Calculation of BMI
bmi = bmi_cal(height, weight);
printf("%5.2f m, %6.2f kg: BMI = %6.2f\n", height, weight, bmi);
// Calculation of standard weight
sweight = std_weight(***********);
printf("%5.2f m, %6.2f kg: Standard weight = %6.2f kg\n", height, weight, sweight);
return (0);
}
(Execution result) $ $ ./a.out Input height(m) and weight(kg) 1.60 68.5 1.60 m, 68.50 kg: BMI = 26.76 1.60 m, 68.50 kg: Standard weight = 56.32 kg $
Below source code is almost same as func2.c, but values returned by function are different. With reference to such difference, type the source code and save it as "func3.c".
/*
Calculation of BMI (Body Mass Index) and Standard weight
*/
#include <stdio.h>
void bmi_cal(double h, double w) {
// BMI = weight(kg) / height(m) / height(m)
printf("%5.2f m, %6.2f kg: BMI = %6.2f\n", h, w, w/h/h);
}
void std_weight(double h) {
// Standard weight (kg) = height(m) * height(m) * 22
printf("%5.2f m: Standard weight = %6.2f kg\n", h, h*h*22);
}
int main(void) {
double height, weight;
printf("Input height(m) and weight(kg) ");
scanf("%lf%lf", &height, &weight);
// Calculation of BMI
bmi_cal(height, weight);
// Calculation of standard weight
std_weight(***********);
return (0);
}
(Execution result) $ $ ./a.out Input height(m) and weight(kg) 1.60 68.5 1.60 m, 68.50 kg: BMI = 26.76 1.60 m, Standard weight = 56.32 kg $
Based on func2.c, below source code is intended to show message of obesity. Write the source code, complement the expression hidden by "******" and save it as "func4.c". Confirm whether it can run properly.
/*
Calculation of BMI (Body Mass Index) and Standard weight
*/
#include <stdio.h>
double bmi_cal(double h, double w) {
// BMI = weight(kg) / height(m) / height(m)
return w/h/h;
}
double std_weight(double h) {
// Standard weight (kg) = height(m) * height(m) * 22
return h*h*22;
}
void message(double h, double w, double bmi, double sweight) {
printf("%5.2f m, %6.2f kg: BMI = %6.2f\n", h, w, w/h/h);
printf("%5.2f m: Standard weight = %6.2f kg\n", h, h*h*22);
if (bmi < 18.5) {
printf("You have low weight\n");
} else if (bmi < 25) {
printf("You have normal weight\n");
} else if (bmi < 30) {
printf("Obesity (1st-degree)\n");
} else if (bmi < 35) {
printf("Obesity (2nd-degree)\n");
} else if (bmi < 40) {
printf("Obesity (3rd-degree)\n");
} else {
printf("Obesity (4th-degree)\n");
}
}
int main(void) {
double height, weight, bmi, sweight;
printf("Input height(m) and weight(kg) ");
scanf("%lf%lf", &height, &weight);
// Calculation of BMI
bmi = bmi_cal(height, weight);
printf("%5.2f m, %6.2f kg: BMI = %6.2f\n", h, w, bmi);
// Calculation of standard weight
sweight = std_weight(***********);
// Message of obesity
message(********************);
return (0);
}
(Execution result) $ $ ./a.out Input height(m) and weight(kg) 1.60 68.5 1.60 m, 68.50 kg: BMI = 26.76 1.60 m, Standard weight = 56.32 kg Obesity (1st-degree) $
Based on func4.c, we want to keep showing the program until Ctrl-d is input. Write the source code to meet this requirement using while-loop or for-loop, save it as "func5.c", and confirm whether it can run properly.
(Execution result) $ $ ./a.out [No. 1] Input height(m) and weight(kg) 1.80 78 1.80 m, 78.00 kg: BMI = 24.07 1.80 m, Standard weight = 71.28 kg You have low weight [No. 2] Input height(m) and weight(kg) 1.56 70 1.56 m, 70.00 kg: BMI = 28.76 1.56 m, Standard weight = 53.54 kg Obesity (1st-degree) [No. 3] Input height(m) and weight(kg) 1.60 90 1.60 m, 90.00 kg: BMI = 35.16 1.60 m, Standard weight = 56.32 kg Obesity (3rd-degree) [No. 4] Input height(m) and weight(kg) <- Ctrl + d $