Chapter 3
ARITHMETIC EXPRESSIONS,
FUNCTION CALLS, AND OUTPUT
Programming Warm-Up Exercises
2. Below, we use the minimum number of parentheses. Extra parentheses would be fine (as long as they are in the correct positions).
a.
x / y - 3.0 _ 3.0 could be 3b.
(x + y) * (x - y)c.
1.0 / (x + y) _ 1.0 could be 1d.
1.0 / x + y _ 1.0 could be 1e.
float(i) / float(j)or
float(i) / j
or
i / float(j)
f.
i / jg.
((x + y) / 3.0 - (x - y) / 5.0) / (4.0 * x) _ The literals could be 3, 5, 43. a.
abs(i)b.
labs(n)c.
fabs(x + y)d.
fabs(x) + fabs(y)e.
pow(x, 3.0) / y or x * x * x / yf.
sqrt(pow(x, 6.0) + pow(y, 5.0))g.
pow(x + sqrt(y), 7.0)5.
// Calculate perimeterperimeter = 2.0 * (length + width);
// Calculate area
area = length * width;
// Output results
cout << "Rectangle length = " << length
<< " and width = " << width << endl;
cout << "Rectangle perimeter is " << perimeter << endl;
cout << "Rectangle area is " << area << endl;
return 0;
}
6. The answer to part (a) is written three different ways (and there are other possibilities as well). The answer to each remaining part is written as a single output statement but could be broken up into several output statements.
a.
cout << "Four score" << endl;cout << "and seven years ago" << endl;
or
cout << "Four score" << endl
<< "and seven years ago" << endl;
or
cout << "Four score" << endl << "and seven years ago" << endl;
b.
cout << "Four score" << endl<< "and seven" << endl
<< "years ago" << endl;
c.
cout << "Four score" << endl<< "and" << endl << endl
<< "seven" << endl
<< "years ago" << endl;
d.
cout << "Four" << endl<< " score" << endl
<< " and" << endl
<< "seven" << endl
<< " years" << endl
<< " ago" << endl;
7. b. The roundoff factor should be a constant, because it should not change during the execution of the program. If the program were changed to let the user enter a different roundoff factor each time, then it would have to be a variable.
ANSWERS TO QUESTIONS
True/False
Multiple Choice
Fill-In
36. precedence
37.
(score1 + score2 + score3)/3.0or
(score1 + score2 + score3) / 3
38.
currentSales * 0.03 + BASE_PAY39. cast (type cast)
40. grouping order (associativity)
41. type coercion
42. mixed type (mixed mode)
43. type casting (type
conversion)
44. library function (library routine)
45. parameter (argument)
46. function call (function invocation)
47. parameter list (argument list)
48. value-returning function
49. standard library
50. void function (procedure)
51.
##45###8952.
##4589