Lab EC - Formating output with printf and cout (Extra Credit)

Due Wenesday October 12, 2011 before class begins (e.g. no later than 12:20pm)

The purpose of this lab is to learn how to format output using both C and C++ syntax.

You may work in groups on this assignment. The lecture room (Sci III 315) is available Monday October 10th during lecture time (12:20-1:40) for you to work on the assignment. Alternatively, you can use the Walk-In Lab (Sci III 324) or work from home. This is an extra credit assignment, but you will be required to know how to format output for future required assignments and exams.

Part 1: Formating with printf

The printf() function is the C-style method of printing output to the screen. Pure C programs will use printf() for output instead of cout. To use the printf() function, you must include stdio.h and also make a function call.

The function call can take multiple arguments. The first argument is always a format string. The format string can be either just a plain string similar to what we've sent to cout (in which case it is the only argument to printf) or a string with formating tokens embedded in it. The formating tokens dictate how to display certain datatypes. It takes the form:

%[-][0][col][.prec]type
The - character is optional and will left justify the text (right justify is the default if - is omited).
The 0 character is optional and will pad the unused space with 0's. If 0 is omitted, spaces will be used for pad characters.
"col" is an integer for the column width. It is optional. If left off, the column will be wide enough to hold the variable.
".prec" is an integer preceeded by a decimal point for the number of digits of precision to use for doubles and floats. It is optional. If left off, the default precision will be used.
The type is a single character that represents the datatype of the variable. It is d for an integer, f for a double (or float) and c for a character. There are other types available. See man printf for a list.

Examples:

Take the following program and input it into a file on Sleipnir. Compile and run the program once you have copied it over. Notice how the numbers on the first line of output are default formatted, while the second line has columns and the third line has columns, justification, and precision.
#include <iostream>
#include <stdio.h>
using namespace std;

int main()
{
  int num1, num2;
  double dnum1, dnum2;

  printf("Enter two integers: ");
  cin >> num1 >> num2;
  printf("Enter two doubles: ");
  cin >> dnum1 >> dnum2;

  printf("%d %d %f %f.\n", num1, num2, dnum1, dnum2);
  printf("%10d %10d %10f %10f.\n", num1, num2, dnum1, dnum2);
  printf("%010d %-10d %10.2f %10.4f.\n", num1, num2, dnum1, dnum2);

  return 0;
}

Part 2: Formating with cout

The cout object can also be used to format output. The method is not as straight-forward as with printf. To format cout output, you must include the iomanip library. This will give you access to several functions, as detailed below: Look at the code in labEC_example.cpp. This code creates the following output using cout manipulators:
integer output
right justify: |         5|
 left justify: |5         |

double output (2 precision)
right justify: |      5.50|
 left justify: |5.50      |

A small table without borders:
Name      Integer   Double
Alice              5      6.20
Bob                8     13.34
Carol             12     19.00
Copy the file over to your current directory using the following command (note: there is a dot at the end of the command, this represents the current working directory is the destination of the copy command):
cp /home/fac/melissa/public_html/cs221/labEC_example.cpp .
Compile and run the code. You should see output similar to above.

Lab Assignment

Recreate the output of labEC_example.cpp using printf() in a file called labEC.cpp. Email your completed labEC.cpp to my Sleipnir account.