G'day everyone,

I need to fill a 2day which is passed as an argument that is able to store the height and weight of 5 volunteers. The function I need to do this in needs to prompt the volunteers for their height and weight.

Could anyone please give me a head start? I have no idea where to begin!

Thanks heaps!

Brittany

Try making something yourself first, and ask questions about the code. We don't give away homework here.

I can give you a few tips:
(2d) arrays
loops
user input

Good luck!

I've done the user input and declared the array, just not sure how to go about row and column sizes and the input of the weight and height....

I've done the user input and declared the array, just not sure how to go about row and column sizes and the input of the weight and height....

Please post what you have so far.

Please post what you have so far.

Sure

#include <iostream>
#include <math.h>
#include <iomanip>

using namespace std;

//Declare constants
const int NUM_COLS = 5;
const int NUM_ROWS = 3;

double volunteers[NUM_COLS][NUM_ROWS];
double bodyMassIndex, weight, height;

//Declare functions
void fillArray(double, double, double);
double calcDisplayResults(double bodyMassIndex);

int main()
    {
    system("PAUSE");
    return 0;
    }

void fillArray(double weight, double height, double bodyMassIndex)
    {
    //Prompt user to enter weight and height
    cout << "Please enter your weight in kg: ";
    cin >> weight;

    cout << "Please enter your height in metres: ";
    cin >> height;

    //Throws an error
    //cout << "Your Body Mass Index is: " << calcDisplayResults(double bodyMassIndex);
    
    }

double calcDisplayResults(double)
    {

    //Calculating the Body Mass Index
    bodyMassIndex = (weight /((height / 100)*(height / 100)));

    return bodyMassIndex;

    }

Sure

#include <iostream>
#include <math.h>
#include <iomanip>

using namespace std;

//Declare constants
const int NUM_COLS = 5;
const int NUM_ROWS = 3;

double volunteers[NUM_COLS][NUM_ROWS];
double bodyMassIndex, weight, height;

//Declare functions
void fillArray(double, double, double);
double calcDisplayResults(double bodyMassIndex);

int main()
    {
    system("PAUSE");
    return 0;
    }

void fillArray(double weight, double height, double bodyMassIndex)
    {
    //Prompt user to enter weight and height
    cout << "Please enter your weight in kg: ";
    cin >> weight;

    cout << "Please enter your height in metres: ";
    cin >> height;

    //Throws an error
    //cout << "Your Body Mass Index is: " << calcDisplayResults(double bodyMassIndex);
    
    }

double calcDisplayResults(double)
    {

    //Calculating the Body Mass Index
    bodyMassIndex = (weight /((height / 100)*(height / 100)));

    return bodyMassIndex;

    }

Normally you declare arrays like this:

double volunteers[NUM_ROWS][NUM_COLS];

rather than this:

double volunteers[NUM_COLS][NUM_ROWS];

The compiler doesn't care, but it's more readable for humans and it's a more accurate description. However, as long as you are consistent, things will work out. There are five people and you have an array volunteers[5][3]; , so the first index will be the person. For the second index, which will range from 0 to 2, you want to pick one index for height, one for weight, one for BMI. Might as well pick 0 for height, 1 for weight, and 2 for BMI. So you have person 0 through person 4 and you need to fill in height and weight: volunteer[0][0] is the height of volunteer 0. volunteer[0][1] is the weight of volunteer 0. To read in a value to the array, just do this:

cin >> volunteer[i][0];  // reads in volunteer i's height
cin >> volunteer[i][1]; // read in volunteer i's weight

You'll calculate volunteer i's BMI from his/her height and weight and put the calculated value into volunteer[i][2] .

So you need to set up a loop in your function to where i goes from 0 to 4, then read the heights and weights into the array. From there, calculate the BMI's for each person and put it into volunteer[i][2] , where i is some number from 0 to 4.

Comments
For making the audience happy! =P

You're a champ! Thanks a lot for your help; I really appreciate it. I'll most likely have more problems arise, so beware!

This article has been dead for over six months. Start a new discussion instead.