+ All Categories
Home > Documents > CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output Number formatters are...

CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output Number formatters are...

Date post: 30-Dec-2015
Category:
Upload: walter-john-andrews
View: 214 times
Download: 0 times
Share this document with a friend
59
CSE1222: Lecture 9 The Ohio State University 1
Transcript
Page 1: CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,

CSE1222: Lecture 9 The Ohio State University 1

Page 2: CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,

Formatting Numbers for Output Number formatters are to be used in

conjunction with cout

For example,

double x = 6;cout << x << endl;

The above outputs 6, but x is declared a floating point number, so why isn’t 6.0000… displayed?

CSE1222: Lecture 9 The Ohio State University 2

Page 3: CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,

Formatting Numbers for Output (2) The decimal point is truncated for display, so we

must force it to be shown:

double x = 6;cout.setf(ios::fixed);cout << x << endl;

Notice the cout.setf(ios::fixed) command

It says “force cout to display the decimal point and 6 significant digits afterward.”

Now 6.000000 will be displayed

CSE1222: Lecture 9 The Ohio State University 3

Page 4: CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,

outputFormat1// format output#include <iostream>using namespace std;

int main(){ double x; cout << "Enter x: "; cin >> x;

cout << "x = " << x << endl; // general format, default cout.setf(ios::fixed); // use fixed decimal notation cout << "x = " << x << endl; cout.unsetf(ios::fixed); // unset fixed decimal notation flag cout.setf(ios::scientific); // use scientific notation cout << "x = " << x << endl; return 0;}

CSE1222: Lecture 9 The Ohio State University 4

Page 5: CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,

> outputFormat1.exeEnter x: 123.45x = 123.45x = 123.450000x = 1.234500e+02

> outputFormat1.exeEnter x: 1e15x = 1e+15x = 1000000000000000.000000x = 1.000000e+15

CSE1222: Lecture 9 The Ohio State University 5

cout << "x = " << x << endl; // general format, default

cout.setf(ios::fixed); // use fixed decimal notation

cout << "x = " << x << endl;

cout.unsetf(ios::fixed); // unset fixed decimal notation flag

cout.setf(ios::scientific); // use scientific notation

cout << "x = " << x << endl;

Page 6: CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,

> outputFormat1.exe

Enter x: 1e-15

x = 1e-15

x = 0.000000

x = 1.000000e-15

>

CSE1222: Lecture 9 The Ohio State University 6

cout << "x = " << x << endl; // general format, default

cout.setf(ios::fixed); // use fixed decimal notation

cout << "x = " << x << endl;

cout.unsetf(ios::fixed); // unset fixed decimal notation flag

cout.setf(ios::scientific); // use scientific notation

cout << "x = " << x << endl;

Page 7: CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,

Setting Precision (1) Remember that fixed shows 6 decimal

places by default

But…

If our program dealt with printing out dollar amounts, do we really need to show 6 decimal places?

Another example: if our program dealt with some rigorous scientific simulation, is 6 decimal places enough?

CSE1222: Lecture 9 The Ohio State University 7

Page 8: CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,

Setting Precision (2) The cout.precision(n) formatter

Set the precisionWhere n is the number of decimal places to be

displayed

double x = 6;cout.precision(3);cout.setf(ios::fixed);cout << x << endl;

Now 6.000 is displayed

CSE1222: Lecture 9 The Ohio State University 8

Page 9: CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,

examplePrecision// example of setting precision…int main(){ cout.setf(ios::fixed); cout << "pi/100 = " << M_PI/100.0 << endl;

cout.precision(3); cout << "pi/100 = " << M_PI/100.0 << endl;

cout.precision(12); cout << "pi/100 = " << M_PI/100.0 << endl;

return 0;}

CSE1222: Lecture 9 The Ohio State University 9

Page 10: CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,

> examplePrecision.exepi/100 = 0.031416pi/100 = 0.031pi/100 = 0.031415926536

>

CSE1222: Lecture 9 The Ohio State University 10

