Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

When a real number has a higher precision (3.14159 for example) than the space s

ID: 641375 • Letter: W

Question

When a real number has a higher precision (3.14159 for example) than the space specified
in the printf statement (4 spaces for example), what will be printed out? The options we
can think are rounded up (3.15), rounded down (3.14), rounded to the nearest (3.14), or
truncated (3.14). Use 3.14159 as an example, if you see output 3.14, you know that rounded
up is not the correct option. But you cannot tell whether it is rounded down, to the nearest,
or truncated. Write multiple printf() statements with different values to show what is the
correct option on our system.

Explanation / Answer

#include <stdio.h> /* printf */
#include <math.h> /* round, floor, ceil, trunc */

int main ()
{
const char * format = "%.1f %.1f %.1f %.1f %.1f ";
printf ("value round floor ceil trunc ");
printf ("----- ----- ----- ---- ----- ");
printf (format, 2.3,round( 2.3),floor( 2.3),ceil( 2.3),trunc( 2.3));
printf (format, 3.8,round( 3.8),floor( 3.8),ceil( 3.8),trunc( 3.8));
printf (format, 5.5,round( 5.5),floor( 5.5),ceil( 5.5),trunc( 5.5));
printf (format,-2.3,round(-2.3),floor(-2.3),ceil(-2.3),trunc(-2.3));
printf (format,-3.8,round(-3.8),floor(-3.8),ceil(-3.8),trunc(-3.8));
printf (format,-5.5,round(-5.5),floor(-5.5),ceil(-5.5),trunc(-5.5));
return 0;
}


Output:

value round floor ceil trunc
----- ----- ----- ---- -----
2.3 2.0 2.0 3.0 2.0
3.8 4.0 3.0 4.0 3.0
5.5 6.0 5.0 6.0 5.0
-2.3 -2.0 -3.0 -2.0 -2.0
-3.8 -4.0 -4.0 -3.0 -3.0
-5.5 -6.0 -6.0 -5.0 -5.0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote