Formatting, Casts, Special Operators and Round Off Errors 09/18/13.

Slides:



Advertisements
Similar presentations
CS1 Lesson 3 Expressions and Interactivity CS1 -- John Cole1.
Advertisements

C++ for Everyone by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved If Statement Continued 10/07/13.
Sizes of simple data types sizeof(char) = 1 size(short) = 2 sizeof(int) = 4 size(long) = 8 sizeof(char) = 1 size(short) = 2 sizeof(int) = 2 size(long)
Computer Science 1620 Other Data Types. Quick Review: checklist for performing user input: 1) Be sure variable is declared 2) Prompt the user for input.
Copyright © 2012 Pearson Education, Inc. Chapter 3: Expressions and Interactivity.
CS150 Introduction to Computer Science 1
1 CSC 1401 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
1 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.
1 CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 3: Expressions and Interactivity.
1 Lecture 7 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 6 – Car Payment Calculator Application: Introducing.
Lecture 17: 10/29/2002CS149D Fall CS149D Elements of Computer Science Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
Expressions and Interactivity Chapter 3. 2 The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Often.
Operaciones y Variables
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Streams Streams –Sequences of characters organized.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
Chapter Two: Fundamental Data Types
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Slides by Evan Gallagher Fundamental Data Types 09/09/13.
Computer Programming Fundamental Data Types Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons.
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
1 Number Types  Every value in Java is either: 1.a reference to an object or 2.one of the eight primitive types  eight primitive types: a.four integer.
IT253: Computer Organization
Chapter 3 COMPLETING THE BASICS Programming Fundamentals with C++1.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved For Loops October 16, 2013 Slides by Evan Gallagher.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
A First Book of ANSI C Fourth Edition Chapter 3 Processing and Interactive Input.
Copyright © 2012 Pearson Education, Inc. Chapter 3: Expressions and Interactivity.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
CSE1222: Lecture 3The Ohio State University1. Assignment Operations  The C++ assignment operator is: =  Examples: x = 3 * 5; y = x – 7; y = y + 4; Do.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 6 – Car Payment Calculator Application: Introducing.
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
C++ Programming: Basic Elements of C++.
Mathematical Calculations in Java Mrs. G. Chapman.
Output Formatting No, I don't want 6 digits…. Standard Behavior Rules for printing decimals: – No decimal point: prints as 1 – No trailing zeros:
Chapter 2 Overview of C++. 2 Overview  2.1 Language Elements  2.2 Reserved Words & Identifiers  2.3 Data Types & Declarations  2.4 Input/Output 
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Formatting Output.
THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15.
Chapter 3: Assignment, Formatting, and Interactive Input.
Loops Wrap Up 10/21/13. Topics *Sentinel Loops *Nested Loops *Random Numbers.
C++ for Engineers and Scientists Second Edition Chapter 3 Assignment, Formatting, and Interactive Input.
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
Mathematical Calculations in Java Mrs. C. Furman.
Expressions and Interactivity. 3.1 The cin Object.
COMP Primitive and Class Types Yi Hong May 14, 2015.
This will all add up in the end. Assignment operator =Simple Assignment operator Arithmetic Operators +Additive operator – Subtraction operator * Multiplication.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Chapter Two: Fundamental Data Types Slides by Evan Gallagher.
1 09/27/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Programming Fundamentals with C++1 Chapter 3 COMPLETING THE BASICS.
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
1 Manipulators manipulators are used only in input and output statements endl, fixed, showpoint, setw, and setprecision are manipulators that can be used.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
2/4/2016Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 2 Simple C++ Programs.
Chapter 05 (Part II) Control Statements: Part II.
Math Operators and Output Formatting. Incrementing and Decrementing StatementEquivalent Counter++;Counter = Counter + 1; ++Counter;Counter = Counter +
Programming Fundamentals. Summary of Previous Lectures Phases of C++ Environment Data Types cin and cout.
Arithmetic, Functions and Input 9/16/13. Arithmetic Operators C++ has the same arithmetic operators as a calculator: * for multiplication: a * b – Not.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
CSCI 125 & 161 / ENGR 144 Lecture 6 Martin van Bommel.
Chapter Two: Fundamental Data Types
Fundamental Data Types
Chapter 4 – Fundamental Data Types
Fundamental Data Types
Wednesday 09/23/13.
Formatting the Output The C++ standard library supplies many manipulators: endl, setw, fixed, showpoint, setprecesion. If we want to use endl, fixed, or.
CS150 Introduction to Computer Science 1
Let’s all Repeat Together
C++ Programming Basics
C++ for Engineers and Scientists Second Edition
Presentation transcript:

