How to format the displayed scores and averages according to the problem specification?

54 views Asked by At
      printf("\n");   //need help with here down and formatting it all correctly
      printf("Name");
      printf("Exam1");
      printf("Exam2");
      printf("Exam3");
      printf("Average\n");
        
        printf("%-10s", name1);
        printf("%-.2f", user1score3);
        printf("%-.2f", user1score2);
        printf("%-.2f", user1score3);
        printf("%-.1f", value1);
        printf("%%\n");
       
        printf("%-10s", name2);
        printf("%-.2f", user2score1);
        printf("%-.2f", user2score2);
        printf("%-.2f", user2score3);
        printf("%-.1f", value2);
        printf("%%\n");
     
        printf("%-10s", name3);
        printf("%-.2f", user3score3);
        printf("%-.2f", user3score2);
        printf("%-.2f", user3score3);
        printf("%-.1f", value3);
        printf("%%\n");
      
    
      return 0;
    }

[![This is my assignment below][1]][1]

[1]: https://i.stack.imgur.com/bZWIT.png

https://i.stack.imgur.com/bZWIT.png

I am confused as to how I am supposed to tell the code how spaced out it should be while also having the decimal place in the correct area. any help is appreciated and thank you all for your time! I would also benefit from someone explaining right and left justification if I'm misunderstanding it fundamentally. Note, this is only the bottom half of my assignment. Let me know if more is needed.

1

There are 1 answers

0
Joshua On

printf format specifiers. All material drawn from "The C Programming", Second Edition, B1.2

Name: string, 10 characters, left justified

%s is for string, to format to 10 characters would be %10s or to format and truncate would be %.10s but we want left justified so %-.10s.

Exam scores, 8 width, 2 decimal places

%f is for floating point (double) output, so that's %8.2f

And another score, 7 width, 1 decimal place; same way as before with different constants

And a trailing percent

That's just %%