C++ function with multiple defaulted parameters used as a polynomial: compute two outputs for x = 2.3.
#include
double CuriousTabFunction(double, double, double = 0, double = 0, double = 0);
int main()
{
double d = 2.3;
cout << CuriousTabFunction(d, 7) << " ";
cout << CuriousTabFunction(d, 7, 6) << endl;
return 0;
}
double CuriousTabFunction(double x, double p, double q, double r, double s)
{
return p + (q + (r + s * x) * x) * x; // Horner-like form
}
-
A7 20
-
B7 19.8
-
C7 Garbage
-
D7 20.8
-
ENone of the above
Answer
Correct Answer: 7 20.8
Explanation
Introduction / Context: The function evaluates a cubic-like expression using Horner-style nesting. Default arguments make most coefficients zero unless explicitly provided. You must track which parameters receive values and which default to zero.
Given Data / Assumptions:
- x = 2.3.
- First call: p = 7, q = r = s = 0.
- Second call: p = 7, q = 6, r = s = 0.
Concept / Approach: For the first call, every nested term beyond p cancels to zero, leaving p. For the second call, only q contributes beyond p, scaled by x once. Evaluating carefully yields numeric results.
Step-by-Step Solution:
Call 1: p + (q + (r + sx)x)x with q=r=s=0 → 7 + (0 + (0 + 0)x)x = 7.Call 2: p=7, q=6, r=s=0 → 7 + (6 + (0 + 0)x)x = 7 + 6x = 7 + 62.3 = 7 + 13.8 = 20.8.Verification / Alternative check: Writing the general form as p + qx + rx^2 + sx^3 confirms that only qx contributes in the second call.
Why Other Options Are Wrong:
- 7 20: Misses the 0.8 contribution of 60.133..; numerically incorrect.
- 7 19.8: Off by 1.0; likely mistaken sign.
- 7 Garbage / None of the above: All arguments are defined and the math is straightforward.
Common Pitfalls: Misplacing parentheses in nested expressions, or forgetting that only q survives when r and s are zero.
Final Answer: 7 20.8