Beginner C - prints custom float inputs in reverse order

I am writing a program that uses 1 instruction to read 6 floats from the user. Then print 6 numbers on 3 lines with all of the following requirements:

(1) the numbers are printed in reverse order to be 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) align the numbers so that they are in column format, on the right side, 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;

}

      

For my novice mind, this makes sense.

Here is the output when I run it

Enter 6 floating point numbers separated by comma: 0,2,3,2,0,1,0,5,0,6,0,7

numbers:

-107374176.0000006

-107374176.0000005, -107374176.0000004

-107374176.0000003, -107374176.0000002,0.2000001

Press any key to continue.,.

These are all trash exits, except for the last one. Appreciate all the helpful advice!

+3


source to share


3 answers


Your format

scanf ("%f1,%f2,%f3,%f4,%f5,%f6",&f1,&f2,&f3,&f4,&f5,&f6);

      

waits 1

after the first float

and before the next comma a 2

after the next, etc.



It should be

scanf ("%f,%f,%f,%f,%f,%f",&f1,&f2,&f3,&f4,&f5,&f6);

      

Since no separator digits were provided, the second conversion (and the next) failed and the other float

remained uninitialized.

+6


source


The problem is in the format:

scanf ("%f1,%f2,%f3,%f4,%f5,%f6",&f1,&f2,&f3,&f4,&f5,&f6);

      



Should be

scanf ("%f,%f,%f,%f,%f,%f",&f1,&f2,&f3,&f4,&f5,&f6);

      

+2


source


The correct format for printing floating point numbers is:

printf("%.1f", variable);

      

In this example, the number in variable

will be printed with 1 digit after the decimal point.

+1


source







All Articles