Tags: %TAGME{ tpaction="" web="SCMP118/Fall2016" tag="" }% view all tags

Lab 3 - Functions

Due: Feb 20, 11:55pm

Moodle Link


Turn in

  • The code (as a cpp file),
  • A file with the collected outputs from the trial inputs below the run outputs as requested below.
  • Follow the guidelines on Program Style described in section 2.5 of the textbook. ( See guidelines here)


Write a C++ program which does the following (Below is an outline) (100 pts)

    Print Menu 
        1) Compute area of a triangle (according to the formula below)
        2) Compute your clothing size (according to Project 9, in chapter 4 of the textbook)
        3) Convert temperature from Fahrenheit to Celsius (nearest integers) 
        4) Convert temperature from Fahrenheit to Celsius (exact values) 
        5) Test a positive integer for being prime 
        6) Calculate a windchill factor according to the formula from the text, given below.
        7) Quit

    Read Option

    Test Option

        If  1 
            Ask the lengths of the sides of the triangle
            If the lengths are acceptable, find and print the area of the triangle (according to the formula below, also see Project 13, Chp 5) 
        If  2 
            Ask user's height,weight, and age 
            Find and compute hat size, jacket size, waist according to formulas in Project 9, page 243 of the textbook 
        If 3 
            Ask for the temperature in Fahrenheit, as an integer 
            Compute the temperature in celsius to nearest integer and print it out 
        If 4 
            Ask for the temperature in Fahrenheit (exact value) 
            Compute the temperature in celsius (exact value) and print it out 
        If  5 
            Ask for an integer and determine whether it is prime or not.
        If 6
            Ask for a temperature and wind speed to calculate windchill index
        If 7 
            Quit the entire program
            Print "Invalid option"

    End Test

Until user quits

Sample output


  1. Use a switch statement to implement the menu.

  2. Use top down design for your system. Write functions for all subtasks (including printing the menu). For each user option (e.g. triangle size, hat size, temperature conversion) write a simple function that promopts the user for the required fields, then calls a computing function to get the result, then displays the results. For xample write a user interface function "void triangeUI()" to get the inputs, and display the results for the triangle. Then write another function "double triangle(double height, double width)" which is called by tranangleUI, that takes the values entered, does the computation, and then returns the results (in this case the area of the triangle). Write comments and pre-and post conditions with function declarations. Tell what each function is supposed to be doing. Separate declarations from definitions.

  3. Use the following formula to find the area of the triangle with sides a,b and c (project 13, Chp 5) Area= sqrt(s*(s-a)*(s-b)*(s-c)) where s = (a+b+c)/2. So, first ask for the lengths of the sides from the user. Not every 3 numbers can be sides of a triangle. They must satisfy the triangle inequality: The sum of any two sides must be greater than the third. You should check this condition and give an error message if it is not satisfied.

  4. All decimal point number outputs must have 2 digits after the decimal point.

  5. Overload the temperature conversion function to implement options 3 and 4. The function for option 3 takes an integer argument and returns an integer (which is the closest integer temperature). All computation is done with integers (no rounding needed). The other one will compute the exact value as a double. So both input and output types will be doubles. Thus you will need two celsius conversion functions, as seen below in the Overloaded Functions for Celsius section.

  6. You should only accept positive inputs for option 5. Give an error message if the input is negative or 0. 1 is not prime by definition.

  7. Use the following windchill computations from below:
Wind Chill Computations

This computations are done in Celsius and m/sec.

W = 13.12 + 0.6215 * t - 11.37 * v0.16 + 0.3965* t * v0.16


  • v = wind speed in m/sec
  • t = temperature in degrees Celsius: t <= 10
  • W = windchill index (in degrees Celsius)
Optimally you should check if the temperature is not <= 10 and tell the user that the program cannot compute a wind chill above 10 degrees Celsius.

The idea is the break this project up into functional that are cohesive - e.g. each function has a singular purpose, and could (conceivably) be used again somewhere else. I came up with 2 types of functions, the user interface functions (UI) that "talk" to the user, and the support functions that provide (mostly mathematical) functional support for the UI functions.

// User Interface Functions
// Each option of the switch will simply call on of these.
void printMenu();
void triangleUI();
void clothsUI();
void FtoCIntUI();
void FtoCDoubleUI();
void windchillUI();
void CheckPrimeUI();

//Support Functions
double triangleArea(double a, double b, double c);
double hatSize(double w, double h);
double jacketSizeUnder30(double w, double h);
double jacketSizeOver30(double w, double h, int age);
double waistSizeOver28(double w, int age);
double waistSizeUnder28(double w);
double celsius(double f);
int celsius(int f);
bool prime(int number);
double windChill(double temp, double wind_speed);

Overloaded Functions for Celsius

Recall that an overload function is one that uses the same name, but has differnent types. The C++ compiler calls the one that matches. You will need to implement each of the versions of celsius below. Which one gets called depends on the type in the call.

double celsius(double f);
int celsius(int f);

Grading Table

Turn in a run with all these inputs

Requirement Grading Comments Points Score
Code broken up in to functions, each of which performs a single objective.   20  
Good clean, correct, easy to understand Input and output user interface   10  
Complete source code with good formatting, variables names, and comments   10  
Area of a triangle correct solutions   10  
Hat size, jacket size, waist solutions   10  
Fahrenheit to Celsius (Integer) correct solutions   10  
Fahrenheit to Celsius (Exact) correct solutions   10  
Prime Check correct solutions   10  
Wind Chill solutions   10  

Test Input

Area of a triangle 15, 20, 30
  2.3, 7.75, 23.2
  8, 0, 5
Hat size, jacket size, waist Height = 58, Weight = 167.5, Age = 32
  Height = 42, Weight = 101.5, Age = 22
  Height = 77, Weight = 255, Age = 55
Fahrenheit to Celsius (Integer) 32
Fahrenheit to Celsius (Exact) 32
Prime Check 2311
Wind Chill 22 C Degrees, 34 m/sec
  -6 C Degrees, 64 m/sec
  0 C Degrees, 12 m/sec

  • Function.png:
Topic attachments
I Attachment History Action Size Date Who Comment
PNGpng Function.png r1 manage 3.4 K 2017-01-17 - 03:04 JimSkon  
Edit | Attach | Watch | Print version | History: r20 < r19 < r18 < r17 < r16 | Backlinks | Raw View | Raw edit | More topic actions
Topic revision: r20 - 2018-02-20 - JimSkon
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2020 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback