Output formatting & Makefile

Paths

  • A path is how you refer to files and directories.
    • It gives the location of a file or directory in the Linux directory structure.
    • It is composed of a name and slash syntax.

Some common paths

  • . : current directory
  • ..: parent directory
  • ~: user’s home directory
  • /: root directory

You can combine these paths using the / operator.

  • ../..: grandparent directory
  • ~/home/user: directory of user

Absolute path VS relative path

If the path starts with slash “/”,

  • It is an absolute path.
  • the first slash denotes root.
  • The rest of the slashes in the path are just separators.

If the path does not start with slash “/”,

  • It is a relative path.
  • It is relative to the current working directory.

Examples:

  • pwd: print working directory, it returns the absolute path of the current working directory.
  • ./data/file.txt: relative path to the file file.txt.
  • /home/user/project: absolute path to the project directory.

Some most common options for g++:

  • -Wall: show all warnings.
  • -g: provides debugging feature for your program.
  • --std=c++<##>: uses version <##> of C++ when compiling.
  • -o <filename>: compiles and links files into an executable named <filename>.
    • The default filename is a.out.
  • -c: compiles and assembles files but doesn’t link them.
    • useful when separating file compilation and minimize what is re-compiled.
  • -I <directory>: add directory to the list of directories to be searched for header files. (gcc/g++)

Some most common options for MSVC:

  • /W<##>: show warnings of level <##>.
  • /Zi: generate PDB file for debugging.
  • /std<##>: uses version <##> of C++ when compiling.
  • /Fe <filename>: compiles and links files into an executable named <filename>.
    • The default filename is the name of the first file.
  • /c: compiles and assembles files but doesn’t link them.
    • useful when separating file compilation and minimize what is re-compiled.
  • /I <directory>: add directory to the list of directories to be searched for header files. (MSVC)

An example

Compile a project with the following structure:

├── SimpleMC.cpp
├── SimpleMC.h
├── SimpleMCmain2.cpp
├── payoff
│   ├── PayOff1.cpp
│   └── PayOff1.h
└── random
    ├── Random1.cpp
    └── Random1.h

Output formatting

In C++, a manipulator is a function or object that is used to modify the behavior of input/output streams (like std::cin and std::cout).

Some common manipulators:

  • std::endl: insert a newline character and flush the output buffer.
  • The following manipulators are defined in <iomanip>:
    • std::setw(int n): Sets the width of the next input/output field to n.
    • std::hex, std::dec, std::oct: Set the base for integer output. It will affect all subsequent integer output.

Float formatting

There are 3 floating-point formats: general, fixed, and scientific.

  • Fixed format always has a number, decimal point, and fraction part, no matter how big the number gets
  • Scientific format displays a number in scientific notation.
  • General format is a combination of fixed and scientific formats, depending on the number.

Example:

#include <iostream>

using std::cout;
using std::endl;
using std::scientific;
using std::fixed;
using std::ios;

int main()
{
    float small = 3.1415926535897932384626;
    float large = 6.0234567e17;
    float whole = 2.000000000;

    cout << "Some values in general format" << endl;
    cout << "small:  " << small << endl;
    cout << "large:  " << large << endl;
    cout << "whole:  " << whole << endl << endl;

    cout << scientific;

    cout << "The values in scientific format" << endl;
    cout << "small:  " << small << endl;
    cout << "large:  " << large << endl;
    cout << "whole:  " << whole << endl << endl;

    cout << fixed;

    cout << "The same values in fixed format" << endl;
    cout << "small:  " << small << endl;
    cout << "large:  " << large << endl;
    cout << "whole:  " << whole << endl << endl;

    // Doesn't work -- doesn't exist
    // cout << general;

    cout.unsetf(ios::fixed | ios::scientific);

    cout << "Back to general format" << endl;
    cout << "small:  " << small << endl;
    cout << "large:  " << large << endl;
    cout << "whole:  " << whole << endl << endl;

    return 0;
}

Precision

  • The precision of a floating-point number is the number of significant digits displayed for the number.
  • The precision is specified by the std::setprecision(int n) manipulator.
  • The default precision is 6.
Note

These manipulators affect all subsequent floating-point output:

  • std::hex, std::dec, std::oct
  • std::fixed, std::scientific, std::setprecision

Output filed width

A very useful thing to do in a program is output numbers and strings in fields of fixed width.

std::setw() adjusts the field width for the item about to be printed.

Note
  • The std::setw() manipulator only affects the next value to be printed.
  • The std::setw() manipulator takes an integer argument which is the minimum field width for the value to be printed.

Example:

#include <iostream>
#include <iomanip>

using std::cout;
using std::endl;
using std::setw;

int main()
{
    // A test of setw()

    cout << "*" << -17 << "*" << endl;
    cout << "*" << setw(6) << -17 << "*" << endl << endl;

    cout << "*" << "Hi there!" << "*" << endl;
    cout << "*" << setw(20) << "Hi there!" << "*" << endl;
    cout << "*" << setw(3) << "Hi there!" << "*" << endl;

    return 0;
}

Assignment

Assignment 1: Declaration and formatting output

  • Integer types:
    • Declare an integer a with value 56789.
    • Choose proper type for b which equals to a * a.
    • Choose proper type for c which equals to a * a * a.
  • Floating-point types:
    • Declare a float number x with value 3.1415926.
    • Declare a double number y with the same value.
    • Declare a long double number z with the same value.

Don’t forget to use literals to make your code more readable.

  • Output the values in four columns.
    • The first column: the type name of the integers with column width to 15.
    • The second column: the hex values of a, b, c, set column width to 15.
    • The third column: the type name of the floating-point numbers with column width to 15.
    • The fourth column: the values of x, y, z, set precision to 15, use fixed format, column width to 20.

Final output should look like:

                 int           ddd5               float    3.14159250259399
           long long       c0397339              double   3.141592600000000
           long long   a6918845136d         long double   3.141592600000000

Assignment 2: Compile program with g++/MSVC

Follow the instructions during the class, compile the project with g++/MSVC.

  • File structure:
    • Remove all the files/directories except header files and source files.
    • Create a new directory payoff and put Payoff1.h and Payoff1.cpp in this directory.
    • Create a new directory random and put Random1.h and Random1.cpp in this directory.
    • Use tree command to show the file structure. (Windows users should add /f to show file names)

Compile:

  • Compile Payoff1.cpp, Random1.cpp, SimpleMC.cpp and SimpleMCmain2.cpp separately without linking.
  • Link the object files into an executable.

Submission

  • Submit format.cpp for assignment 1.
  • Take screenshots for each step and put them in a pdf file named lab1.pdf for assignment 2.
  • Submit both files to Canvas.

Suggested reading

The format manipulators are not hard to use, you don’t need to memorize them. You can always refer to this page for help.