1 Lecture 7 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.

Slides:



Advertisements
Similar presentations
Dale/Weems/Headington
Advertisements

More Review, with Some New Chapter 3. Review C++ simple data types – Integral and float – Arithmetic operators Expressions and expression evaluation –
Numeric Types, Expressions, and Output ROBERT REAVES.
1 Lecture 6 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
1 Lecture-4 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
1 Lecture 19 Chapter 4 Program Input and the Software Design Process Dale/Weems/Headington.
1 Chapter 3 Arithmetic Expressions. 2 Chapter 3 Topics l Overview of Java Data Types l Numeric Data Types l Declarations for Numeric Expressions l Simple.
Lecture 18:Topic: I/O Formatting and Files Dale/Weems/Headington
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
1 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.
1 Chapter 3 Topics Constants of Type int and float l Evaluating Arithmetic Expressions l Implicit Type Coercion and Explicit Type Conversion l Calling.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
Chapter 3: Input/Output
Basic Elements of C++ Chapter 2.
Input and Output in Console Mode UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Expressions and Interactivity Chapter 3. 2 The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Often.
CSI 2372: Advanced Programming Concepts with C++
Chapter 3 COMPLETING THE BASICS Programming Fundamentals with C++1.
1 Numeric Types, Expressions, and Output. 2 Chapter 3 Topics  Constants of Type int and float  Evaluating Arithmetic Expressions  Declaration for Numeric.
Numeric Types, Expressions, and Output 1. Chapter 3 Topics Constants of Type int and float Evaluating Arithmetic Expressions Implicit Type Coercion and.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems.
1 Chapter 3 Numeric Types, Expressions, and Output CS185/09 - Introduction to Programming Caldwell College.
1 Chapter 4 Program Input and the Software Design Process Dale/Weems/Headington.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Formatting, Casts, Special Operators and Round Off Errors 09/18/13.
1 C++ Syntax and Semantics, and the Program Development Process.
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
C++ Programming: Basic Elements of C++.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
Output Formatting No, I don't want 6 digits…. Standard Behavior Rules for printing decimals: – No decimal point: prints as 1 – No trailing zeros:
1 Chapter 3 Numeric Types, Expressions, and Output.
I/O and Data Formatting Introduction to Class Concepts INFSY 307 Spring 2003 Lecture 3.
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.
Chapter 3: Assignment, Formatting, and Interactive Input.
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.
1 Chapter 4 Program Input and the Software Design Process Dale/Weems/Headington.
Formatting Output  Escape Sequences  iomanip.h Objects  setw()  setiosflags(…)  setprecision()
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Programming Fundamentals with C++1 Chapter 3 COMPLETING THE BASICS.
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
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.
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
C++ Data Types Check sample values of? ‘4’
Chapter 3 Numeric Types, Expressions, and Output.
1 Chapter 4 Program Input and the Software Design Process.
Chapter 3 The New Math. C++ Data Types simple integral charshort intlong bool floating float double Long double enum address pointer reference structured.
Last Time…. Operators Arithmetic Operators Assignment Operators Increment/Decrement Operators Relational Operators Logical Operators Expression Statements.
Chapter 05 (Part II) Control Statements: Part II.
CS221 C++ Basics. C++ Data Types structured array struct union class address pointer reference simple integral char short int long bool floating float.
1 A Simple “Hello World” Example #include // input-output library using namespace std; int main() // function main { cout
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
CSCI 125 & 161 / ENGR 144 Lecture 6 Martin van Bommel.
Chapter 3 Numeric Types, Expressions, and Output
Introduction to C++ (Extensions to C)
Chapter Topics The Basics of a C++ Program Data Types
Arithmetic Expressions Function Calls Output
Basic Elements of C++.
Basic Elements of C++ Chapter 2.
Basic Input and Output C++ programs can read and write information using streams A simple input stream accepts typed data from a keyboard A simple 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
C++ for Engineers and Scientists Second Edition
Chapter 3 The New Math.
Programming Fundamental-1
Presentation transcript:

