logo

UTK Notes


Lab 06: Boiling Water

Overview

You have made it as a software engineer, living and working as a remote traveling blogger as a side hustle. As you can’t always rely on being near a coffee shop, you travel with an electric kettle that can be set to a specific temperature. As you travel around, sometimes you travel to high elevations and the boiling point of water will change due to pressure, so you must change the boiling point temperature setting on your machine. (If you leave it at the default setting, 100 degrees C, it boils way too fast and scalding hot water can spew out of the kettle’s ventilation holes and burn hands.)

As you don’t always have internet access to look this up, you must make a simple program that looks up the city you are in, and given the elevation, calculates pressure and boiling point.

Input & Command Line Usage

Example input files are already created for you – a list cities you plan on visiting, and their respective states and elevations. You should list the input file on the command line to be received by your program with argv/argc.

The command line usage is

1
./boiling my-input-file.csv

with boiling as the program executable and my-input-file.csv replaced with the name of the input file being used.

Here are two example input files. You can expect any input file to follow this exact format.

User Experience

The user (aka anyone who runs your program) should see a list of cities to choose from when the program runs, in a numbered list. The user chooses a city by typing its number, presses enter, then the program outputs the calculated boiling point for the user. The print statements (prompts from the program) should match the examples EXACTLY. See “Example Output” below.

Calculating Boiling Point at Altitude

In order to calculate the boiling point at altitude, we need to find the pressure at that altitude. This boiling point calculation finds the pressure at altitude assuming that the pressure at sea level is constant and equal to 1013 hPa (1.013 bar). This approximation is sufficient for your coffee making, and you will not take into account the current weather and air pressure.

The formulas for boiling point are:

boiling point = 49.161 * ln(pressure) + 44.932

pressure = 29.921 * (1 - 0.0000068753 * altitude)^ 5.2559

Note that these formulas use specific units:

  • boiling point is expressed in degrees Fahrenheit (°F);
  • pressure is expressed in inches of mercury (inHg); and
  • altitude should be inputted into the equation in the imperial unit — feet.

Let’s say you travelled to Machu Picchu. To calculate the temperature for water to boil for your coffee there, you need to…

  1. The altitude at Machu Picchu is 2430 meters (7970 ft) above sea level.
  2. Input this altitude (in imperial units) to the formula for pressure:

pressure = 29.921 * (1 - 0.0000068753 * 7970)^ 5.2559 = 22.25 inHg

  1. Now, input this pressure into the boiling point equation:

boiling point = 49.161 * ln(22.25) + 44.932 = 197.44°F

  1. Congratulations! Now you can make your coffee at Machu Picchu.

Using the Math Library

To calculate the natural log and powers, use the math library by

1
#include <cmath>

The pow() function is used with two parameters, the base and the exponent: pow(base, exponent)

pow(3, 4) is 3 ^ 4 (3 raised to the 4th power).

The log() function returns the natural logarithm (base-e logarithm) of the argument passed in the parameter.

log(5) returns the natural logarithm of 5.

To view all cmath library functions, go to the c++ reference page HERE

Example Output

Using the City-State-Altitude-1.csv as input:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Boiling Point at Altitude Calculator
1. Leadville
2. Breckenridge
3. Aspen
4. Santa Fe
5. Park City
6. Flagstaff
7. Big Bear Lake
8. Cheyenne
9. Golden
10. Beech Mountain
11. Denver
12. Fort Davis
13. Bozeman
14. Salt Lake City
15. Moab
16. Banner Elk
17. Bend
18. Billings
Enter city number: 1
The boiling point at Leadville is 193.315 degrees Farenheit.

Requirements

  • You must use argv/argc to learn the input filename. See “Input” for usage. You must check for the correct number of command line arguments (2). If the user types an incorrect number of arguments when running the program, print

"usage: ./boiling filename"

and exit the program.

  • You must use file streams to read from the file, and you must error check to make sure the file opened. If it failed to open, print “File failed to open.” and exit the program immediately.
  • You must close the file stream when you are done using it.
  • As you read in the lines from the input file, you must save the city name in a vector of strings called cityNames and you must save the elevations in a vector of ints called elevations. Each index should correspond to a respective city’s name and elevation. For example, Leadville’s name would be at index 0 in cityNames, and it’s elevation at index 0 in the elevations vector.
  • You may only use concepts we learned in class or in lecture notes/powerpoints to write this program (file streams, string streams, getline, etc).
  • Your output must match the example output and round the boiling point to 3 decimal places.
  • Submit your program as boiling.cpp

Hints

  • I recommend only calculating the boiling point for the city requested. No need to calculate for any other city.
  • Using file streams to gather each line, then string streams to break up the line with getline() is an easy way to do this, similar to the reading CSVs program we did in class.
  • Remember there are two ways to use getline() - with two arguments (a stream and a string) or three (stream, string, and a character to stop at). Using the optional parameter of where to stop will be helpful (think - stopping when we see a comma).
  • Use temporary print statements for sanity checks while you code up your file reading loop and to make sure your vector is stored correctly.
  • This program is nearly identical to a combination of programs we wrote in class, just a different task. Review your argc/argv programs and the stringstreams program.
  • While it seems like this is a long writeup, it should be a pretty easy lab if you paid attention in class. Here is a *high-level* breakdown of what you should do:
    • Use argv/argc to get a filename/error check
    • Read the file/error check and store the city names/elevations in vectors
    • Ask for which city
    • Calculate the boiling point at that city’s elevation using the given formulas