2016-03-28 835 views
-2

计算体重指数。 身体质量指数将您的体重与身高进行比较,计算方法是将体重以千克除以身高(以米为单位)。它给你一个关于你是否体重不足,体重健康,超重或肥胖的想法。如何使用函数计算BMI?

身体质量指数分类:

  1. 体重不足= < 18.5
  2. 正常体重= 18.5-24.9
  3. 超重= 25-29.9
  4. 肥胖= 30 BMI或更大

如果您体重不足或超重或肥胖,请根据您的身高和年龄确定理想体重。

估计理想体重使用迪瓦恩公式(公斤):

男性:IBW = 50 kg + 2.3 kg for each inch over 5 feet.

女性:IBW = 45.5 kg + 2.3 kg for each inch over 5 feet.

#include <stdio.h> 
#include <stdlib.h> 

double calculateBMI(double weight, double height); 

int main(void) 
{ 
    printf("Calculate your BMI\n"); //Calculation of body mass index (BMI) 

    double w, h, ret; 
    double BMI = w/(h*h); 
    ret = calculateBMI(BMI); 

    printf("Enter your weight in kilograms:\n", w); //Input your weight in kilograms here 
    scanf("%lf", &w); 
    printf("Enter your height in metres:\n", h); //Input your height in metres here 
    scanf("%lf", &h); 
    printf("Your BMI is %lf\n", ret) 

    printf("BMI categories:\n"); 
    if (BMI < 18.5) 
    { 
     printf("Your BMI is %lf and you are currently underweight.\n"); 
    } 
    else if (BMI >= 18.5 && BMI <= 24.9) 
    { 
     printf("Your BMI is %lf and you are normal weight.\n"); 
    } 
    else if (BMI >= 25 && BMI <= 29.9); 
    { 
     printf("Your BMI is %lf and you are currently overweight.\n"); 
    } 
    else (BMI >= 30); 
    { 
     printf("Your BMI is %lf and you are obese.\n"); 
    } 


    return 0; 
} 

double calculateBMI(double weight, double height) 
{ 
    double result; 
    result = weight/(height*height); 

    return result; 
} 

帮助我还是不知道该怎么办的职能。请帮帮我。

+2

什么是你遇到的问题?你的问题是什么? –

+0

我有问题,我的功能和回报。你能帮我指出我做错了什么,并加强我的编码吗? –

+0

对于初学者而言,您实际上不会在任何地方*致电*您的功能。要继续使用带有格式化代码的'printf',它需要参数,但不提供这些参数(导致*未定义行为*)。最后,你使用了很多变量而没有被初始化(再次导致未定义的行为)。 –

回答

2

您应该使用方法中使用的参数来计算BMI。 您的变量BMI,身高和体重最初并未在函数中声明。相反,您应该将BMI声明为double,并将高度和重量用作函数参数。

另外,您需要从函数中返回BMI的值。您正在错误地返回calculateBMI,它不是函数内的有效标识符。

工作代码如下: -

double calculateBMI(double weight, double height) 
{ 
    double BMI = weight/(height*height); 
    return BMI; 
} 

而且,你不叫方法calculateBMI()内的主()。

... 
printf("Enter your weight in kilograms:\n"); //Input your weight in kilograms here 
scanf("%lf", &weight); 
printf("Enter your height in metres:\n"); //Input your height in metres here 
scanf("%lf", &height); 
// add below line in your code to call the function. 
BMI = calculateBMI(height,weight); 
printf("BMI categories:\n"); 
... 

我也建议你阅读更多关于C.功能你需要的功能更多一些基本知识(苦练过)。

编辑--->基于OP的评论,最终代码将是:

#include <stdio.h> 
#include <stdlib.h> 

double calculateBMI(double weight, double height); 

int main(void) 
{ 
printf("Calculate your BMI\n"); //Calculation of body mass index (BMI) 

double w, h, BMI; 
printf("Enter your weight in kilograms:\n", w); //Input your weight in kilograms here 
scanf("%lf", &w); 
printf("Enter your height in metres:\n", h); //Input your height in metres here 
scanf("%lf", &h); 
BMI = calculateBMI(w,h); 
printf("Your BMI is %lf\n", BMI) 

printf("BMI categories:\n"); 
if (BMI < 18.5) 
{ 
    printf("Your BMI is %lf and you are currently underweight.\n"); 
} 
else if (BMI >= 18.5 && BMI <= 24.9) 
{ 
    printf("Your BMI is %lf and you are normal weight.\n"); 
} 
else if (BMI >= 25 && BMI <= 29.9); 
{ 
    printf("Your BMI is %lf and you are currently overweight.\n"); 
} 
else (BMI >= 30); 
{ 
    printf("Your BMI is %lf and you are obese.\n"); 
} 


return 0; 
} 

double calculateBMI(double weight, double height) 
{ 
    double result; 
    result = weight/(height*height); 
    return result; 
} 
+0

是的BMI应退还不是功能。谢谢 –

+0

@ A.nonymous - 检查编辑也。 –

+0

@ A.nonymous还有一件事没有函数调用calculateBMI(),请检查相同。 – Ash

0

您的功能:

double calculateBMI(double w, double h) 
{ 
    BMI = weight/(height*height); 
    return calculateBMI; 
} 

在这里你调用的函数,并返回函数名 “calculateBMI”。

返回你想要的正确值。这不是递归函数。

+0

这也不会编译,BMI,体重和身高从未宣布。 –

+0

是的,我错过了 – Ash

+0

你们能帮我吗? –