Difficulty: Easy
Correct Answer: rem = fmod(3.14, 2.1);
Explanation:
Introduction / Context:Unlike integer division, floating-point division in C/C++ uses library functions to compute the remainder. The operator '%' is defined only for integers, so you must select the proper function when working with doubles or floats. This question checks whether you know the standard library tool for floating-point remainders.
Given Data / Assumptions:
Concept / Approach:
Step-by-Step Solution:
Select the correct API: 'fmod(3.14, 2.1)'.Compute conceptually: r = 3.14 - trunc(3.14/2.1) * 2.1.Identify that '%' with doubles is illegal; 'modf' does something different entirely.Verification / Alternative check:
C also offers 'remainder(x, y)' which returns r = x - rint(x/y) * y with possibly different sign/ magnitude conventions; the prompt specifically expects 'fmod'.Why Other Options Are Wrong:
Common Pitfalls:
Final Answer:
rem = fmod(3.14, 2.1);
Discussion & Comments