Homework 4
1. Complete the following table. The first three rows are already
filled in.
|
Expression |
Data Type of the result of the expression |
Value of the expression |
|
1 / 2 |
int |
0 (integer
division, / returns the number of 2s
that can be taken out of 1) |
|
1 % 2 |
int |
1
(% returns the remainder after
taking as many 2s out of 1 as you can) |
|
1.0 / 2 |
double |
0.5 (“normal”
floating point division due to 1.0) |
|
5 + 7 / 2 |
|
|
|
'B' |
|
|
|
6 + 13 / 5 - 35 % 3 |
|
|
|
3.5 * (5/4) |
|
|
|
(3.5 * 5)/4 |
|
|
2. Write a flowchart (or pseudocode, see hint below) that reads in two lengths in the format x' y" (i.e. x feet y inches) and returns the difference in length between the two in the same format. You may assume that the first is always larger than the second!
See hint 1 and hint 2 below. For example, if the user enters 32' 6" and 15' 11" the result should be 16' 7"
Hint: pseudocode is a rough plan for your program that shows the key steps, but isn’t a formal program with all the details of declarations, semicolons, etc. You can mix English instructions with lines that look closer to code (for formulas, etc.). For instance, here is some example pseudocode for reading two numbers that represents a point (x,y) and computing the length:
- Read first number, store in x
- Read second number, store in y
- Compute length = square root(x*x + y*y)
- Output the length
3. Write a C++ program for the problem in question 2. See hint 3 and hint 4 below. A typical run of your program should look like this (user input shown in red):
Enter two lengths in feet and inches (larger first!) 32' 6" 15' 11"
Difference is 16' 7"
"'" works just fine, but to get the
character constant of "
you must write "\"",
which uses the backslash as an escape character to tell the complier
that you really want a " and not the end of string marker
. For example, consider
cout <<
"Difference is " << 19 << "' "<< 83
<< "\"" << endl; which prints out 19' 83"Turn In (all stapled together with your name and alpha code on it):
1. A copy of this homework assignment with Table 1 filled in.
2. Your flowchart or pseudocode from question 2.
3. A printout of your source code from question 3.
4. A screen capture of your program from question 3 running on the example input from question 3.