Presentation is loading. Please wait.

Presentation is loading. Please wait.

Output Formatting Bina Ramamurthy 4/16/2019 BR.

Similar presentations


Presentation on theme: "Output Formatting Bina Ramamurthy 4/16/2019 BR."— Presentation transcript:

1 Output Formatting Bina Ramamurthy 4/16/2019 BR

2 Introduction We will review the cmath functions
When outputting data we would like to display the appropriately: For exam, right number of digits, left or right justified, in scientific or ordinary notation. IOmanipulation functions are also useful for displaying monetary data. In this discussion we will see how we can do that for C++ program outputs. 4/16/2019 BR

3 Functions in <cmath>
abs(x) computes absolute value of x sqrt(x) computes square root of x, where x >=0 pow(x,y) computes xy ceil(x) nearest integer larger than x floor(x) nearest integer smaller than x exp(x) computes ex log(x) computes ln x, where x >0 log10(x) computes log10x, where x>0 sin(x) sine of x, where x is in radians cos(x) cosine of x, where x is in radians tan(x) tangent of x, where x is in radians 4/16/2019 BR

4 Manipulators and methods
setf() and unsetf() Flag Meaning ios:: showpoint display the decimal point ios::fixed fixed decimal notation ios::scientific scientific notation ios::right right justification ios::left left justification Manipulators in <iomanip> setprecision(n) setw(n) 4/16/2019 BR

5 setw() and setprecision
setw( n ) specifies the number of columns for printing a value; value of the data is printed right justified in the number of columns specified. setprecision( n ): specifies the number of significant digits to be displayed. Lets examine all these “manipulators” using a sample program. 4/16/2019 BR

6 Using IO manipulators #include <iostream>
#include <iomanip> #include <cmath> using namespace std; const double PI=acos(-1.0); int main() { // Declare and initialize objects. double radius(4.6777), area; area = PI*radius*radius; cout << setprecision(4) << "The radius of the circle is: " << setw(10) << radius << " centimeters" << endl; cout.setf(ios::scientific); cout << "The area of the circle is: " << setw(12) << area << " square centimeters" << endl; return 0; } 4/16/2019 BR


Download ppt "Output Formatting Bina Ramamurthy 4/16/2019 BR."

Similar presentations


Ads by Google