Hi I have a assignment about formatting C
this is the assignment


print_patterns(int i, double d) - Print 10 lines as specified below.
* Lines 1 and 2 are fixed patterns with 20 characters each.
* Line 3-6 are for integer i (sample value 123).
* Line 7-10 are for double d (sample value 456.789).
*
* LINE# DISPLAYED PATTERN COMMENTS
* 1 12345678901234567890 fixed pattern
* 2 -------------------- fixed pattern
* 3 123 ends at the 10th position
* 4 123 starts from the 11th position
* 5 00000000000000000123 flushes to the right, with padding
* 6 123 123 two occurrences, flush to both ends
* 7 456.789 flushes to the right, no padding
* 8 0000000000000456.789 flushes to the right, with padding
* 9 0000000456.789000000 decimal point at 11th position, with padding
* 10 00456.78900 decimal point at 11th position, pads to 5 digits
* before and after decimal point

so far I got them to line 7, but I have no idea how to do 8,9,10 any one can help ?

Recommended Answers

All 6 Replies

There are two parts for the field width using %f. The first part is the total width including radix and precision. The second part is the width of the precision. A leading 0 will change the padding from whitespace to zeros:

printf("%020.3f\n", 123.456); /* Line 8 */
printf("%020.9f\n", 123.456); /* Line 9 */
printf("%011.5f\n", 123.456); /* Line 10 */

There are two parts for the field width using %f. The first part is the total width including radix and precision. The second part is the width of the precision. A leading 0 will change the padding from whitespace to zeros:

printf("%020.3f\n", 123.456); /* Line 8 */
printf("%020.9f\n", 123.456); /* Line 9 */
printf("%011.5f\n", 123.456); /* Line 10 */

thanks man, but for line 10 how you I can move the decimal point to the 11th position ?

this my code

void print_patterns(int i, double d)
{
printf ( "%s\n", "12345678901234567890");
printf ( "%s\n", "--------------------");
printf ( "%-10d\n", i);
printf ( "%13d\n", i);
printf ( "%.20d\n", i);
printf ( "%d              %d\n", i, i);
printf ( "%20.3lf\n", d);
printf("%020.3f\n", d);
printf("%020.9f\n", d);
printf("%011.5f\n", d);
}

how you I can move the decimal point to the 11th position ?

Do the math. What total length less the specified precision will place your radix at the 11th position?

Do the math. What total length less the specified precision will place your radix at the 11th position?

you mean like this

printf("%016.5f\n", d);

but I need to look like this

LINE#   DISPLAYED PATTERN       COMMENTS
 *   1       12345678901234567890    fixed pattern
 *   2       --------------------    fixed pattern
 *   3              123              ends at the 10th position
 *   4                 123           starts from the 11th position
 *   5       00000000000000000123    flushes to the right, with padding
 *   6       123              123    two occurrences, flush to both ends
 *   7                    456.789    flushes to the right, no padding
 *   8       0000000000000456.789    flushes to the right, with padding
 *   9       0000000456.789000000    decimal point at 11th position, with padding
 *  10            00456.78900        decimal point at 11th position, pads to 5 digits
 *                                     before and after decimal point

How I can get rid of the leading zeros ?

How I can get rid of the leading zeros ?

Read my first post again.

Read my first post again.

thanks I got man ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.