Lecture 17: 10/29/2002CS149D Fall 20021 CS149D Elements of Computer Science Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.

Slides:



Advertisements
Similar presentations
Chapter 3. Expressions and Interactivity CSC125 Introduction to C++
Advertisements

CPS120: Introduction to Computer Science INPUT/OUTPUT.
Numeric Types, Expressions, and Output ROBERT REAVES.
True or false A variable of type char can hold the value 301. ( F )
Copyright © 2012 Pearson Education, Inc. Chapter 3: Expressions and Interactivity.
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
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.
C++ Numerical Data Input/Output Programming. COMP 102 Prog Fundamentals I:C++ Numerical Data, Input/Output /Slide 2 Rules for Division l C++ treats integers.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
Basic Elements of C++ Chapter 2.
Unformatted and Formatted I/O Operations. 2 Unformatted Input/output is the most basic form of input/output. Unformatted I/O transfers the internal binary.
Input and Output in Console Mode UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
Expressions and Interactivity Chapter 3. 2 The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Often.
Chapter 3 COMPLETING THE BASICS Programming Fundamentals with C++1.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems.
Copyright © 2012 Pearson Education, Inc. Chapter 3: Expressions and Interactivity.
Formatting, Casts, Special Operators and Round Off Errors 09/18/13.
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
C++ Programming: Basic Elements of C++.
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.
1.  By the end of this section you should: ◦ Understand what the variables are and why they are used. ◦ Use C++ built in data types to create program.
THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15.
I/O and Data Formatting Introduction to Class Concepts INFSY 307 Spring 2003 Lecture 3.
Chapter 3 Arithmetic Expressions, Function Calls, and Output
Lecture 13: 10/10/2002CS149D Fall CS149D Elements of Computer Science Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
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.
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.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.
Expressions and Interactivity. 3.1 The cin Object.
1 09/27/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
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.
Chapter 05 (Part II) Control Statements: Part II.
C Building Block Chapter 2. Variables A variable is a space in the computer’s memory set aside for a certain kind of data and given a name for easy reference.
Chapter Expressions and Interactivity 3. The cin Object 3.1.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
arithmetic operator & cin I.Mona Alshehri The output formatting functions setw(width) setw(n) - output the value of the next expression in n columns.
Chapter 4 Strings and Screen I/O. Objectives Define strings and literals. Explain classes and objects. Use the string class to store strings. Perform.
CSCI 125 & 161 / ENGR 144 Lecture 6 Martin van Bommel.
Introduction to C++ (Extensions to C)
Chapter Topics The Basics of a C++ Program Data Types
What Actions Do We Have Part 1
BIL 104E Introduction to Scientific and Engineering Computing
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Basic Elements of C++.
CS149D Elements of Computer Science
Basic Elements of C++ Chapter 2.
Variables Kingdom of Saudi Arabia
Chapter 3: Input/Output
Introduction to cout / cin
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
CS150 Introduction to Computer Science 1
Chapter 4 INPUT AND OUTPUT OBJECTS
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
CS149D Elements of Computer Science
C++ Programming Basics
Introduction to cout / cin
C++ for Engineers and Scientists Second Edition
Presentation transcript:

Lecture 17: 10/29/2002CS149D Fall CS149D Elements of Computer Science Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture 17: 10/29/2002

CS149D Fall Outline Priority of Operators Revisit Increment and decrement operators Standard Input and Output

Lecture 17: 10/29/2002CS149D Fall Priority of Operators Precedence of arithmetic operators PrecedenceOperatorAssociativity (grouping order) 1( )innermost first 2unary operators + - castright to left 3binary operators * / %left to right 4binary operators + -left to right Expression x-y+z is evaluated as (x-y)+z(left to right associativity) Expression a*b+b/c*dEvaluated as if written (a*b) + ((b/c)*d) Expression 5.0 * 2.0 / 4.0 * 2.0 yields 5.0 (Why?) Expression 10 % 3 – 4 /2 yields –1 (Why?)

Lecture 17: 10/29/2002CS149D Fall Algebraic Expressions into C++ Expressions f = (x 3 – 2x 2 + x – 6.3)/(x x-3.14) A C++ expression Would be f = (x*x*x - 2*x*x + x – 6.3) / (x*x *x – 3.14); Or can divide into computing numerator and denominator float numerator, denominator, f; numerator = x*x*x - 2*x*x + x – 6.3; denominator = x*x *x – 3.14; f = numerator/denominator;

Lecture 17: 10/29/2002CS149D Fall Revisit Increment and Decrement Operators int y = 5; x = y++ * 3;//postfix position Is equivalent to x = y *3; y = y +1x  15y  6 x = ++y *3;//prefix position Is equivalent toy = y+1 x = y *3x  18 y  6

Lecture 17: 10/29/2002CS149D Fall Abbreviated Assignment Operators x = x +3;is equivalent to x += 3; identifier = identifier operator expression can be written as identifier operator= expression a = b += c +d; Assignment operators have least priority and group right to left Then previous expression evaluated as a = (b += (c+d)); Or b = b + (c+d); a = b; The previous multiple assignment use is not recommended but is available as part of the C++ language

Lecture 17: 10/29/2002CS149D Fall Standard Input and Output Must include Header file iostream.h contains prototype definitions of input/output functions cout << “angle = “ << angle << “radians” << endl; Escape characters Backslash (\) is an escape character \” used to print a “ \\ used to print a \ \n used to represent a new line, equivalent to endl functionality \t used to print a horizontal tab cout << “\”The End.\”’ << “\n” ;output is “The End.”

Lecture 17: 10/29/2002CS149D Fall Formatted Output To use must include Field Width Format manipulator Set field width (number of character positions that a number will occupy on the screen) setw(size) cout << setw(8) << x;// print value of x in field of 8 spaces Field width increased if necessary to print the value If field width specifies more positions than needed, value is right justified Only specifies field width for the next item in cout. After item is displayed the field size reverts to default value of 0 cout << setw(10) <<x << y; //only value of x is printed in 10 spaces, value of y will be printed in enough space to display it

Lecture 17: 10/29/2002CS149D Fall Formatted Output Precision manipulator Specify precision of value to be displayed Precision is number of digits after decimal point cout << setprecision(2) << setw(8) << x; Print value of x with 2 digits after decimal point, using a total field width of 8 spaces Decimal portion is rounded to the specified precision printed as with three blanks to its left (Why?) setprecision manipulator is applied to all subsequent output

Lecture 17: 10/29/2002CS149D Fall Keyboard Input int x; float y; cin >> y >> x; User input x assigned 5 y assigned 15.5 User input x assigned 5 y assigned 15.5 User input x assigned 15 y assigned 5.0