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

Slides:



Advertisements
Similar presentations
1 C++ Syntax and Semantics The Development Process.
Advertisements

Dale/Weems/Headington
Numeric Types, Expressions, and Output ROBERT REAVES.
Dale/Weems/Headington
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.
© 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5/e Starting Out with C++: Early Objects 5 th Edition Chapter 2 Introduction.
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.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Expressions and Operators Program Style.
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.
Data types and variables
1 Chapter 3 Topics Constants of Type int and float l Evaluating Arithmetic Expressions l Implicit Type Coercion and Explicit Type Conversion l Calling.
Chapter 2 Data Types, Declarations, and Displays
Chapter 2: Introduction to C++.
1 Lecture 7 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Data Types, Expressions and Functions (part I)
Basic Elements of C++ Chapter 2.
Operaciones y Variables
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 2 Introduction to C++
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.
Chapter 2: Using Data.
1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Lecture no 3 Control statements.
1 C++ Syntax and Semantics, and the Program Development Process.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems.
C++ Programming: Basic Elements of C++.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems.
1 Chapter 3 Numeric Types, Expressions, and Output.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Chapter 3 Arithmetic Expressions, Function Calls, and Output
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Sixth.
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.
Data Types Declarations Expressions Data storage C++ Basics.
CHAPTER 2 C++ SYNTAX & SEMANTICS #include using namespace std; int main() { cout
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
1 What is a Named Constant? A named constant is a location in memory that we can refer to by an identifier, and in which a data value that cannot be changed.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Programming in C++
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.
Chapter 3 The New Math. C++ Data Types simple integral charshort intlong bool floating float double Long double enum address pointer reference structured.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
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 Lecture Three I/O Formatting and Arithmetic Dr. Sherif Mohamed Tawfik.
Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016.
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
Chapter Topics The Basics of a C++ Program Data Types
Basic Elements of C++.
Basic Elements of C++ Chapter 2.
Chapter 2: Introduction to C++.
Engineering Problem Solving with C++ An Object Based Approach
Chapter 3 The New Math.
Presentation transcript:

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

2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long double

3 C++ Simple Data Types simple types integralfloating char short int long bool enum float double long double unsigned

4 Standard Data Types in C++ Integral Types –represent whole numbers and their negatives –declared as int, short, or long Floating Types –represent real numbers with a decimal point –declared as float, or double Character Type –represents single characters –declared as char

5 Samples of C++ Data Values int sample values float sample values E-3-95E E2 char sample values ‘ B ’ ‘ d ’ ‘ 4 ’‘ ? ’ ‘ * ’

6 More About Floating Point Values floating point numbers have an integer part and a fractional part, with a decimal point in between. Either the integer part or the fractional part, but not both, may be missing EXAMPLES alternatively, floating point values can have an exponent, as in scientific notation--the number preceding the letter E doesn’t need to include a decimal point EXAMPLES 1.84E1 5E2 8E E3

7 Division Operator the result of the division operator depends on the type of its operands if one or both operands has a floating point type, the result is a floating point type. Otherwise, the result is an integer type Examples 11 / 4 has value / 4.0 has value / 4.0 has value 2.75

8 Modulus Operator the modulus operator % can only be used with integer type operands and always has an integer type result its result is the integer type remainder of an integer division EXAMPLE 11 % 4 has value 3 because ) 4 11 R = ?

9 More C++ Operators 8 int age; age = 8; age = age + 1; age 9

10 Increment Operator 8 int age; age = 8; age++; age 9

11 Decrement Operator 100 int dogs; dogs = 100; dogs--; dogs 99 dogs

12 What is an Expression in C++? An expression is a valid arrangement of variables, constants, and operators. in C++ each expression can be evaluated to compute a value of a given type the value of the expression 9.3 * 4.5 is 41.85

13 Operators can be binaryinvolving 2 operands unaryinvolving 1 operand- 3 ternary involving 3 operands later

14 Some C++ Operators Precedence OperatorDescription Higher ( )Function call +Positive - Negative *Multiplication / Division % Modulus (remainder) +Addition - Subtraction Lower = Assignment

15 Precedence higher Precedence determines which operator is applied first in an expression having several operators

16 Associativity left to right Associativity means that in an expression having 2 operators with the same priority, the left operator is applied first in C++ the binary operators *, /, %, +, - are all left associative expression means ( )

17 7 * % 3 * means (7 * 10) - 5 % 3 * % 3 * (5 % 3) * * ( 2 * 4 ) ( ) Evaluate the Expression

18 Parentheses parentheses can be used to change the usual order parts in ( ) are evaluated first evaluate (7 * (10 - 5) % 3) * ( 7 * 5 % 3 ) * ( 35 % 3 ) * *

19 Mileage Program /* This program computes miles per gallon given four amounts for gallons used, and starting and ending mileage. Constants: The gallon amounts for four fillups. The starting mileage. The ending mileage. Output (screen) The calculated miles per gallon */ #include using namespace std;

20 C++ Code Continued const float AMT1 = 11.7 ; // Number of gallons for fillup 1 const float AMT2 = 14.3 ; // Number of gallons for fillup 2 const float AMT3 = 12.2 ; // Number of gallons for fillup 3 const float AMT4 = 8.5 ; // Number of gallons for fillup 4 const float START_MILES = ; // Starting mileage const float END_MILES = ; // Ending mileage int main( ) { float mpg ; // Computed miles per gallon mpg = (END_MILES - START_MILES) / (AMT1 + AMT2 + AMT3 + AMT4) ;

21 Main returns an int value to the operating system cout << “For the gallon amounts “ << endl ; cout << AMT1 << ‘ ‘ << AMT2 << ‘ ‘ << AMT3 << ‘ ‘ << AMT4 << endl ; cout << “and a starting mileage of “ << START_MILES << endl ; cout << “and an ending mileage of “ << END_MILES << endl ; cout << “the mileage per gallon is “ << mpg << endl ; return 0; }

22 Variable = Expression first, Expression on right is evaluated then the resulting value is stored in the memory location of Variable on left NOTE: An automatic type coercion occurs after evaluation but before the value is stored if the types differ for Expression and Variable Assignment Operator Syntax

23 What value is stored? float a; float b; a = 8.5; b = 9.37; a = b; a b a b ? ?

24 What is stored? ? float someFloat; someFloat someFloat = 12; // causes implicit type conversion someFloat 12.0

25 What is stored? ? int someInt; someInt someInt = 4.8; // causes implicit type conversion someInt 4

26 Type Casting is Explicit Conversion of Type int(4.8) has value4 float(5)has value5.0 float(7/4)has value1.0 float(7) / float(4)has value1.75

27 Some Expressions int age; EXAMPLEVALUE age = age / / float ( 4 / 8 )0.0 float ( 4 ) / 80.5 cout << “How old are you?” cout cin >> agecin cout << agecout

28 What values are stored? float loCost; float hiCost; loCost = ; hiCost = ; loCost = float (int (loCost * ) ) / 100.0; hiCost = float (int (hiCost * ) ) / 100.0;

29 Values were rounded to 2 decimal places hiCost loCost

30 Insertion Operator ( << ) the insertion operator << takes 2 operands the left operand is a stream expression, such as cout the right operand is an expression of simple type, or a string, or a manipulator

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

32 Output Statements SYNTAX These examples yield the same output. cout << “The answer is “ ; cout << 3 * 4 ; cout << “The answer is “ << 3 * 4 ; cout << Expression << Expression... ;

33 Using Manipulators Fixed and Showpoint 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 ;

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

35 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 ; }

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