Lab: Expressions

Learning Objectives

Exercise 1: Practice with mathematical expressions (10 points)

Write a console program that prompts the user for 3 double precision floating point values x, y and z. Have the program compute and display the following values.

To access the built-in functions sin, cos and log2, you need to include the cmath header at the top of your code.

#include <cmath>

Exercise 2: Manual Practice with code analysis (50 points)

Print Manual Practice Problems and then complete the problems first without the aid of a computer. After coming up with your answers, then write programs to determine if your answers are correct. Submit your answers as part of the lab assignment. Either submit hard copy or include the answers in the email you send to the teaching assistant.

NOTE: the character set used in the PDF is probably incompatible with the character set expected by your compiler. For this reason, you should avoid copying and pasting from the PDF into your text editor. To test any of the code, you should type in the code manually.

Exercise 3: Random numbers (10 points)

Write a program that generates a list of 100 random integers.

The C++ standard library contains a pseudo random number generator that you can use to solve this problem. The function rand returns a pseudo random number. To use rand in your programs, you need to include the cstdlib header as follows.

#include <cstdlib>

The function rand will return the same sequence of values for a given seed. You can change the sequence of values it returns by changing the seed as follows.

srand(719);  // Seed the random number generator with 719.

The following is an example of how you can use the return value of rand.

cout << rand();

You can vary the seed passed into srand by calling the time function. Calling the time function with 0 as an argument produces the number of seconds from a given epoch (January 1, 1970 by convention). The following code shows how to seed rand with this time.


Use a for loop to generate the 100 list item entries required by this problem.

You can save the program output by redirecting standard out to a file. To do this, run the program as follows.

./a.out > numbers.txt

Open numbers.txt in a text editor to see the result.

Exercise 4: Unit conversion (10 points)

Write a console program that converts meters into feet and inches. The program starts by asking the user for a measurement in meters. The program should accept a floating point number. After getting the number, the program computes the number of feet and inches equal to the specified number of meters. The program outputs feet and inches as whole numbers. The program should round the number of inches correctly. Test that your program produces the following outputs for the given inputs.

Input Output
1 3 ft 3 in
2 6 ft 7 in
0.3049 1 ft 0 in
0.3048 1 ft 0 in
0.3047 1 ft 0 in

There are 39.3700787 inches in a meter.

Hint: Read meters into a double. Convert to integer inches, adding .5 to round result to nearest whole inches.