COMP 1671 : Introduction to Computer Science

Lab 6 : Fun with functions


In this lab, you will be using user defined functions to modify and / or complete the following programs so they do what we require of them. Much of the programs are written - you just need to add  / modify code to use user defined functions.
Print out and turn in the source code of each of your completed programs.

As always, feel free to define any additional variables you may need.

Program 1.

Modify the factorial program you wrote in the previous lab as follows. The program should define two functions: getUserInput()  and calcFactorial().  Your program should like something like this.

#include <iostream>
using namespace std;

int getUserInput();  // gets a number from the user
unsigned long calcFactorial(int); // calculates the factorial of a number


int main()
{
    int n(0);         // store number entered by the user
    unsigned long factorial(1);  // store value of factorial

    // get a number from the user
    n = getUserInput();

    // calculate the factorial of the number
    factorial = calcFactorial(n);
    

    // output the factorial
    cout << "The factorial of " << n << " is " << factorial << endl;

    return 0;
}


/* Asks user a number to calculate factorial.
   Returns the number entered by the user. */
int getUserInput()
{
    // add code to prompt the user and get a number
}


/* Calculates the factorial of a number.
    Param n - the number to calculate the factorial of.
    Returns n! */
unsigned long calcFactorial(int n)
{
    // add code to calculate the factorial of n
}

Program 2.

Fill in code in the program below, so that it gets user's height in inches and weight in pounds, checks if the input is valid, then calculates their body mass index (BMI) and prints out the BMI value and an appropriate message.
Note some of the functions are already provided. However you do need to write prototypes for all functions.

#include <iostream>
#include <string>
using namespace std;

// add function prototypes here


int main()
{
    int heightInInches(0);
    float weightInPounds(0);
    float bodyMassIndex(0);	// body mass index


    // Get user's height in Inches

    // Get user's weight in Pounds

    // Check if input is valid (height and weight are > 0)
    // if not, then print message and stop program
    if (!isValidInput(heightInInches, weightInPounds))  {
	cout << "Invalid input." << endl;
	return 0;
    }

    // calculate user's BMI (body mass Index)

    // Output BMI
    cout << "Your body mass index (BMI) is " << bodyMassIndex << endl;

    // print appropriate message

    return 0;
}


/* Prompts user to enter height in inches.
   Returns height entered by user */
int getUserHeight()
{
    // add code here
}


/* Prompts user to enter weight in pounds.
   Returns weight entered by user */
float getUserWeight()
{
    // add code here
}


/* Checks for validity of height and weight.
   Param height - person's height
   Param weight - person's weight
   Returns true if both are greater than 0. Otherwise returns false */
bool isValidInput(int height, float weight)
{
    // add code here
}


/* Calculates the body mass index of a person.
   Param heightInInches - person's height in inches
   Param weightInPounds - person's weight in pounds
   Returns the body mass index for the height and weight */
float calculateBMI(int heightInInches, float weightInPounds)
{
    return (weightInPounds * 703) / (heightInInches * heightInInches);
}


/* Chooses appropriate message based on the body mass index value.
   Param bodyMassIndex - person's body mass index (BMI)
   Returns a message based on the BMI value */
string chooseMessage(float bodyMassIndex)
{
    string message;

    if (bodyMassIndex < 18.5)
	message = "You are underweight.";
    else if (bodyMassIndex < 25)
	message = "You are normal.";
    else if (bodyMassIndex < 30)
	message = "You are overweight.";
    else if (bodyMassIndex < 40)
	message = "You are obese.";
    else
	message = "You are extremely obese. Go see a doctor TODAY!";

    return message;
}