Output Formatting Bina Ramamurthy 4/16/2019 BR.

Slides:



Advertisements
Similar presentations
1 Algorithms Basic Control Structures Comparisons and if (…) statement What is a function? Math Library Functions Character Functions Reading Sample Programs.
Advertisements

More Review, with Some New Chapter 3. Review C++ simple data types – Integral and float – Arithmetic operators Expressions and expression evaluation –
I/O Streams as an Introduction to Objects and Classes
I/O and Program Control Statements Dick Steflik. Overloading C++ provides two types of overloading –function overloading the ability to use the same function.
Programming Stream Manipulators. COMP102 Prog. Fundamentals I: Stream Manipulator Slide 2 Stream Manipulators in iostream.h Library I/O stream manipulators.
1 Lecture 6: Input/Output (II) Introduction to Computer Science Spring 2006.
1 Lecture 14:User-Definded function I Introduction to Computer Science Spring 2006.
1 Engineering Problem Solving with C++ An Object Based Approach Chapter 2 Simple C++ Programs.
1 Chapter 3 Topics Constants of Type int and float l Evaluating Arithmetic Expressions l Implicit Type Coercion and Explicit Type Conversion l Calling.
1 CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 3: Input/Output.
CSE202: Lecture 8The Ohio State University1 Formatting Numbers for Output.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 3: Input/Output.
1 9/26/07CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
1 Lecture 7 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Chapter 3: Input/Output
Expressions and Interactivity Chapter 3. 2 The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Often.
1 TAC2000/ Protocol Engineering and Application Research Laboratory (PEARL) MATH Functions in C Language.
CHAPTER 3 INPUT/OUTPUT. In this chapter, you will:  Learn what a stream is and examine input and output streams  Explore how to read data from the standard.
Numeric Types, Expressions, and Output 1. Chapter 3 Topics Constants of Type int and float Evaluating Arithmetic Expressions Implicit Type Coercion and.
CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,
Output Formatting. Precision #include... float grade = f; cout.precision(4); cout
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 3: Input/Output.
Formatting, Casts, Special Operators and Round Off Errors 09/18/13.
STANDARD FUNCTIONS Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Output Formatting No, I don't want 6 digits…. Standard Behavior Rules for printing decimals: – No decimal point: prints as 1 – No trailing zeros:
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Formatting Output.
Chapter 3 Arithmetic Expressions, Function Calls, and Output
Input/Output Sujana Jyothi C++ Workshop Day 2. C++ I/O Basics 2 I/O - Input/Output is one of the first aspects of programming that needs to be mastered:
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
CPS120: Introduction to Computer Science Formatted I/O.
CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 31 Thanks for Lecture Slides: C How to Program by Paul Deital &
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Chapter 3: Input/Output
Formatting Output  Escape Sequences  iomanip.h Objects  setw()  setiosflags(…)  setprecision()
11/10/2016CS150 Introduction to Computer Science 1 Last Time  We covered “for” loops.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
1 Manipulators manipulators are used only in input and output statements endl, fixed, showpoint, setw, and setprecision are manipulators that can be used.
2/4/2016Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 2 Simple C++ Programs.
USING & CREATING FUNCTIONS. MODULAR PROGRAMMING  Why Modular Programming?  Improves Readability & Understandability  Improve Maintainability  Allows.
Last Time…. Operators Arithmetic Operators Assignment Operators Increment/Decrement Operators Relational Operators Logical Operators Expression Statements.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
CSCI 125 & 161 / ENGR 144 Lecture 6 Martin van Bommel.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
Chapter 3 Numeric Types, Expressions, and Output
Today Variable declaration Mathematical Operators Input and Output Lab
Topic 2 Input/Output.
Introduction to C++ (Extensions to C)
C++ Basic Input and Output (I/O)
CS102 Introduction to Computer Programming
BIL 104E Introduction to Scientific and Engineering Computing
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Chapter 3 L7.
EGR 2261 Unit 3 Input/Output Read Malik, Chapter 3.
Chapter 21 - C++ Stream Input/Output Stream Manipulators
Output Stream Formatting
Math Library and IO formatting
Copyright © 2012 Pearson Education, Inc.
Lecture 2B Expressions Richard Gesick
Input/Output Handouts: Quiz 2, Unit 3 practice sheets.
Chapter 5 Input and Output Streams
Chapter 3: Input/Output
Formatting the Output The C++ standard library supplies many manipulators: endl, setw, fixed, showpoint, setprecesion. If we want to use endl, fixed, or.
Chapter 3: Expressions and Interactivity
Chapter 4 INPUT AND OUTPUT OBJECTS
Formatted Input, Output & File Input, Output
Let’s all Repeat Together
Output Formatting Bina Ramamurthy 9/25/2019 BR.
Presentation transcript:

Output Formatting Bina Ramamurthy 4/16/2019 BR

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

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

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

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

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