… cout.setf(ios::fixed); cout << "pi/100 = " << M_PI/100.0 << endl;

cout.precision(3); cout << "pi/100 = " << M_PI/100.0 << endl;

cout.precision(12); cout << "pi/100 = " << M_PI/100.0 << endl;…

Page 11: CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,

examplePrecision2// example of setting precision…int main(){ cout.setf(ios::scientific); cout << "pi/100 = " << M_PI/100.0 << endl;

cout.precision(3); cout << "pi/100 = " << M_PI/100.0 << endl;

cout.precision(12); cout << "pi/100 = " << M_PI/100.0 << endl;

return 0;}

CSE1222: Lecture 9 The Ohio State University 11

Page 12: CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,

> examplePrecision2.exepi/100 = 3.141593e-02pi/100 = 3.142e-02pi/100 = 3.141592653590e-02

>

CSE1222: Lecture 9 The Ohio State University 12

… cout.setf(ios::scientific); cout << "pi/100 = " << M_PI/100.0 << endl;

cout.precision(3); cout << "pi/100 = " << M_PI/100.0 << endl;

cout.precision(12); cout << "pi/100 = " << M_PI/100.0 << endl;…

Page 13: CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,

examplePrecision3// example of setting precision…int main(){ cout.precision(30);

cout.setf(ios::fixed); cout << "pi/100 = " << M_PI/100.0 << endl; return 0;}

CSE1222: Lecture 9 The Ohio State University 13

> examplePrecision3.exepi/100 = 0.031415926535897934

>

Page 14: CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,

examplePrecision4// example of setting precision…int main(){ cout.precision(30);

cout.setf(ios::scientific); cout << "pi/100 = " << M_PI/100.0 << endl; return 0;}

CSE1222: Lecture 9 The Ohio State University 14

> examplePrecision4.exepi/100 = 3.141592653589793394e-02

>

Page 15: CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,

Double Precision

IEEE Standard for Floating-Point Arithmetic:

Double precision numbers have 16 significant digits

CSE1222: Lecture 9 The Ohio State University 15

Page 16: CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,

outputFormat2// format output#include <iostream>using namespace std;

