Last Time…. Operators Arithmetic Operators Assignment Operators Increment/Decrement Operators Relational Operators Logical Operators Expression Statements.

Slides:



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

Numeric Types, Expressions, and Output ROBERT REAVES.
CS1 Lesson 3 Expressions and Interactivity CS1 -- John Cole1.
1 9/13/06CS150 Introduction to Computer Science 1 Type Casting.
Copyright © 2012 Pearson Education, Inc. Chapter 3: Expressions and Interactivity.
1 9/17/07CS150 Introduction to Computer Science 1 Type Casting.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
CS150 Introduction to Computer Science 1
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 3- 1.
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.
An Introduction to Programming with C++ Fifth Edition Chapter 4 Chapter 4: Variables, Constants, and Arithmetic Operators.
Chapter 3 Assignment and Interactive Input
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 3: Expressions and Interactivity.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 3: Input/Output.
Basic Elements of C++ Chapter 2.
CHAPTER 2 BASIC ELEMENTS OF C++. In this chapter, you will:  Become familiar with the basic components of a C++ program, including functions, special.
Expressions and Interactivity Chapter 3. 2 The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Often.
Variables, Data Types and Constants Yared Semu Addis Ababa Institute of Technology Mar 31, 2012.
© Janice Regan, CMPT 128, Sept CMPT 128: Introduction to Computing Science for Engineering Students C++ Basic Input and output.
Chapter 3 COMPLETING THE BASICS Programming Fundamentals with C++1.
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.
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.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 3: Input/Output.
Input, Output, and Processing
Computer Programming TCP1224 Chapter 4 Variables, Constants, and Arithmetic Operators.
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
A FIRST BOOK OF C++ CHAPTER 3 ASSIGNMENT AND INTERACTIVE INPUT.
Chapter 3 Arithmetic Expressions, Function Calls, and Output
Chapter 3: Assignment, Formatting, and Interactive Input.
C++ for Engineers and Scientists Second Edition Chapter 3 Assignment, Formatting, and Interactive Input.
C++ Programming, Namiq Sultan1 Chapter 3 Expressions and Interactivity Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin.
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.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 3 Expressions and Interactivity.
Chapter 3: Input/Output
Expressions and Interactivity. 3.1 The cin Object.
1 09/27/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Formatting Output.
Programming Fundamentals with C++1 Chapter 3 COMPLETING THE BASICS.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Recap……Last Time [Variables, Data Types and Constants]
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.
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.
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.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output Samples.
Lecture 3 Expressions, Type Conversion, Math and String
Introduction to C++ (Extensions to C)
C++ Basic Input and Output (I/O)
Chapter Topics The Basics of a C++ Program Data Types
Programming Fundamentals
Basic Elements of C++.
Expressions and Interactivity
A First Book of ANSI C Fourth Edition
Expressions and Interactivity
Chapter 3: Input/Output
Formatting the Output The C++ standard library supplies many manipulators: endl, setw, fixed, showpoint, setprecesion. If we want to use endl, fixed, or.
C++ Programming Basics
Lecture 3 Expressions, Type Conversion, and string
Programming Fundamental-1
Presentation transcript:

Last Time…. Operators Arithmetic Operators Assignment Operators Increment/Decrement Operators Relational Operators Logical Operators Expression Statements Introduction to Library Functions

Precedence of Operators Evaluation the following Expressions: a) * 6 / 3 – 5 b) /

Summary of Operators

Defining constants  const Qualifier  #define directive

Automatic Type Conversion and Casts int count = 100; float avgWeight = 70F; double totalWeight = count * avgWeight; cout<<“The total weight in this room = ”<<totalWeight; Automatic Type Conversion, Compiler handles the type conversion

Static Casts int result; float a = 4.0; result = a * 3.56; > How do you cast to another type? >>Up-Cast >>Down-Cast

