- Understand the notion of a data type.
- Distinguish between the data types int and double.
- Understand the difference between primitive and complex data types.
- Understand how to use the string data type.
- Understand how to change the state of a variable using the assignment operator.
- Understand how to use mathematical operations and parentheses to form expressions.
- C++ Operator Precedence
- Learn how name values using the keyword
- Learn how to use the complex data type called
- Learn how to invoke (or call) functions.
- Learn about the boolean data type.
- Learn how to use conditional expressions (forms of the
- Learn how to compare integers, floating point numbers and strings.
- Learn how to write loops and use them to solve problems.
- Learn the difference between floating point and integer division.
- Learn to round floating point values to the nearest integer by adding
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.
- the sum of x, y and z
- the sine of x
- the cosine of x plus y divided by z
cos((x + y) / z)
- the average of x, y and z
- the log2 of x
To access the built-in functions
you need to include the cmath header at the top of your code.
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.
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
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.
|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.