1 Lecture 7 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington

2 Chapter 3 Topics Constants of Type int and float l Evaluating Arithmetic Expressions l Implicit Type Coercion and Explicit Type Conversion l Calling a Value-Returning Function l Using Function Arguments l Using C++ Library Functions in Expressions l Calling a Void Function l C++ Manipulators to Format Output String Operations length, find, substr

3 Revision of Lecture 6 l Value=Expression; When does automatic type coercion occur? l How do we explicitly convert 4.58 to integer? l F(x,y) = 5x-3y-2; What parameters are passed and what value is returned? l Can a subprogram (function except main) call another function?

4 Revision of Lecture 6 l If yes, why should we avoid it? l What are the rules for top down C++ program design? l void my_salary(tax); n How do we call this function in our program: l float tax(base_salary); n How do we call this function? l Why do we use Output manipulators?

5 Output Statements SYNTAX (revised) cout << ExpressionOrManipulator << ExpressionOrManipulator... ;

6 Using Manipulators Fixed and Showpoint l use the following statement to specify that (for output sent to the cout stream) decimal format (not scientific notation) be used, and that a decimal point be included (even for floating values with 0 as fractional part) cout << fixed << showpoint ;

7 setprecision(n) l requires #include and appears in an expression using insertion operator (<<) l if fixed has already been specified, argument n determines the number of places displayed after the decimal point for floating point values l remains in effect until explicitly changed by another call to setprecision

8 What is exact output? #include // for setw( ) and setprecision( ) #include using namespace std; int main ( ) { float myNumber = ; cout << fixed << showpoint ; // use decimal format // print decimal points cout << “Number is ” << setprecision ( 3 ) << myNumber << endl ; return 0 ; }

9 OUTPUT Number is value is rounded if necessary to be displayed with exactly 3 places after the decimal point

10 Manipulator setw l “set width” lets us control how many character positions the next data item should occupy when it is output l setw is only for formatting numbers and strings, not char type data

11 setw(n) l requires #include and appears in an expression using insertion operator (<<) l argument n is called the fieldwidth specification, and determines the number of character positions in which to display a right-justified number or string (not char data). The number of positions used is expanded if n is too narrow l “set width” affects only the very next item displayed, and is useful to align columns of output

12 What is exact output? #include // for setw( ) #include using namespace std; int main ( ) { int myNumber = 123 ; int yourNumber = 5 ; cout << setw ( 10 ) << “Mine” << setw ( 10 ) << “Yours” << endl; << setw ( 10 ) << myNumber << setw ( 10 ) << yourNumber << endl ; return 0 ; }

13 OUTPUT Mine Yours each is displayed right-justified and each is located in a total of 10 positions position

14 What is exact output? #include // for setw( ) and setprecision( ) #include using namespace std; int main ( ) { float myNumber = ; float yourNumber = ; cout << fixed << showpoint ; // use decimal format // print decimal points cout << “Numbers are: ” << setprecision ( 4 ) << endl << setw ( 10 ) << myNumber << endl << setw ( 10 ) << yourNumber << endl ; return 0 ; }

15 OUTPUT Numbers are: each is displayed right-justified and rounded if necessary and each is located in a total of 10 positions with 4 places after the decimal point

16 float x = ; float y = ; cout << fixed << showpoint ; OUTPUT cout << setprecision ( 2 ) << setw ( 10 ) << x << endl ’’’’ << setw ( 10 ) << y << endl ; ’’’’’’ 4.83 cout << setprecision ( 1 ) << setw ( 10 ) << x << endl ’’’’’ << setw ( 10 ) << y << endl ; ’’’’’’’ 4.8 cout << setprecision ( 5 ) << setw ( 7 ) << x << endl << setw ( 7 ) << y << endl ; More Examples x y 4.827

