/* This program shows you how you can format output with C++. */ #include // include I/O Stream definitions #include // include manipulator definitions #include // std::streamsize #include // old C style printf and ilk #include using namespace std; int main() { // b is a 2x2 "matrix". // The C++ counterpart to this F90 RESHAPE command // b = RESHAPE( (/1.11111E300,-2.2222E12,3.3333,-4.0E7/),(/2,2/)) // is this: double b[2][2] = {{1.11111E300,-2.2222E12},{3.3333,-4.0E7}}; // The counterpart to F90's list-directed formatting "Print *, b" is: for (size_t i = 0; i != 2; ++i) for (size_t j= 0; j != 2; ++j) cout << " " << b[i][j]; cout << endl; // The approximate counterpart of F90's explicit formatting // with e (general) edit descriptor: Print "(2e16.4)", b for (size_t i = 0; i != 2; ++i) { for (size_t j= 0; j != 2; ++j) cout << setw(16) << setprecision(4) << b[i][j]; cout << endl; } cout << endl; // You can also use old C-style output for (size_t i = 0; i != 2; ++i) { for (size_t j= 0; j != 2; ++j) printf ("%12.2e", b[i][j]); printf ("\n"); } } // Note: it is generally not a good idea to mix C style // I/O with C++ style I/O. Output: // 1.11111e+300 -2.2222e+12 3.3333 -4e+07 // 1.111e+300 -2.222e+12 // 3.333 -4e+07 // // 1.11e+300 -2.22e+12 // 3.33e+00 -4.00e+07