I’m writing a program that uses 1 statement to read 6 floating numbers from user. Then have it print the 6 numbers in 3 lines, with all of following requirements:
(1) the numbers are printed in the reverse order that they are read in
(2) they are on 3 lines: 1 number on the first line, 2 numbers on the next line, 3 numbers on the last line
(3) line up the numbers so they are in column format, right justified, with 1 digit after the decimal point
Here’s my attempt for the first 2 requirements
#include <stdio.h>
int main(void)
{
//variable definitions
float f1,f2,f3,f4,f5,f6;
printf ("Enter 6 float numbers, separated by commas: ");
scanf ("%f1,%f2,%f3,%f4,%f5,%f6",&f1,&f2,&f3,&f4,&f5,&f6);
printf ("%f6\n",f6);
printf ("%f5,%f4\n",f5,f4);
printf ("%f3,%f2,%f1\n",f3,f2,f1);
return 0;
}
To my beginner mind, it makes perfect sense.
Here’s the result when i run it
Enter 6 float numbers, separated by commas: 0.2,3.2,0.1,0.5,0.6,0.7
the numbers are:
-107374176.0000006
-107374176.0000005,-107374176.0000004
-107374176.0000003,-107374176.0000002,0.2000001
Press any key to continue . . .
All of them are garbage outputs except for the last one. Appreciate all the helps!
Your format
expects a
1after the firstfloatand before the following comma, a2after the next etc.It should be
Since the separating digits weren’t provided, the second conversion (and the following) failed, and the other
floats remained uninitialised.