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 3

b. (x + y) * (x - y)

c. 1.0 / (x + y) _ 1.0 could be 1

d. 1.0 / x + y _ 1.0 could be 1

e. float(i) / float(j)

or

float(i) / j

or

i / float(j)

f. i / j

g. ((x + y) / 3.0 - (x - y) / 5.0) / (4.0 * x) _ The literals could be 3, 5, 4

3. 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 / y

f. sqrt(pow(x, 6.0) + pow(y, 5.0))

g. pow(x + sqrt(y), 7.0)

5. // Calculate perimeter

perimeter = 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

    1. False 4. False 7. True 10. True 13. True 16. False
    2. True 5. False 8. True 11. False 14. False 17. true
    3. True 6. False 9. False 12. True 15. False

Multiple Choice

    1. a 21. b 24. d 27. c 30. e 33. d
    2. b 22. b 25. d 28. c 31. e 34. e
    3. c 23. a 26. c 29. c 32. c 35. d

Fill-In

36. precedence

37. (score1 + score2 + score3)/3.0

or

(score1 + score2 + score3) / 3

38. currentSales * 0.03 + BASE_PAY

39. 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###89

52. ##4589