Study Guide 2 Answers

Formulas

Please review: Converting Formulas to C++ Statements

Some general observations:

Library Functions

Documentation Example Program
double sqrt(double x); sqrt(a*a + b*b); hypot.cpp
double pow(double x, double y); p * r / (1 - pow(1 + r, -n)); payment.cpp

The documentation for the library functions always shows the data type for each argument and for the return value. However, looking at the examples given in the videos and text, we can see the data type, double in the examples above, is not included when we use or call the function in a program. (In this way, C++, Java, and Python are the same.)

Answers

  1. d
  2. a
  3. c; Whenever both operands of the division operator are type int, the operation takes place using integer arithmetic, so the result is of type int, and the value is truncated (not rounded).
  4. c
  5. a
  6. c; the division operation occurs first because it's inside the parentheses.
  7. b; Whenever at least one operand of the division operator is type float or type double, then the operation takes place using floating-point arithmetic, and the result is of type float or double (depending on the type of the operands). Integers assigned to double variables are automatically cast or promoted to double values.
  8. d; 3 / 4 = 0 with a remainder of 3.
  9. b; 5 / 4 = 1 with a remainder of 1.
  10. a; 5 / 5 = 1 with a remainder of 0.
  11. f; 5 / 6 = 0 with a remainder of 5.
  12. d
  13. c
  14. a
  15. pow(x,y) The question tests your understanding of the term "expression" and knowledge of the pow function. Be aware that C++ does not have an exponentiation operator; specifically, x^y DOES NOT raise x to the y power.
  16. b
  17. a
  18. c
  19. d
  20. #include
  21. b
  22. a
  23. c
  24. d
  25. b
  26. b or d
  27. a, b, or c
  28. Swapping Problem (1) used T to represent a generic data type. You only need to substitute Foo for T to answer the question. This pattern will work for all data types - including classes. Please note that the data type of temp must be the same type as the data specified in the problem, but the name of the temporary variable is not significant.
    Foo temp = x;
    x = y;
    y = temp;

    Once the value stored in x is copied to temp, then x can be changed without losing any data. Copy y to x; now y can be changed without losing data. Copy temp to y and temp is no longer needed.

  29. There is often not an exactly-right number of parentheses to use, but one of the purposes of this question is to demonstrate how hard it is to find errors when an excessive number of parentheses are used.
    1. Missing a closing parenthesis, which is hard to spot because of the many unnecessary parentheses used.
    2. This statement does work, but it’s not the best due to the excessive parentheses and the unnecessary "-1," which makes the code harder to read and understand.
    3. One too many opening parentheses.
    4. Just the right number of parentheses and all operators are correct.
    5. The denominator must be in parentheses due to the subtraction operation.
    6. Cannot use square brackets for grouping.
    7. Missing multiplication operation.
    8. Missing multiplication operation; cannot use square brackets for grouping.
  30. \[ i_2 = \left[ \left(1 + {i_1 \over n_1} \right)^{n_1 \over n_2} - 1 \right] n_2 \] Converting the formula to C++ code may seem daunting, but taking it one step at a time makes it doable. And, sometimes it's easier to start in the middle and work your way out:
    1. The exponentiation problem has two parts: the base and the exponent:
      1. The base is: 1 + i1 / n2
      2. The exponent is: n1 / n2
      3. Which is written as: pow(1 + i1 / n2, n1 / n2)
    2. Next, do the subtraction: pow(1 + i1 / n2, n1 / n2) - 1
    3. The final multiplication requires another set of parentheses and an explicit multiplication operator:
      (pow(1 + i1 / n2, n1 / n2) - 1) * n2
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    int main()
    {
    	double n1;
    	double n2;
    	double i1;
    
    	cout << "Enter n1: ";
    	cin >> n1;
    
    	cout << "Enter n2: ";
    	cin >> n2;
    
    	cout << "Enter i1: ";
    	cin >> i1;
    
    	double i2 = (pow(1 + i1 / n2, n1 / n2) - 1) * n2;
    
    	cout << "i2 = " << i2 << endl;
    
    	return 0;
    }
  31. The question is more about providing an elaboration than writing a comment. Furthermore, which elaboration is "best" depends on purpose and context, which is why more than one answer was considered correct.

    Unfortunately, there is no "Goldielocks" or "just right" amount of detail to use in an elaboration. The amount of detail you include may vary depending on the situation: if you are writing the elaboration, you may want to be economical with your comments, but if you are verbalizing the elaboration to yourself or others, you can afford to be a bit more detailed. Remember, the point of elaboration is to help you understand what is taking place in the program well enough that you can use the same concepts to solve a new problem. Please see Elaboration for more information.

    1. The statement is incorrect.
    2. The statement is correct but doesn't explain what is taking place, meaning it is difficult to apply this example to a new problem.
    3. This statement is correct and, in many cases, is a sufficient elaboration.
    4. This statement is also correct and contains much more detail. This level of detail is appropriate the first time you see this problem - especially if you are verbalizing the elaboration rather than writing it.