Formatting, Casts, Special Operators and Round Off Errors 09/18/13

Formatted Output C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Formatted Output Sometimes we want to control the number of decimal places in an output. A manipulator is something that is sent to cout to specify how values should be formatted. To use manipulators, you must include the iomanip header in your program: #include and using namespace std; is also needed C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Formatted Output Which do you think the user prefers to see on her gas bill? Price per liter: $1.22 or Price per liter: $ C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Formatted Output C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Formatted Output You can combine manipulators and values to be displayed into a single statement: price_per_liter = ; cout << fixed << setprecision(2) << "Price per liter: $" << price_per_liter << endl; This code produces this output: Price per liter: $1.22 C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Formatted Output You use the setw manipulator to set the width of the next output field. The width is the total number of characters used for showing the value, including digits, the decimal point, and spaces. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Formatted Output If you want columns of certain widths, use the setw manipulator. For example, if you want a number to be printed, right justified, in a column that is eight characters wide, you use << setw(8) C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Formatted Output This code: price_per_ounce_1 = ; price_per_ounce_2 = 117.2; price_per_ounce_3 = ; cout << setprecision(2); cout << setw(8) << price_per_ounce_1; cout << setw(8) << price_per_ounce_2; cout << setw(8) << price_per_ounce_3; cout << " " << endl; produces this output: C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Formatted Output There is a notable difference between the setprecision and setw manipulators. Once you set the precision, that width is used for all floating-point numbers until the next time you set the precision. But setw affects only the next value. Subsequent values are formatted without added spaces. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Write a program Write a program to input the dimensions of a piece of paper in inches. Convert the measures into mm. Give the user the metric measurements to the nearest ten of and inch. (There are 24.5 mm in an inch.)

C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Casts, Round Off Errors, More Operators 09/23/13

C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Announcements Program 3 due Wednesday Exam I Next Monday, 09/25/13

Casts Occasionally, you need to store a value into a variable of a different type. Whenever there is the risk of information loss, the compiler generally issues a warning. It’s not a compilation error to lose information. But it may not be what a programmer intended. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Casts For example, if you store a double value into an int variable, information is lost in two ways: The fractional part will be lost. int n = ; // NO 1 is stored (the decimal part is truncated) The magnitude may be too large. int n = 1.0E100; // NO is not likely to work, because 1.0E100 is larger than the largest representable integer. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Casts C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved int a = 15; double b = double(a); int s = int(1.8);

Casts Participation C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Write a program to find the average number of cars per household. Input the number of cars and the number of households as ints. Output an average as a double, e.g. 2.5.

Common Error – Roundoff Errors This program produces the wrong output: //internalRd.cpp #include using namespace std; int main() { double price = 4.35; int cents = 100 * price; // Should be 100 * 4.35 = 435 cout << cents << endl; // Prints 434! return 0; } Why? C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Common Error – Roundoff Errors In the processor hardware, numbers are represented in the binary number system, not in decimal. In the binary system, there is no exact representation for 4.35, just as there is no exact representation for ⅓ in the decimal system. The representation used by the computer is just a little less than 4.35, so 100 times that value is just a little less than 435. The remedy is to add 0.5 in order to round to the nearest integer: int cents = 100 * price + 0.5; C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Increment and Decrement Changing a variable by adding or subtracting 1 is so common that there is a special shorthand for these: The increment and decrement operators. counter++; // add 1 to counter counter--; // subtract 1 from counter C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Combining Assignment and Arithmetic In C++, you can combine arithmetic and assignments. For example, the statement total += cans * CAN_VOLUME; is a shortcut for total = total + cans * CAN_VOLUME; Similarly, total *= 2; is another way of writing total = total * 2; Many programmers prefer using this form of coding. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Spaces in Expressions It is easier to read x1 = (-b + sqrt(b * b - 4 * a * c)) / (2 * a); than x1=(-b+sqrt(b*b-4*a*c))/(2*a); Itreallyiseasiertoreadwithspaces! So always use spaces around all operators: + - * / % = C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Spaces in Expressions However, don’t put a space after a unary minus: that’s a – used to negate a single quantity like this: -b That way, it can be easily distinguished from a binary minus, as in a - b It is customary not to put a space after a function name. Write sqrt(x) not sqrt (x) C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Questions What is the purpose of a cast? What is a short cut way to add one to a variable? What is meant by a round off error?