2

I need to align my numbers properly. I'm having issues dealing with floats that are one and three-digits long. Here's my output:

ID G1 G2 G3 Average 000000065 92.000000 93.000000 86.000000 90.333336 000000101 85.500000 75.500000 90.000000 83.666664 000002202 100.000000 92.000000 87.250000 93.083336 000022227 96.000000 84.000000 75.500000 85.166664 000031303 99.000000 87.000000 62.000000 82.666664 101010010 0.000000 81.000000 91.000000 57.333332 424242428 77.000000 77.000000 87.500000 80.500000 700666124 88.000000 65.000000 89.000000 80.666664 812345676 95.000000 76.000000 87.000000 86.000000 999999999 99.000000 99.500000 100.000000 99.500000 

Here's my print function:

//function that prints out contents of tree int print_tree (NodePtr treePtr){ // if statement begins if (treePtr != NULL){ print_tree(treePtr->left); printf( "%.9d %f %f %f %f\n\n", treePtr->studentID, treePtr->g1, treePtr->g2, treePtr->g3, treePtr->average); print_tree(treePtr->right); }//if statement ends return 0;//indicates successful termination }//end print_tree 

As you can see some numbers aren't properly lined up due to the one and three digit-long floats (I need to print floats).

2
  • .9 is for integers. It works because everything is spaced evenly for the IDs, but not G1, G2, G3 and Average Commented Feb 12, 2014 at 16:23
  • Once you have your numeric spacing sorted out, you might want to go back and include format specifiers for your header labels to align them either right or left justified above the numbers. The tutorial that @ryyker points to in his answer should cover it. Commented Feb 12, 2014 at 16:43

3 Answers 3

1

Here is a good tutorial on using format specifiers for spacing

A simple example: Use format specifiers such as:

 printf("%5.3f", 13.3423452); 

In your case use:

"%09d" for integers, The 09 will guarantee 9 spaces used, padded with 0s
eg: for 123, 000000123 would print.

"%9.7f" for floats, 9 Guarantees field will be at least 9 wide, 7 will give 7 digits after "."

on the last column of each row of course add a \n.

Code example: Say I had the following input, formatted as shown:

printf("%20.7f\n", 1213.342345287867587); printf("%20.7f\n", 13.342); printf("%20.7f\n", 1213.342345287867587); printf("%20.7f\n", 1213.342345287867587); printf("%020d", 3); 

The output would look like this:

enter image description here

Note:, Every column is 20 wide. (because of the leading 20 in format spec.)
floats are padded with spaces as specified to align number.
Integers are padded with 0s to align. (because of the 020 format spec.)

Sign up to request clarification or add additional context in comments.

1 Comment

Hmmm. I believe you are after uniform column width. If that is true, pick a number in the format specifier at least as wide as the longest number you would like to display, +1 for spacing between columns (or +2 or +3 if more spacing is desired between columns). Use the ".n" in the format specifier for floats to dictate how many numbers to display after the decimal. If you do these things, your formatting will align. For more exhaustive help, the examples (along with link) pretty much cover how to align floats and integers.
0

You should specify the field width as well the decimal places: e.g. for 9 columns each time:

printf( "%.9d %9.9f %9.9f %9.9f %9.9f\n\n", treePtr->studentID, treePtr->g1, treePtr->g2, treePtr->g3, treePtr->average); 

Comments

0

Another quick solution is using tab character to do it for you (though it fails to align properly if digits count difference is more than 8):

printf("%d\t%f\t%f\t%f\t%f\t%f\n\n", treePtr->studentID, treePtr->g1, treePtr->g2, treePtr->g3, treePtr->average); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.