# Thread: Calculate BMI and BMR

1. ## Calculate BMI and BMR

I'm trying to write a program for class which calls 3 functions in main which is get input, do calculations, and print output. I also need to be using pointers which I have no understanding of how to use.

so far my attempt is just giving me headaches. Can anyone steer me in the right direction?

Code:
```#include <stdio.h>
#include <math.h>

#define INCHES_FT 12
#define CENT_INCH 2.54
#define CENT_METER 100
#define LB_KG 2.2

void getInput();
void doCalculations(int feet, double inches, double pounds, int age);
double printOutput(double bmi, double bmr);
double convertToInch(int feet, double inches);
double convertToCent(double inInches);
double convertToMeter(double inCent);
double convertToKG(double pounds);
double calculateBMI(double inKG, double inMeter);
double calculateBMR(double inKG, double inCent, int age);

int main(void)
{
getInput();
doCalculations(&feet, &inches, &pounds, &age,);
printOutput(double bmi, double bmr);

return 0;
}

void getInput()
{

int feet;
double inches;
double pounds;
int age;

printf("Enter height in feet & inches: ");
scanf("%d %lf", &feet, &inches);
printf("Enter weight in pounds: ");
scanf("%lf", &pounds);
printf("Enter age in years (whole # only): ");
scanf("%d\n\n", &age);
}

void doCalculations(int *feet, double *inches, double *pounds, int *age)

{
double inInches;
double inCent;
double inMeter;
double inKG;
double bmi;
double bmr;

inInches = convertToInch(*feet, *inches);
inCent = convertToCent(inInches);
inMeter = convertToMeter(inCent);
inKG = convertToKG(*pounds);
bmi = calculateBMI(inKG, inMeter);
bmr = calculateBMR(inKG, inCent, *age);
}

double convertToInch(int feet, double inches)
{
return (feet*INCHES_FT + inches);
}

double convertToCent(double inInches)
{
return (inInches*CENT_INCH);
}

double convertToMeter(double inCent)
{
return (inCent/CENT_METER);
}

double convertToKG(double pounds)
{
return (pounds/LB_KG);
}

double calculateBMI(double inKG, double inMeter)
{
return inKG/pow(inMeter, 2);
}

double calculateBMR(double inKG, double inCent, int age)
{
return 66 + (13.75 * inKG) + (5 * inCent) – (6.8 * age);
}

double printOutput(double bmi, double bmr)
{
printf("The BMI = %.3lg", bmi);
printf("The BMR (Male) = %.3lg", bmr);

return 0;
}```

2. Welcome to the forum, Nguystep!

Now, I hate to tell you (well not really), that a function with more than 3 returns in it, is a serious sin against my religion. So your headache is a just punishment, of the heavens.

When you're looking for an algorithm to follow for your code, use how humans would do it, as your starting point. Humans are lazy, and we tend to work with what's at hand, so your program will tend to have good locality of data.

C is best kept simple and concise, and that's right from the creators mouth. Being too cute and "Rube Goldberg" with you program, is just an invitation for more headaches - both now and in the future when you want to modify/extend/or teach someone else using your code as an example.

KISS ==> try to use it as a goal for your code, after accuracy (which is always the paramount thing and #1).

Sit down and figure out an example or two by hand, and then make your code to mimic that same algorithm.

And please, no dozen returns in one function.

Remember, the heavens are watching!!

3. ## Im too complicated

I have no idea how to make it any more simple.

This is one of my first programs using functions and in my assignment instructions it seems like my teacher wants all the calculations in the one function. There is just so much I do not understand at all while doing this.

I actually made a simpler program which converts feet+inches to inches, inches to cent, cent to meters and in the assignment she wanted us to copy and paste those 3 functions over into this assignment.

Code:
```#include <stdio.h>#include <math.h>

#define INCHES_FT 12
#define CENT_INCH 2.54
#define CENT_METER 100
#define LB_KG 2.2

void getInput();
double doCalculations(int feet, double inches, double pounds, int age);
double printOutput(double bmi, double bmr);
double convertToInch(int feet, double inches);
double convertToCent(double inInches);
double convertToMeter(double inCent);
double convertToKG(double pounds);
double calculateBMI(double inKG, double inMeter);
double calculateBMR(double inKG, double inCent, int age);

int main()
{
int feet;
double inches;
double pounds;
int age;
double bmi;
double bmr;

getInput();
doCalculations(feet, inches, pounds, age);
printOutput(bmi, bmr);

return 0;
}

void getInput()
{
int feet;
double inches;
double pounds;
int age;

printf("Enter height in feet & inches: ");
scanf("%d %lf", &feet, &inches);
printf("Enter weight in pounds: ");
scanf("%lf", &pounds);
printf("Enter age in years (whole # only): ");
scanf("%d", &age);
}

double doCalculations(int feet, double inches, double pounds, int age)
{

double inInches;
double inCent;
double inMeter;
double inKG;
double bmi;
double bmr;

inInches = convertToInch(feet, inches);
inCent = convertToCent(inInches);
inMeter = convertToMeter(inCent);
inKG = convertToKG(pounds);
bmi = calculateBMI(inKG, inMeter);
bmr = calculateBMR(inKG, inCent, age);
}

double convertToInch(int feet, double inches)
{
return (feet*INCHES_FT + inches);
}

double convertToCent(double inInches)
{
return (inInches*CENT_INCH);
}

double convertToMeter(double inCent)
{
return (inCent/CENT_METER);
}

double convertToKG(double pounds)
{
return (pounds/LB_KG);
}

double calculateBMI(double inKG, double inMeter)
{
return inKG/pow(inMeter, 2);
}

double calculateBMR(double inKG, double inCent, int age)
{
return 66 + (13.75 * inKG) + (5 * inCent) - (6.8 * age);
}

double printOutput(double bmi, double bmr)
{
printf("\n");
printf("The BMI = %lg\n", bmi);
printf("The BMR (Male) = %lg", bmr);

return 0;
}```

4. My hint to you would be if you want to return more than one value or pass around lots of values together, look at structures, or "struct"s. One struct can contain all of int feet;
double inches;
double pounds;
int age;
double bmi;
double bmr;

together. You would initialize some of the values and then pass by pointer to the calculation function to fill in the rest (bmi and bmr).

5. I am starting to appreciate the logic here. If your teacher wants all the calculations done in one function, then why are you using 6 functions for the calculations? You could easily make all those calculations, in just the one function. A struct like Alanevans suggested, would be a great improvement.

But you may not have studied structs just yet. So looking at the program as it is right now, what's it doing wrong - and what is it doing right? Just a general "it doesn't work", doesn't show any effort at working out the errors, on your part. Can you be more specific? Much more specific, hopefully?