Implicit Type Conversion When an operator’s operands are of different data types, C++ will automatically convert them to the same data type. When a value is converted to a higher data type, it is said to be prompted. To demote a value means to convert it to a lower data type.

Two Rules Rule 1: When an operator works with two values of different values of different data types, the lower-ranking value is prompted to the type of the higher-ranking value. Rule 2: When the final value of and expression is assigned to a variable, it will be converted to the data type of that variable.

Explicit Type Conversion [A type cast expression lets you manually promote or demote a value in the same way that automatic conversion takes place.]

Syntax static_cast (Value) A variable or Literal value Data Type you wish to convert it to

int main() { int books, months; double booksPerMonth; cout<<“How many books do you plan to read?” cin>>books; cout<<“How many month will it take you to read them?” cin>>months; booksPerMonth = books/months; cout<<“That is”<<booksPerMonth<<“books per month.\n”; system(“pause”); return 0; } How would you prevent the INTEGER DIVISION?

Quiz _1 : Implement an Pseudocode Write a program that implements the following algorithm. Start Read the total hours the employee has worked, TotalHours Read the hourly rate of pay for the employee, HourlyRate GrossSalary = TotalHours * HourlyRate Tax = GrossSalary * 0.1 NetSalary = GrossSalary - Tax Display NetSalary Stop

Quiz _2:Currency [Write a program that will convert U.S. dollar amounts to Japanese yen and to euros. The conversion factors to use are 1dollar = yen and 1 dollar = euros]

[Solutions to the Quiz Questions]

Library Functions Yared Semu Addis Ababa Institute of Technology April 2012

Library Functions In C++, we make extensive use of library functions to accomplish tasks. –Mathematical Functions –IO Manipulators

Mathematical Functions The mathematical functions allow us to do mathematical operations. These operations include: raising a number to a certain power, computing the square root of a number, computing the cosine of an angle, etc.... These functions are defined in the header file math.h (or cmath in standard C++). #include

Mathematical Functions … sqrt is the name of the function that performs the square root operation. This function takes one argument of type double and returns a result of type double.

Multiple Arguments When a function takes multiple arguments, they are separated by commas inside the parenthesis. This statement computes 3 raised to 4.

Mathematical Functions … The function that computes the power of two numbers is : More examples of mathematical functions are:

IO Manipulators IO Manipulators are operators used with the insertion operator (<<) to modify or manipulate the way data is displayed.  Other IO manipulators, which take arguments, are defined in the iomanip.h (or iomanip in standard C++) header file. #include

Commonly Used Manipulators General Output ManipulatorRangeDescription endlnowWrite a newline ('\n') and flush buffer. setw(n)next Sets minimum field width on output. (Note: A field is an area on a computer screen, where information such as characters or numbers can be entered and manipulated) Use left and right to justify the data appropriately in the field. Output is right justified by default. e.g. cout<<setw(7)<<n<<endl width(n)nextSame as setw(n)

IO Manipulators General Output ManipulatorRangeDescription leftnow Left justifies output in field width. Only useful after setw(n). rightnext Right justifies output in field width. Since this is the default, it is only used to override the effects of left. Only useful after setw(n). setfill(ch)all Only useful after setw. If a value does not entirely fill a field, the character ch will be used to fill in the other characters. Default value is blank. E.g. cout << setw(4) << setfill('0') << n << endl;

IO Manipulators Floating point output ManipulatorRangeDescription setprecision(n)all Sets the number of digits printed to the right of the decimal point. This applies to all subsequent floating point numbers written to that output stream. However, this won't make floating-point "integers" print with a decimal point. It's necessary to use fixed for that effect fixedall Used fixed point notation for floating-point numbers. Opposite of scientific. If no precision has already been specified, it will set the precision to 6 scientificall Formats floating-point numbers in scientific notation. Opposite of fixed.

IO Manipulators(Example) What is the output of this program?

Example- IO Manipulators Write a program that displays the output show below.

Exercises