Numeric Types, Expressions, and Output ROBERT REAVES.

Slides:



Advertisements
Similar presentations
CPS120: Introduction to Computer Science INPUT/OUTPUT.
Advertisements

Dale/Weems/Headington
More Review, with Some New Chapter 3. Review C++ simple data types – Integral and float – Arithmetic operators Expressions and expression evaluation –
CS1 Lesson 3 Expressions and Interactivity CS1 -- John Cole1.
1 Lecture 6 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Copyright © 2012 Pearson Education, Inc. Chapter 3: Expressions and Interactivity.
1 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.
An Introduction to Programming with C++ Fifth Edition Chapter 4 Chapter 4: Variables, Constants, and 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.
An Introduction to Programming with C++ Fifth Edition Chapter 4 Chapter 4: Variables, Constants, and Arithmetic Operators.
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.
Data Types, Expressions and Functions (part I)
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.
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.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Expressions and Interactivity.
Copyright © 2012 Pearson Education, Inc. Chapter 3: Expressions and Interactivity.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Computer Programming TCP1224 Chapter 4 Variables, Constants, and Arithmetic Operators.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
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.
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.
I/O and Data Formatting Introduction to Class Concepts INFSY 307 Spring 2003 Lecture 3.
Chapter 3 Arithmetic Expressions, Function Calls, and Output
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Chapter 3: Assignment, Formatting, and Interactive Input.
Simple Data Types Built-In and User Defined Chapter 10.
C++ for Engineers and Scientists Second Edition Chapter 3 Assignment, Formatting, and Interactive Input.
CPS120: Introduction to Computer Science Formatted I/O.
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.
Expressions and Interactivity. 3.1 The cin Object.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Operating System Using setw and setprecision functions Using setiosflags function Using cin function Programming 1 DCT
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.
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.
Last Time…. Operators Arithmetic Operators Assignment Operators Increment/Decrement Operators Relational Operators Logical Operators Expression Statements.
Chapter Expressions and Interactivity 3. The cin Object 3.1.
Chapter 4: Introduction To C++ (Part 3). The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Information.
Chapter 4 Chapter 4: Variables, Constants, and Arithmetic Operators.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3: Expressions and Interactivity.
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.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
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.
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
An Introduction to Programming with C++ Fifth Edition
C++ for Engineers and Scientists Second Edition
Chapter 3 The New Math.
Programming Fundamental-1
Presentation transcript:

Numeric Types, Expressions, and Output ROBERT REAVES

Compound Arithmetic Expressions  Arithmetic expressions can be made up of many constants, variables, operators, and parentheses.  Precedence Level (highest  lowest)  Unary +, Unary -  *, /, %  +, -  (…) are always evaluated first.  Ex.  10 / 2 * 3 = 15  10 % 3 – 4 / 2 = -1  5.0 * 2.0 / 4.0 * 2.0 = 5.0  5.0 * 2.0 / (4.0 * 2.0) =1.25

Type Coercion and Type Casting  Type Coercion is the implicit (automatic) conversion of a value from one data type to another.  Ex.  float someFloat = 12;  Computer inserts extra machine language instructions to convert to  Type Casting is the explicit conversion of a value from one data type to another, also called type conversion.  A C++ cast operation consists of a data type name and then, within parentheses, the expression to be converted.  Ex.  float someFloat = float(3 * someInt * 2);

Arithmetic Expressions  Possible to mix data types within an expression, this is called mixed type expression or mixed mode expression.  Whenever an integer value and floating-point value are joined by an operator, implicit type coercion occurs:  Integer value is temporarily coerced to a floating-point value  Operation is performed  Result is a floating-point value

What is a value-returning function?  A function that returns a single value to its caller and is invoked from within an expression.

Void Functions  void myFunc(...) {. }  Notice how it begins with the word void instead of a data type like int or float.  Void Function( (procedure) is a function that does not return a function value to its caller and is invoked as a separate statement.  What do you mean by a separate statement?

Formatting Output  What this means is to control how output appears visually on the screen or on a printout.  The C++ standard library supplies many manipulators, but we will look at only five of them:  endl  setw  fixed  showpoint  setprecision

Header Files  endl, fixed, and showpoint are including inside the iostream header file to perform I/O.  #include  setw and setprecision are inside the iomanip header file.  #include

Setw  setw means “set width”, it lets us control how many character positions the next data item should occupy when it is output.  Cout << setw(4) << “Hi” << endl;  Output (_ means blank) _ _ Hi  If number of characters to output is less then that amount of characters in the output the field will automatically expand to fit the output.

Fixed  What happens when we use floating-point values and setw?  Take the value 4.85, it takes four output positions to print this to the screen.  Another problem with float-point values is that large values are printed in Scientific notation.  may print as e+08 on some systems.  Can use fixed to force all subsequent floating-point output to appear in decimal form instead of scientific notation.  cout << fixed << 3.8 * x;  However, if the number is whole number it will not print as a floating- point number.

Showpoint  showpoint forces decimal points to be displayed in subsequent floating-point output, even for whole numbers.  cout << showpoint << someFloat;  What if we want just two decimal places?

setprecision  setprecision specifies the desired number of decimal places.  REMAINS IN EFFECT for all subsequent output.  Should use setprecision and the fixed together to get correct results.

Additional String Operations .at()  Included instead to the cctype header file;  #include  toupper()  tolower()

At function  At function allows for individual character access in a string.  str1.at(pos);  pos being the character position you want to access.  This returns the character at that location within the string.  string str1 = “Robert”;  char letter = str1.at(0);  The variable letter now is holding the character ‘R’;

toupper  toupper(ch) returns the uppercase equivalent of ch, if ch is a lowercase character; ch, otherwise.  Ex.  toupper(‘r’) returns ‘R’  toupper(‘R’) returns ‘R’

tolower  tolower(ch) returns the lowercase equivalent of ch, if ch is an uppercase letter; ch, otherwise.  Ex.  tolower(‘R’) returns ‘r’  tolower(‘r’) returns ‘r’