I have a program simulating a BMI (Body Mass Index) Calculator that takes in inputs from 10 users asking each user for a weight and a height, the program then calculates the BMI and prints the result of the computation in a formatted output.
我有一个模拟BMI(身体质量指数)计算器的程序,该计算器接收来自10个用户的输入,询问每个用户的重量和高度,然后程序计算BMI并在格式化的输出中打印计算结果。
Something like this:
像这样的东西:
USER BMI STATUS
1
:
10
So I thought of this, I would take in the inputs from the users first, then try to display the computation of the inputs in the above formatted way. Here's what I've tried to come up with which keeps asking the users for their weight and height until the 10th user.
所以我想到了这一点,我会首先接受用户的输入,然后尝试以上面的格式化方式显示输入的计算。以下是我试图提出的问题,一直向用户询问他们的体重和身高,直到第10位用户。
#include <stdio.h>
#include <math.h>
int main(void) {
float weight_value = 0.0f;
float user_height = 0.0f;
float BMI = 0.0f;
char BMI_Status[30];
int i;
for ( i = 1; i <= 10; i++)
{
printf("Please enter your weight: ");
scanf("%f", &weight_value); //Reads in the user's weight
printf("Now enter your height: ");
scanf("%f", &user_height); //Reads in the user's height
BMI = weight_value / (user_height * user_height);
}
return 0;
}
The BMI Status is a string that displays different states of the computed BMI values, which are "Underweight", "Normal", "Overweight"
for when the BMI value is within a given range.
BMI状态是一个字符串,显示计算的BMI值的不同状态,当BMI值在给定范围内时,它们是“体重不足”,“正常”,“超重”。
The code snippet above is still incomplete as I'm still finding it difficult to add the rest of the code. Here are the issues I'm trying to solve.
上面的代码片段仍然不完整,因为我仍然发现很难添加其余的代码。以下是我要解决的问题。
-
How can I print out the BMI values after I have taken in the 10 inputs?
在接收10个输入后,如何打印出BMI值?
I have tried having a nested for loop that prints out the values, but this is illogical as there will now be an output of the BMI for every iteration of the outer for loop, so I'm guessing need a way to make
scanf()
hold the BMI values after the for loop's block.我试过有一个嵌套的for循环打印出来的值,但这是不合逻辑的,因为现在外部for循环的每次迭代都会有BMI的输出,所以我猜想需要一种方法来制作scanf()在for循环块之后保持BMI值。
-
How do I make the status strings appear?
如何显示状态字符串?
I know there's no
string
datatype in C, so I'll have to declare achar
array that holds the string for the status, but these are different strings for different statuses, and it has to be printed out for every line. I thought of havingif else if else
conditions for this but I just can't get thechar
array to work.我知道C中没有字符串数据类型,所以我必须声明一个包含状态字符串的char数组,但这些是不同状态的不同字符串,并且必须为每一行打印出来。我想如果有其他if条件,但我不能让char数组工作。
I already successfully wrote a code that prints out the user's BMI at every iteration, but this isn't how its supposed to be done.
我已经成功编写了一个代码,可以在每次迭代时打印出用户的BMI,但这不是它应该如何完成的。
Another thing I thought of was keeping the BMI values in an array of floating point numbers then displaying the values from the array in the above formatted way, but I'm still a bit sceptical about how I intend doing this.
我想到的另一件事是将BMI值保存在浮点数数组中,然后以上面格式化的方式显示数组中的值,但我仍然对我打算如何做这一点持怀疑态度。
4 个解决方案
#1
2
Here the complete code to display the use of arrays to store the values. Your idea of using an if else for displaying how overweight a person is was good and so I implemented it.
这里显示了使用数组来存储值的完整代码。你使用if else来显示一个人超重的想法是好的,所以我实现了它。
#include <stdio.h>
#include <math.h>
int main(void) {
float weight_value[10]; //two arrays of ten elements each
float user_height[10];
float BMI = 0.0f;
char BMI_Status[30];
int i;
for ( i = 0; i < 10; i++) //the array starts from zero in C
{
printf("Please enter your weight: ");
scanf("%f", &weight_value[i]); //store the values in the array
printf("Now enter your height: ");
scanf("%f", &user_height[i]); //store the values in the array
}
printf("USER BMI\t\tSTATUS\n");
for( i = 0; i < 10; i++) //pass the array once again and calculate the needed values
{
BMI = weight_value[i] / (user_height[i] * user_height[i]);
printf("%d\t%f\t", i+1, BMI);
if(BMI < 30)
printf("Underweight\n");
else if(BMI < 50)
printf("Normal\n");
else
printf("Overweight\n");
}
return 0;
}
#2
1
edited: skip to last
编辑:跳到最后
you will need to save them somewhere before you can read them to display it, you need to use an array to store all 10 inputs, at the moment every time a user inputs a data it will rewrite the data that was in the variable, which had the last user's data.
您需要将它们保存到某个地方才能读取它们以显示它,您需要使用一个数组来存储所有10个输入,此时每次用户输入数据时它将重写变量中的数据,有最后一个用户的数据。
it needs to look like this:
它需要看起来像这样:
...
float weight_value[10];
float user_height[10];
float BMI =[10];
char BMI_Status[30][3];
for ( int i = 1; i <= 10; i++)
{
printf("Please enter your weight: ");
scanf("%f", &weight_value[i]); //Reads in the user's weight
printf("Now enter your height: ");
scanf("%f", &user_height[i]); //Reads in the user's height
BMI[i] = weight_value / (user_height * user_height);
}
...
then to display you will need a loop like this
然后显示你需要一个像这样的循环
...
printf("user \t weight \t height \t BMi");
for ( int i = 1; i <= 10; i++)
{
printf("%d \t%f \t%f \t%s ", i,user_weight[i],user_height[i],BMI[i]);
}
...
i just re-read the question and realised i misread, however the last bit shows how you can read them from the array and display them.
我只是重新阅读了这个问题并意识到我误读了,但最后一点显示了如何从数组中读取它们并显示它们。
#3
1
The following code will store the weight and height of 4 users and print their BMI for an arbitrary number of thresholds (set to two in this case).
以下代码将存储4个用户的权重和高度,并将其BMI打印为任意数量的阈值(在本例中设置为2)。
You can solve problem 1. by storing the weight and height into arrays (in the first loop).
您可以通过将权重和高度存储到数组中来解决问题1.(在第一个循环中)。
You can then display a BMI status string by first defining a set of thresholds and a corresponding status string. In the second loop, for every user you compute and check whether the BMI value is under a given threshold and print its corresponding status string.
然后,您可以通过首先定义一组阈值和相应的状态字符串来显示BMI状态字符串。在第二个循环中,为您计算的每个用户检查BMI值是否低于给定阈值并打印其对应的状态字符串。
#include <stdio.h>
#include <math.h>
#include <stdbool.h>
#define USERS 4
#define THRESHOLDS 2
int main(void) {
float weight[USERS];
float height[USERS];
float BMI = 0.0f;
char * BMI_Status[THRESHOLDS+1] = {"Normal","Overweight","Obese"};
float thresholds[THRESHOLDS] = {10.0,20.0};
int i,j;
for ( i = 0; i < USERS; i++)
{
printf("Please enter your weight: ");
scanf("%f", &(weight[i])); //Reads in the user's weight
printf("Now enter your height: ");
scanf("%f", &(height[i])); //Reads in the user's height
}
for ( i = 0; i < USERS; i++)
{
BMI = weight[i]/(height[i]*height[i]);
printf("%d\t%f\t",i,BMI);
for ( j = 0 ; j < THRESHOLDS ; j++ ){
if ( BMI < thresholds[j]){
break;
}
}
printf("%s\n",BMI_Status[j]);
}
return 0;
}
#4
1
- Add an
if
statement in the for-loop
在for循环中添加if语句
float BMI[10]; int j; for ( i = 1; i <= 10; i++) { printf("Please enter your weight: "); scanf("%f", &weight_value); //Reads in the user's weight printf("Now enter your height: "); scanf("%f", &user_height); //Reads in the user's height BMI[i-1] = weight_value / (user_height * user_height); if (i==10) for (j=0; j<10; j++) printf ("%d\n", BMI[j]); }
- Use
char **a
orchar *a[]
for example:
使用char ** a或char * a []例如:
char s[12] = "Hello World!"; char *p; p = &s[0]; char *BMI_Status[30]; //declare 30 char* BMI_Status[0] = "Fat"; BMI_Status[1] = "Thin"; ... //output printf ("%s", BMI_Status[0]); //Fat
#1
2
Here the complete code to display the use of arrays to store the values. Your idea of using an if else for displaying how overweight a person is was good and so I implemented it.
这里显示了使用数组来存储值的完整代码。你使用if else来显示一个人超重的想法是好的,所以我实现了它。
#include <stdio.h>
#include <math.h>
int main(void) {
float weight_value[10]; //two arrays of ten elements each
float user_height[10];
float BMI = 0.0f;
char BMI_Status[30];
int i;
for ( i = 0; i < 10; i++) //the array starts from zero in C
{
printf("Please enter your weight: ");
scanf("%f", &weight_value[i]); //store the values in the array
printf("Now enter your height: ");
scanf("%f", &user_height[i]); //store the values in the array
}
printf("USER BMI\t\tSTATUS\n");
for( i = 0; i < 10; i++) //pass the array once again and calculate the needed values
{
BMI = weight_value[i] / (user_height[i] * user_height[i]);
printf("%d\t%f\t", i+1, BMI);
if(BMI < 30)
printf("Underweight\n");
else if(BMI < 50)
printf("Normal\n");
else
printf("Overweight\n");
}
return 0;
}
#2
1
edited: skip to last
编辑:跳到最后
you will need to save them somewhere before you can read them to display it, you need to use an array to store all 10 inputs, at the moment every time a user inputs a data it will rewrite the data that was in the variable, which had the last user's data.
您需要将它们保存到某个地方才能读取它们以显示它,您需要使用一个数组来存储所有10个输入,此时每次用户输入数据时它将重写变量中的数据,有最后一个用户的数据。
it needs to look like this:
它需要看起来像这样:
...
float weight_value[10];
float user_height[10];
float BMI =[10];
char BMI_Status[30][3];
for ( int i = 1; i <= 10; i++)
{
printf("Please enter your weight: ");
scanf("%f", &weight_value[i]); //Reads in the user's weight
printf("Now enter your height: ");
scanf("%f", &user_height[i]); //Reads in the user's height
BMI[i] = weight_value / (user_height * user_height);
}
...
then to display you will need a loop like this
然后显示你需要一个像这样的循环
...
printf("user \t weight \t height \t BMi");
for ( int i = 1; i <= 10; i++)
{
printf("%d \t%f \t%f \t%s ", i,user_weight[i],user_height[i],BMI[i]);
}
...
i just re-read the question and realised i misread, however the last bit shows how you can read them from the array and display them.
我只是重新阅读了这个问题并意识到我误读了,但最后一点显示了如何从数组中读取它们并显示它们。
#3
1
The following code will store the weight and height of 4 users and print their BMI for an arbitrary number of thresholds (set to two in this case).
以下代码将存储4个用户的权重和高度,并将其BMI打印为任意数量的阈值(在本例中设置为2)。
You can solve problem 1. by storing the weight and height into arrays (in the first loop).
您可以通过将权重和高度存储到数组中来解决问题1.(在第一个循环中)。
You can then display a BMI status string by first defining a set of thresholds and a corresponding status string. In the second loop, for every user you compute and check whether the BMI value is under a given threshold and print its corresponding status string.
然后,您可以通过首先定义一组阈值和相应的状态字符串来显示BMI状态字符串。在第二个循环中,为您计算的每个用户检查BMI值是否低于给定阈值并打印其对应的状态字符串。
#include <stdio.h>
#include <math.h>
#include <stdbool.h>
#define USERS 4
#define THRESHOLDS 2
int main(void) {
float weight[USERS];
float height[USERS];
float BMI = 0.0f;
char * BMI_Status[THRESHOLDS+1] = {"Normal","Overweight","Obese"};
float thresholds[THRESHOLDS] = {10.0,20.0};
int i,j;
for ( i = 0; i < USERS; i++)
{
printf("Please enter your weight: ");
scanf("%f", &(weight[i])); //Reads in the user's weight
printf("Now enter your height: ");
scanf("%f", &(height[i])); //Reads in the user's height
}
for ( i = 0; i < USERS; i++)
{
BMI = weight[i]/(height[i]*height[i]);
printf("%d\t%f\t",i,BMI);
for ( j = 0 ; j < THRESHOLDS ; j++ ){
if ( BMI < thresholds[j]){
break;
}
}
printf("%s\n",BMI_Status[j]);
}
return 0;
}
#4
1
- Add an
if
statement in the for-loop
在for循环中添加if语句
float BMI[10]; int j; for ( i = 1; i <= 10; i++) { printf("Please enter your weight: "); scanf("%f", &weight_value); //Reads in the user's weight printf("Now enter your height: "); scanf("%f", &user_height); //Reads in the user's height BMI[i-1] = weight_value / (user_height * user_height); if (i==10) for (j=0; j<10; j++) printf ("%d\n", BMI[j]); }
- Use
char **a
orchar *a[]
for example:
使用char ** a或char * a []例如:
char s[12] = "Hello World!"; char *p; p = &s[0]; char *BMI_Status[30]; //declare 30 char* BMI_Status[0] = "Fat"; BMI_Status[1] = "Thin"; ... //output printf ("%s", BMI_Status[0]); //Fat