Making the BMICalc exercise a little more robust


After entering the code as written, I decided to beef up the code a little bit to include an array of three people, extrapolating from what I learned about arrays here…

I wasn’t sure whether or how arrays would work within with my new typedef, but the two concepts seemed to fit together pretty much as expected.

I also added the individual’s bmi into the typedef, as well as giving each person a name. That was new too, figuring out how to store text into a char variable, since in the Class Certificates exercise, we sent the quoted text directly to the function, instead of storing it in a variable first.

[code]#include <stdio.h>

// Here is the declaration of the type Person
typedef struct {
char *nameOfPerson;
float heightInMeters;
int weightInKilos;
float bmi;
} Person;

// Here is the BMI calculation function
float bodyMassIndex(Person p)
float tempBMI = p.weightInKilos / (p.heightInMeters * p.heightInMeters);
return tempBMI;

// Here is the main function
int main(int argc, const char * argv[])
Person people[3];
people[0].nameOfPerson = “Billy Jo”;
people[0].weightInKilos = 96;
people[0].heightInMeters = 1.8;

people[1].nameOfPerson = "Jim";
people[1].weightInKilos = 106;
people[1].heightInMeters = 1.6;

people[2].nameOfPerson = "Bob";
people[2].weightInKilos = 73;
people[2].heightInMeters = 1.7;

int counter;
for (counter = 0; counter <= 2; counter ++) {
    people[counter].bmi = bodyMassIndex(people[counter]);
    printf("%s weighs %i kilograms.\n", people[counter].nameOfPerson, people[counter].weightInKilos);
    printf("%s is %.2f meters tall.\n", people[counter].nameOfPerson, people[counter].heightInMeters);
    printf("%s has a BMI of %.2f.\n\n", people[counter].nameOfPerson, people[counter].bmi);
    return 0;


This produces the following output:

[color=#0000FF]Billy Jo weighs 96 kilograms.
Billy Jo is 1.80 meters tall.
Billy Jo has a BMI of 29.63.

Jim weighs 106 kilograms.
Jim is 1.60 meters tall.
Jim has a BMI of 41.41.

Bob weighs 73 kilograms.
Bob is 1.70 meters tall.
Bob has a BMI of 25.26.[/color]

All in all I’m pleased with the results. Your comments/criticisms are welcome.


Thanks for the advance glimpse into arrays. That was nice.


You’re welcome.