17 HEADER MANIPULATOR ARGUMENT EFFECT FILE TYPE showpoint none displays decimal point fixed none suppresses scientific notation setprecision(n) int sets precision to n digits setw(n) int sets fieldwidth to n positions endl none terminates output line

18 length Function function length returns an unsigned integer value that equals the number of characters currently in the string function size returns the same value as function length you must use dot notation in the call to function length or size

19 find Function function find returns an unsigned integer value that is the beginning position for the first occurrence of a particular substring within the string the substring argument can be a string constant, a string expression, or a char value if the substring was not found, function find returns the special value string::npos

20 substr Function function substr returns a particular substring of a string l the first argument is an unsigned integer that specifies a starting position within the string l the second argument is an unsigned integer that specifies the length of the desired substring l positions of characters within a string are numbered starting from 0, not from 1

21 What is exact output? #include #include // for functions length, find, substr using namespace std; int main ( ) { string stateName = “Mississippi” ; cout << stateName.length( ) << endl; cout << stateName.find(“is”) << endl; cout << stateName.substr( 0, 4 ) << endl; cout << stateName.substr( 4, 2 ) << endl; cout << stateName.substr( 9, 5 ) << endl; return 0 ; }

22 What is exact output? #include #include // for functions length, find, substr using namespace std; int main ( ) { string stateName = “Mississippi” ; cout << stateName.length( ) << endl; // value 11 cout << stateName.find(“is”) << endl;// value 1 cout << stateName.substr( 0, 4 ) << endl;// value “Miss” cout << stateName.substr( 4, 2 ) << endl;// value “is” cout << stateName.substr( 9, 5 ) << endl;// value “pi” return 0 ; }

23 Map Measurement Case Study You want a program to determine walking distances between 4 sights in the city. Your city map legend says one inch on the map equals 1/4 mile in the city. You use the measured distances between 4 sights on the map. Display the walking distances (rounded to the nearest tenth) between each of the 4 sights.

24 // *************************************************** // Walk program // This program computes the mileage (rounded to nearest // tenth of mile) for each of 4 distances, given map // measurements on map with scale of 1 in = 0.25 mile // *************************************************** #include // for cout, endl #include // For setprecision using namespace std; float RoundToNearestTenth( float ); // declare function const float SCALE = 0.25; // Map scale (mi. per inch) C++ Program

25 C++ Code Continued const float DISTANCE1 = 1.5; // First map distance const float DISTANCE2 = 2.3; // Second map distance const float DISTANCE3 = 5.9; // Third map distance const float DISTANCE4 = 4.0; // Fourth map distance int main( ) { float totMiles; // Total of rounded miles float miles;// One rounded mileage cout << fixed << showpoint // Set output format << setprecision(1); totMiles = 0.0;// Initialize total miles

26 // Compute miles for each distance on map miles = RoundToNearestTenth( DISTANCE1 * SCALE ); cout << DISTANCE1 << “ inches on map is “ << miles << “ miles in city.” << endl; totMiles = totMiles + miles; miles = RoundToNearestTenth( DISTANCE2 * SCALE ); cout << DISTANCE2 << “ inches on map is “ << miles << “ miles in city.” << endl; totMiles = totMiles + miles;

27 // Compute miles for other distances on map miles = RoundToNearestTenth( DISTANCE3 * SCALE ); cout << DISTANCE3 << “ inches on map is “ << miles << “ miles in city.” << endl; totMiles = totMiles + miles; miles = RoundToNearestTenth( DISTANCE4 * SCALE ); cout << DISTANCE4 << “ inches on map is “ << miles << “ miles in city.” << endl; totMiles = totMiles + miles;

28 cout << endl << “Total walking mileage is “ << totMiles << “ miles.” << endl; return 0 ;// Successful completion } // *************************************************** float RoundToNearestTenth ( /* in */ float floatValue) // Function returns floatValue rounded to nearest tenth. { return float(int(floatValue * )) / 10.0; }