-2

Calculate the body mass index. Body mass index compares your weight to your height, and is calculated by dividing your weight in kilograms by your height in meters squared. It gives you an idea of whether you're underweight, a healthy weight, overweight, or obese for your height.

Body Mass Index Categories:

  1. Underweight = <18.5
  2. Normal weight = 18.5–24.9
  3. Overweight = 25–29.9
  4. Obesity = BMI of 30 or greater

If you are underweight or overweight or obese, determine the ideal weight based on your height and age.

Estimate Ideal body weight in (kg) using Devine formula:

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

Females: 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;
}

Help I still don't know how to do the functions. Please help me.

A.nonymous
  • 65
  • 2
  • 2
  • 10
  • 2
    And what is the problem you're having? What is your question? – Some programmer dude Mar 28 '16 at 05:58
  • I am having problem with my function and return. Can you help me point out where I did wrong and enhance my coding? – A.nonymous Mar 28 '16 at 06:00
  • For starters, you don't actually *call* your function anywhere. To continue you use `printf` with formatting code that expects arguments but you don't provide those arguments (leading to *undefined behavior*). And lastly you use a lot of variables without being initialized (again leading to undefined behavior). – Some programmer dude Mar 28 '16 at 06:03
  • Oh, and the code should not even compile as you use variables that are not defined in your `calculateBMI` function, and you even declare the function prototype in a way that will give you compiler errors. – Some programmer dude Mar 28 '16 at 06:05
  • 1
    If you're a beginner in C I recommend you take a couple of steps back, and start over with the simplest "hello world" program and take baby steps from there. Also you should [find a good beginners book](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) to help you. – Some programmer dude Mar 28 '16 at 06:06
  • Can you give me the correct code for the wrong part only so that I can identify my problem. Thanks. – A.nonymous Mar 28 '16 at 06:12
  • Can someone help me please? I need to pass it up in a few more days. – A.nonymous Mar 28 '16 at 06:41
  • Try to start at the basics, I am sure you got an introduction in the course where you have to hand it in now – arc_lupus Mar 28 '16 at 06:47

2 Answers2

2

You should use the parameters used in the method to calculate the BMI. Your variables BMI, height, and weight are not declared originally in the function. Instead you should declare BMI as double, and use height and weight as the function parameters.

Also, you need to return the value of BMI from the function. You are incorrectly returning the calculateBMI, which is not a valid identifier inside the function.

The working code would be :-

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

Also, you've not called the method calculateBMI() inside your main().

...
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");
...

I'd also suggest you to read more about functions in C. You need some more basic knowledge of functions(practice hard too).

EDIT ---> Based on OP's comment, the final code would be :

#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;
}
Am_I_Helpful
  • 18,735
  • 7
  • 49
  • 73
  • Yeah the BMI should be returned not the function. Thank you – A.nonymous Mar 28 '16 at 06:49
  • @A.nonymous - Check the edit also. – Am_I_Helpful Mar 28 '16 at 06:51
  • @A.nonymous One more thing there is no function calling for calculateBMI(), Please check the same. – Ash Mar 28 '16 at 06:52
  • Help I just edited the codes. But I still don't know whether it is correct or wrong. Help. – A.nonymous Apr 04 '16 at 03:25
  • Your `ret` and `calculateBMI()` both are doing the same calculations! Please don't use ret for calculating the BMI, that is the purpose of calculateBMI() function. @A.nonymous – Am_I_Helpful Apr 04 '16 at 05:24
  • @A.nonymous - Also, please ask a separate question if you've a new question. Please don't edit the solved question. If the problem was solved earlier, please accept the answer. – Am_I_Helpful Apr 04 '16 at 05:27
  • No, because I have to pass up my assignment today and it won't allow me to post another question until the next 7 days so I have to edit it. And I still can't do it... – A.nonymous Apr 04 '16 at 05:59
-1

Your function :

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

Here you are calling the function and returning the function name "calculateBMI".

Return the proper value which you want.This is not a recursive function.

Ash
  • 176
  • 1
  • 11