C BMI分类结构
我正在做一项C语言的作业,我必须阅读多个人的身高和体重,并确定他们的bmi。然后我将他们分为各自的bmi类别,但我一直在思考如何正确地做到这一点,这是我迄今为止的代码:C BMI分类结构,c,C,我正在做一项C语言的作业,我必须阅读多个人的身高和体重,并确定他们的bmi。然后我将他们分为各自的bmi类别,但我一直在思考如何正确地做到这一点,这是我迄今为止的代码: # include <stdio.h> int main () { int people; double bmi, weight, inches; printf("How many peoples? > "); scanf("%d", &am
# include <stdio.h>
int main () {
int people;
double bmi, weight, inches;
printf("How many peoples? > ");
scanf("%d", &people);
do {
printf("Enter height (inches) and weight (lbs) (%d left) > ", people);
scanf("%lf %lf", &inches, &weight);
people--;
}
while (people > 0);
bmi = (weight / (inches * inches)) * 703;
if (bmi < 18.5) {
printf("Under weight: %d\n", people);
}
else if (bmi >= 18.5 && bmi < 25) {
printf("Normal weight: %d\n", people);
}
else if (bmi >= 25 && bmi < 30) {
printf("Over weight: %d\n", people);
}
else if (bmi >= 30) {
printf("Obese: %d\n", people);
}
return 0;
}
#包括
int main(){
国际人士;
双倍bmi,体重,英寸;
printf(“有多少人?>”;
scanf(“%d”和人);
做{
printf(“输入身高(英寸)和体重(磅)(%d左)>”,人);
扫描量(“%lf%lf”、&英寸和重量);
人--;
}
而(人>0);
体重指数=(体重/(英寸*英寸))*703;
如果(体重指数<18.5){
printf(“体重不足:%d\n”,人);
}
否则,如果(体重指数>=18.5&&bmi<25){
printf(“正常体重:%d\n”,人);
}
否则,如果(体重指数>=25&&bmi<30){
printf(“超重:%d\n”,人);
}
否则,如果(体重指数>=30){
printf(“肥胖者:%d\n”,人);
}
返回0;
}
我哪里做错了?在哪里修复此代码?使用一些数据结构来存储数据。您正在为多个人获取输入,但最终为一个人处理 还有
人--代码>已完成。因此,people
变量递减到零,这使得while
在不执行BMI计算的情况下退出
修改代码:
#include <stdio.h>
#define MAX_PEOPLE 100
int main () {
int people;
double bmi[MAX_PEOPLE], weight[MAX_PEOPLE], inches[MAX_PEOPLE];
int index = 0;
printf("How many peoples? > ");
scanf("%d", &people);
index = people;
do {
printf("Enter height (inches) and weight (lbs) (%d left) > ", index);
scanf("%lf %lf", &inches[index], &weight[index]);
index--;
}while (index > 0);
for(index = 0; index < people; index++)
{
bmi[index] = (weight[index] / (inches[index] * inches[index])) * 703;
if (bmi[index] < 18.5) {
printf("Under weight: %d\n", index);
}
else if (bmi[index] >= 18.5 && bmi[index] < 25) {
printf("Normal weight: %d\n", index);
}
else if (bmi[index] >= 25 && bmi[index] < 30) {
printf("Over weight: %d\n", index);
}
else if (bmi[index] >= 30) {
printf("Obese: %d\n", index);
}
}
return 0;
}
#包括
#定义最多100人
int main(){
国际人士;
双倍bmi[最大人数],体重[最大人数],英寸[最大人数];
int指数=0;
printf(“有多少人?>”;
scanf(“%d”和人);
指数=人;
做{
printf(“输入高度(英寸)和重量(磅)(%d左)>”,索引);
扫描量(“%lf%lf”、&英寸[索引]、&重量[索引];
索引--;
}而(指数>0);
用于(索引=0;索引<人;索引+)
{
体重指数=(体重指数)/(英寸指数)*英寸指数)*703;
如果(体重指数[指数]<18.5){
printf(“重量不足:%d\n”,索引);
}
否则如果(bmi[指数]>=18.5&&bmi[指数]<25){
printf(“正常重量:%d\n”,索引);
}
否则如果(bmi[指数]>=25&&bmi[指数]<30){
printf(“超重:%d\n”,索引);
}
否则如果(bmi[指数]>=30){
printf(“肥胖:%d\n”,索引);
}
}
返回0;
}
使用一些数据结构来存储数据。您正在为多个人获取输入,但最终为一个人处理
还有人--代码>已完成。因此,people
变量递减到零,这使得while
在不执行BMI计算的情况下退出
修改代码:
#include <stdio.h>
#define MAX_PEOPLE 100
int main () {
int people;
double bmi[MAX_PEOPLE], weight[MAX_PEOPLE], inches[MAX_PEOPLE];
int index = 0;
printf("How many peoples? > ");
scanf("%d", &people);
index = people;
do {
printf("Enter height (inches) and weight (lbs) (%d left) > ", index);
scanf("%lf %lf", &inches[index], &weight[index]);
index--;
}while (index > 0);
for(index = 0; index < people; index++)
{
bmi[index] = (weight[index] / (inches[index] * inches[index])) * 703;
if (bmi[index] < 18.5) {
printf("Under weight: %d\n", index);
}
else if (bmi[index] >= 18.5 && bmi[index] < 25) {
printf("Normal weight: %d\n", index);
}
else if (bmi[index] >= 25 && bmi[index] < 30) {
printf("Over weight: %d\n", index);
}
else if (bmi[index] >= 30) {
printf("Obese: %d\n", index);
}
}
return 0;
}
#包括
#定义最多100人
int main(){
国际人士;
双倍bmi[最大人数],体重[最大人数],英寸[最大人数];
int指数=0;
printf(“有多少人?>”;
scanf(“%d”和人);
指数=人;
做{
printf(“输入高度(英寸)和重量(磅)(%d左)>”,索引);
扫描量(“%lf%lf”、&英寸[索引]、&重量[索引];
索引--;
}而(指数>0);
用于(索引=0;索引<人;索引+)
{
体重指数=(体重指数)/(英寸指数)*英寸指数)*703;
如果(体重指数[指数]<18.5){
printf(“重量不足:%d\n”,索引);
}
否则如果(bmi[指数]>=18.5&&bmi[指数]<25){
printf(“正常重量:%d\n”,索引);
}
否则如果(bmi[指数]>=25&&bmi[指数]<30){
printf(“超重:%d\n”,索引);
}
否则如果(bmi[指数]>=30){
printf(“肥胖:%d\n”,索引);
}
}
返回0;
}
现在您正在处理相同的数据
每次为权重指定新值时,旧值都会被删除
您可以创建多个变量,如下所示:
双重权重1,权重2,权重3,权重4,
…等等(非常不实际!!)
或
创建一个双精度数组:
double weight[100];
并参考每个特定的双变量,如下所示:
scanf("%lf %lf", inches[0], weight[0]);
scanf("%lf %lf", inches[1], weight[1]);
scanf("%lf %lf", inches[2], weight[2]);
你明白我的意思了吗?您可以操作数组tru afor loop现在您正在处理相同的数据
每次为权重指定新值时,旧值都会被删除
您可以创建多个变量,如下所示:
双重权重1,权重2,权重3,权重4,
…等等(非常不实际!!)
或
创建一个双精度数组:
double weight[100];
并参考每个特定的双变量,如下所示:
scanf("%lf %lf", inches[0], weight[0]);
scanf("%lf %lf", inches[1], weight[1]);
scanf("%lf %lf", inches[2], weight[2]);
你明白我的意思了吗?您可以操作数组tru afor loop您是否建议分配另一个变量来表示“people--”,比如y=people--;?谢谢我将使用你所做的,重新编写我的代码,这样我就可以确保我完全理解它。因此,我能够使用你的示例重新编写代码,但我需要以某种方式获得的输出,在使用它一段时间后,我无法获得它,我的输出应该如下所示:处理了3个人,发现:体重不足:0正常体重:1超重:1肥胖:1警告!人口可能是不健康的,因为它实际上会打印出所发现类别的所有0,然后我加上超重和肥胖的总值,如果该值超过总值的一半,那么我会打印警告。我似乎不知道如何始终存储数据,所以我总是为每个类别打印,即使它为零,那么如何引用回该数据才能再次与se进行比较呢