int main(){ double x;

cout << "Enter x: "; cin >> x;

cout.precision(12); cout << "x = " << x << endl; // general format, default cout.setf(ios::fixed); // use fixed decimal

notation cout << "x = " << x << endl; cout.unsetf(ios::fixed); // unset fixed decimal

notation flag cout.setf(ios::scientific); // use scientific notation cout << "x = " << x << endl; return 0;}

CSE1222: Lecture 9 The Ohio State University 16

Page 17: CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,

> outputFormat2.exeEnter x: 123.45x = 123.45x = 123.450000000000x = 1.234500000000e+02

> outputFormat2.exeEnter x: 1e15x = 1e+15x = 1000000000000000.000000000000x = 1.000000000000e+15

CSE1222: Lecture 9 The Ohio State University 17

cout.precision(12);

cout << "x = " << x << endl; // general format, default

cout.setf(ios::fixed); // use fixed decimal notation

cout << "x = " << x << endl;

cout.unsetf(ios::fixed); // unset fixed decimal notation flag

cout.setf(ios::scientific); // use scientific notation

cout << "x = " << x << endl;

Page 18: CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,

> outputFormat2.exeEnter x: 123456789e-17x = 1.23456789e-09x = 0.000000001235x = 1.234567890000e-09

>

CSE1222: Lecture 9 The Ohio State University 18

cout.precision(12);

cout << "x = " << x << endl; // general format, default

cout.setf(ios::fixed); // use fixed decimal notation

cout << "x = " << x << endl;

cout.unsetf(ios::fixed); // unset fixed decimal notation flag

cout.setf(ios::scientific); // use scientific notation

cout << "x = " << x << endl;

Page 19: CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,

Fixed vs. Scientific cout.precision(12);

cout.setf(ios::fixed);cout << “x = “ << x << endl;

Outputs 12 digits after the decimal place.

cout.precision(12);cout.setf(ios::scientific);cout << “x = “ << x << endl;

Outputs 13 significant digits.

CSE1222: Lecture 9 The Ohio State University 19

Page 20: CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,

CSE1222: Lecture 9 The Ohio State University 20

Page 21: CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,

Formatted Writing to Files

Everything we can do with cout also applies to fout

Including I/O manipulation: setprecision(), setw(), etc.

CSE1222: Lecture 9 The Ohio State University 21

Page 22: CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,

writeFile2.cpp#include <cstdlib> // function exit() is in cstdlib#include <fstream> // class ofstream() is in fstream#include <iomanip>#include <iostream>using namespace std;

int main(){ ofstream fout; // declare an output file stream

fout.open("sample.txt" , ios::out); // open file file_name for output

if (!fout.is_open()) // check if file is opened for output

{ cerr << "Unable to open file sample.txt." << endl; exit(10); }

CSE1222: Lecture 9 The Ohio State University 22

Page 23: CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,

writeFile2.cpp (cont) cout << "Writing to file sample.txt." << endl;

// write to the file fout << "Test file output." << endl; fout << "100.0/3.0 = " << 100.0/3.0 << endl;

fout.precision(12); fout << "100.0/3.0 = " << 100.0/3.0 << endl; fout << "100.0/3.0 = " << fixed << 100.0/3.0 <<

endl;

// close file stream fout fout.close();

return 0; }

CSE1222: Lecture 9 The Ohio State University 23

Page 24: CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,

CSE1222: Lecture 9 The Ohio State University 24

Page 25: CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,

tempTable

CSE1222: Lecture 9 The Ohio State University 25

> tempTable.exe

fahrenheit celsius kelvin -40 -40.00 233.15 -20 -28.89 244.26 0 -17.78 255.37 20 -6.67 266.48 40 4.44 277.59 60 15.56 288.71>

Page 26: CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,

tempTableBad.cpp// Output with no formatting#include <iostream>using namespace std;

int main(){ cout << "fahrenheit celsius kelvin" << endl; for (int fahrenheit = -40; fahrenheit <= 60; fahrenheit += 20) { double celsius = (fahrenheit - 32.0) * (5.0/9.0); double kelvin = celsius + 273.15;

cout << " " << fahrenheit << " " << celsius << " " << kelvin << endl; }

return 0;}

CSE1222: Lecture 9 The Ohio State University 26

Page 27: CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,

tempTableBad

CSE1222: Lecture 9 The Ohio State University 27

> tempTableBad.exefahrenheit celsius kelvin -40 -40 233.15 -20 -28.8889 244.261 0 -17.7778 255.372 20 -6.66667 266.483 40 4.44444 277.594 60 15.5556 288.706 >

cout << "fahrenheit celsius kelvin" << endl; for (int fahrenheit = -40; fahrenheit <= 60; fahrenheit += 20) { double celsius = (fahrenheit - 32.0) * (5.0/9.0); double kelvin = celsius + 273.15;

cout << " " << fahrenheit << " " << celsius << " " << kelvin << endl; }

Page 28: CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,

tempTableBad2.cpp. . .int main(){ cout.setf(ios::fixed); cout.precision(2);

cout << "fahrenheit celsius kelvin" << endl; for (int fahrenheit = -40; fahrenheit <= 60;

fahrenheit += 20) { double celsius = (fahrenheit - 32.0) * (5.0/9.0); double kelvin = celsius + 273.15;

cout << " " << fahrenheit << " " << celsius << " " << kelvin <<

endl; }

return 0;}

CSE1222: Lecture 9 The Ohio State University 28

Page 29: CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,

tempTableBad2

CSE1222: Lecture 9 The Ohio State University 29

> tempTableBad2.exefahrenheit celsius kelvin -40 -40.00 233.15 -20 -28.89 244.26 0 -17.78 255.37 20 -6.67 266.48 40 4.44 277.59 60 15.56 288.71>

cout << "fahrenheit celsius kelvin" << endl; for (int fahrenheit = -40; fahrenheit <= 60; fahrenheit += 20) { double celsius = (fahrenheit - 32.0) * (5.0/9.0); double kelvin = celsius + 273.15;

cout << " " << fahrenheit << " " << celsius << " " << kelvin << endl; }

Page 30: CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,

Width Problem

CSE1222: Lecture 9 The Ohio State University 30

> tempTableBad2.exefahrenheit celsius kelvin -40 -40.00 233.15 -20 -28.89 244.26 0 -17.78 255.37 20 -6.67 266.48 40 4.44 277.59 60 15.56 288.71>

cout << " " << fahrenheit << " " << celsius << " " << kelvin << endl;

Problem: Need to fix the width of each output field.

Page 31: CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,

#include <iomanip>

Output can also be formatted using routines in iomanip

To use of these functions, include this file in your program header:

#include <iomanip>

CSE1222: Lecture 9 The Ohio State University 31

Page 32: CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,

setw The formatter setw(n) sets the width of the output to at least n

characters. String/value is right justified. Example:

cout << setw(10)<< “Age” << setw(10) << “Weight” << endl;cout << setw(10) << 4 << setw(10) << 32 << endl;cout << setw(10) << 22 << setw(10) << 130.5 << endl;cout << setw(10) << 101 << setw(10) << 120 << endl;

Output is: Age Weight 4 32 22 130.5 101 120

CSE1222: Lecture 9 The Ohio State University 32

Page 33: CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,

tempTable.cpp#include <iostream>#include <iomanip> // Note: Must include iomanip...int main(){ cout.setf(ios::fixed); cout.precision(2);

cout << "fahrenheit celsius kelvin" << endl; for (int fahrenheit = -40; fahrenheit <= 60;

fahrenheit += 20) { double celsius = (fahrenheit - 32.0) * (5.0/9.0); double kelvin = celsius + 273.15;

cout << setw(6) << fahrenheit << setw(15) << celsius << setw(10) << kelvin << endl;

}...

CSE1222: Lecture 9 The Ohio State University 33

Page 34: CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,

tempTable

CSE1222: Lecture 9 The Ohio State University 34

> tempTable.exefahrenheit celsius kelvin -40 -40.00 233.15 -20 -28.89 244.26 0 -17.78 255.37 20 -6.67 266.48 40 4.44 277.59 60 15.56 288.71>

cout << setw(6) << fahrenheit << setw(15) << celsius << setw(10) << kelvin << endl;

Page 35: CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,

trigTable

CSE1222: Lecture 9 The Ohio State University 35

> trigTable.exe degrees radians sin cos -180 -3.14 -0.00 -1.00 -150 -2.62 -0.50 -0.87 -120 -2.09 -0.87 -0.50 -90 -1.57 -1.00 0.00 -60 -1.05 -0.87 0.50 -30 -0.52 -0.50 0.87 0 0.00 0.00 1.00 30 0.52 0.50 0.87 60 1.05 0.87 0.50 90 1.57 1.00 0.00 120 2.09 0.87 -0.50 150 2.62 0.50 -0.87 180 3.14 0.00 -1.00>

Page 36: CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,

trigTable.cpp...#include <iomanip> // NOTE: Must include iomanip... cout.setf(ios::fixed); cout.precision(2);

cout << setw(8) << "degrees" << setw(8) << "radians" << setw(8) << "sin" << setw(8) << "cos" << endl;

for (int degrees = -180; degrees <= 180; degrees += 30)

{ double radians = degrees * M_PI/180.0;

cout << setw(8) << degrees << setw(8) << radians << setw(8) << sin(radians) << setw(8) <<

cos(radians) << endl; }...

CSE1222: Lecture 9 The Ohio State University 36

Page 37: CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,

trigTable

CSE1222: Lecture 9 The Ohio State University 37

> trigTable.exe degrees radians sin cos -180 -3.14 -0.00 -1.00 -150 -2.62 -0.50 -0.87 -120 -2.09 -0.87 -0.50 -90 -1.57 -1.00 0.00 -60 -1.05 -0.87 0.50 -30 -0.52 -0.50 0.87 0 0.00 0.00 1.00 30 0.52 0.50 0.87 60 1.05 0.87 0.50 90 1.57 1.00 0.00 120 2.09 0.87 -0.50 150 2.62 0.50 -0.87 180 3.14 0.00 -1.00>

cout << setw(8) << "degrees" << setw(8) << "radians" << setw(8) << "sin" << setw(8) << "cos" << endl;

Page 38: CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,

expTable

CSE1222: Lecture 9 The Ohio State University 38

> expTable.exeEnter number of values: 10 x e^x 2^x 1 2.72 2.00 2 7.39 4.00 3 20.09 8.00 4 54.60 16.00 5 148.41 32.00 6 403.43 64.00 7 1096.63 128.00 8 2980.96 256.00 9 8103.08 512.00 10 22026.47 1024.00>

Page 39: CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,

expTable.cpp...#include <iomanip> // NOTE: Must include iomanip... cout << "Enter number of values: "; cin >> n;

cout.setf(ios::fixed); cout.precision(2);

cout << setw(5) << "x" << setw(10) << "e^x" << setw(10) << "2^x" << endl;

for (int i = 1; i <= n; i++) { double x = i; cout << setw(5) << i << setw(10) << exp(x) <<

setw(10) << pow(2.0,x) << endl; }...

CSE1222: Lecture 9 The Ohio State University 39

Page 40: CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,

expTable

CSE1222: Lecture 9 The Ohio State University 40

> expTable.exeEnter number of values: 10 x e^x 2^x 1 2.72 2.00 2 7.39 4.00 3 20.09 8.00 4 54.60 16.00 5 148.41 32.00 6 403.43 64.00 7 1096.63 128.00 8 2980.96 256.00 9 8103.08 512.00 10 22026.47 1024.00>

cout << setw(5) << i << setw(10) << exp(x) << setw(10) << pow(2.0,x) << endl;

Page 41: CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,

expTable

CSE1222: Lecture 9 The Ohio State University 41

> expTable.exeEnter number of values: 15 x e^x 2^x 1 2.72 2.00 2 7.39 4.00... 11 59874.14 2048.00 12 162754.79 4096.00 13 442413.39 8192.00 141202604.28 16384.00 153269017.37 32768.00>

cout << setw(5) << i << setw(10) << exp(x) << setw(10) << pow(2.0,x) << endl;

Page 42: CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,

expTable

CSE1222: Lecture 9 The Ohio State University 42

> expTable.exeEnter number of values: 25 x e^x 2^x 1 2.72 2.00 2 7.39 4.00... 13 442413.39 8192.00 141202604.28 16384.00 153269017.37 32768.00... 239744803446.258388608.00 2426489122129.8416777216.00 2572004899337.3933554432.00 >

cout << setw(5) << i << setw(10) << exp(x) << setw(10) << pow(2.0,x) << endl;

Page 43: CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,

expTable2.cpp... cout << "Enter number of values: "; cin >> n;

cout.setf(ios::fixed); cout.precision(2);

cout << setw(5) << "x" << " " << setw(10) << "e^x" << " " << setw(10) << "2^x" << endl;

for (int i = 1; i <= n; i++) { double x = i; // Add spaces between fields. cout << setw(5) << i << " " << setw(10) << exp(x) << " " << setw(10) << pow(2.0,x) << endl; }...

CSE1222: Lecture 9 The Ohio State University 43

Page 44: CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,

expTable2

CSE1222: Lecture 9 The Ohio State University 44

> expTable2.exeEnter number of values: 10 x e^x 2^x 1 2.72 2.00 2 7.39 4.00 3 20.09 8.00 4 54.60 16.00 5 148.41 32.00 6 403.43 64.00 7 1096.63 128.00 8 2980.96 256.00 9 8103.08 512.00 10 22026.47 1024.00>

// Add spaces between fields cout << setw(5) << i << " " << setw(10) << exp(x) << " " << setw(10) << pow(2.0,x) << endl;

Page 45: CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,

expTable2

CSE1222: Lecture 9 The Ohio State University 45

> expTable2.exeEnter number of values: 15 x e^x 2^x 1 2.72 2.00 2 7.39 4.00... 10 22026.47 1024.00 11 59874.14 2048.00 12 162754.79 4096.00 13 442413.39 8192.00 14 1202604.28 16384.00 15 3269017.37 32768.00>

// Add spaces between fieldscout << setw(5) << i << " " << setw(10) << exp(x) << " " << setw(10) << pow(2.0,x) << endl;

Page 46: CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,

expTable2

CSE1222: Lecture 9 The Ohio State University 46

> expTable2.exeEnter number of values: 25 x e^x 2^x 1 2.72 2.00 2 7.39 4.00... 20 485165195.41 1048576.00 21 1318815734.48 2097152.00 22 3584912846.13 4194304.00 23 9744803446.25 8388608.00 24 26489122129.84 16777216.00 25 72004899337.39 33554432.00>

// Add spaces between fieldscout << setw(5) << i << " " << setw(10) << exp(x) << " " << setw(10) << pow(2.0,x) << endl;

Page 47: CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,

expTable3.cpp... cout << "Enter number of values: "; cin >> n;

// DO NOT SET FIXED OUTPUT FORMAT // cout.setf(ios::fixed); cout.precision(2);

cout << setw(5) << "x" << setw(10) << "e^x" << setw(10) << "2^x" << endl;

for (int i = 1; i <= n; i++) { double x = i; cout << setw(5) << i << setw(10) << exp(x) << setw(10) << pow(2.0,x) << endl; }...

CSE1222: Lecture 9 The Ohio State University 47

Page 48: CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,

expTable3

CSE1222: Lecture 9 The Ohio State University 48

> expTable3.exeEnter number of values: 25 1 2.7 2 2 7.4 4... 20 4.9e+08 1e+06 21 1.3e+09 2.1e+06 22 3.6e+09 4.2e+06 23 9.7e+09 8.4e+06 24 2.6e+10 1.7e+07 25 7.2e+10 3.4e+07>

cout << setw(5) << i << setw(10) << exp(x) << setw(10) << pow(2.0,x) << endl;

Page 49: CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,

expTable3

CSE1222: Lecture 9 The Ohio State University 49

> expTable3.exeEnter number of values: 10 x e^x 2^x 1 2.7 2 2 7.4 4 3 20 8 4 55 16 5 1.5e+02 32 6 4e+02 64 7 1.1e+03 1.3e+02 8 3e+03 2.6e+02 9 8.1e+03 5.1e+02 10 2.2e+04 1e+03 >

cout << setw(5) << i << setw(10) << exp(x) << setw(10) << pow(2.0,x) << endl;

Page 50: CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,

Other Formatters Iomanip contains many other formatters, including:

fixed Force show a decimal point with 6 trailing decimal places

scientific Outputs floating point number in scientific notation

hex Outputs integer in hexadecimal (base16)

oct Outputs integer in octal (base 8)

showpos Force positive numbers to be shown with a leading ‘+’ symbol

setprecision(n) Set floating point precision to n places

setw(n) Set the field width to n characters

CSE1222: Lecture 9 The Ohio State University 50

Page 51: CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,

outputFormat3// format output#include <iostream>#include <iomanip>using namespace std;

int main(){ double x;

cout << "Enter x: "; cin >> x;

// general format, default, 12 significant digits cout << "x = " << setprecision(12) << x << endl; // fixed decimal notation, 12 digits after decimal

point cout << "x = " << fixed << setprecision(12) << x <<

endl; // scientific notation, 12 digits after decimal point cout << "x = " << scientific << setprecision(12) << x

<< endl; return 0;}

CSE1222: Lecture 9 The Ohio State University 51

Page 52: CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,

> outputFormat3.exeEnter x: 123.45x = 123.45x = 123.450000000000x = 1.234500000000e+02

> outputFormat3.exeEnter x: 123456789e-17x = 1.23456789e-09x = 0.000000001235x = 1.234567890000e-09

CSE1222: Lecture 9 The Ohio State University 52

// general format, default, 12 significant digits

cout << "x = " << setprecision(12) << x << endl;

// fixed decimal notation, 12 digits after decimal point

cout << "x = " << fixed << setprecision(12) << x << endl;

// scientific notation, 12 digits after decimal point

cout << "x = " << scientific << setprecision(12) << x << endl;

Page 53: CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,

Rounding Output// example of output rounding

#include <iostream>#include <iomanip>using namespace std;

int main(){ double x = 1.0/7.0;

cout << "x = " << setprecision(12) << x << endl; cout << "x = " << setprecision(7) << x << endl; cout << "x = " << setprecision(5) << x << endl; return 0;}

CSE1222: Lecture 9 The Ohio State University 53

• C++ rounds output

Page 54: CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,

> roundOutput.exex = 0.142857142857x = 0.1428571x = 0.14286

>

CSE1222: Lecture 9 The Ohio State University 54

double x = 1.0/7.0;

cout << "x = " << setprecision(12) << x << endl;

cout << "x = " << setprecision(7) << x << endl;

cout << "x = " << setprecision(5) << x << endl;

Page 55: CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,

expTable

CSE1222: Lecture 9 The Ohio State University 55

> expTable.exeEnter number of values: 10 x e^x 2^x 1 2.72 2.00 2 7.39 4.00 3 20.09 8.00 4 54.60 16.00 5 148.41 32.00 6 403.43 64.00 7 1096.63 128.00 8 2980.96 256.00 9 8103.08 512.00 10 22026.47 1024.00>

Page 56: CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,

expTable4

CSE1222: Lecture 9 The Ohio State University 56

> expTable4.exeEnter number of values: 10 x e^x 2^x 1 2.72 2 2 7.39 4 3 20.09 8 4 54.60 16 5 148.41 32 6 403.43 64 7 1096.63 128 8 2980.96 256 9 8103.08 512 10 22026.47 1024>

Page 57: CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,

expTable4.cpp... cout << "Enter number of values: "; cin >> n;

cout.setf(ios::fixed);

cout << setw(5) << "x" << " " << setw(10) << "e^x" << " " << setw(8) << "2^x" << endl;

for (int i = 1; i <= n; i++) { double x = i; // Use precision 2 for e^x and precision 0

(integer) for 2^x. cout << setw(5) << i << " " << setw(10) <<

setprecision(2) << exp(x) << " " << setw(8) << setprecision(0) << pow(2.0,x) << endl;

}...

CSE1222: Lecture 9 The Ohio State University 57

Page 58: CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,

expTable

CSE1222: Lecture 9 The Ohio State University 58

> expTable4.exeEnter number of values: 10 x e^x 2^x 1 2.72 2 2 7.39 4 3 20.09 8 4 54.60 16 5 148.41 32 6 403.43 64 7 1096.63 128 8 2980.96 256 9 8103.08 512 10 22026.47 1024>

// Use precision 2 for e^x and precision 0 (integer) for 2^x.cout << setw(5) << i << " " << setw(10) << setprecision(2) << exp(x) << " " << setw(8) << setprecision(0) << pow(2.0,x) << endl;

Page 59: CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,

Summary cout.setf(ios::fixed); // use fixed decimal notation cout.setf(ios::scientific); // use scientific notation cout.precision(k); // print k digits after decimal

iomanip: #include <iomanip> // include IO manipulators(formatters) cout << setw(k) << ...; // set next field width to k cout << setprecision(k) << ...; // print k digits after decimal cout << fixed << ...; // switch to fixed decimal notation cout << scientific << ...; // switch to scientific notation

CSE1222: Lecture 9 The Ohio State University 59


Recommended