Chapter 3: Expressions and Interactivity. Outline cin object Mathematical expressions Type Conversion and Some coding styles.

Slides:



Advertisements
Similar presentations
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Expressions.
Advertisements

CS1 Lesson 3 Expressions and Interactivity CS1 -- John Cole1.
Chapter 3 Assignment and Interactive Input. 2 Objectives You should be able to describe: Assignment Operators Mathematical Library Functions Interactive.
Copyright © 2012 Pearson Education, Inc. Chapter 3: Expressions and Interactivity.
Announcements Quiz 1 Next Week. int : Integer Range of Typically -32,768 to 32,767 (machine and compiler dependent) float : Real Number (i.e., integer.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 3 Expressions.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 3- 1.
1 9/08/06CS150 Introduction to Computer Science 1 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.
Admin Office hours 2:45-3:15 today due to department meeting if you change addresses during the semester, please unsubscribe the old one from the.
Basic Elements of C++ Chapter 2.
Expressions and Interactivity Chapter 3. 2 The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Often.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Chapter 3 COMPLETING THE BASICS Programming Fundamentals with C++1.
CSE1222: Lecture 4The Ohio State University1. Mathematical Functions (1)  The math library file cmath Yes, this is a file with definitions for common.
CSE202: Lecture 4The Ohio State University1 Mathematical Functions.
Chapter 3 Expressions and Interactivity Department of Computer Science Missouri State Univeristy.
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.
Computer Programming TCP1224 Chapter 4 Variables, Constants, and Arithmetic Operators.
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
C++ Programming: Basic Elements of C++.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Chapter 2 Overview of C++. 2 Overview  2.1 Language Elements  2.2 Reserved Words & Identifiers  2.3 Data Types & Declarations  2.4 Input/Output 
CHAPTER 2 COMPONENTS OF A PROGRAMMING LANGUAGE I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
A FIRST BOOK OF C++ CHAPTER 3 ASSIGNMENT AND INTERACTIVE INPUT.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for CMPS 1043 Computer Science I at MSU.
Chapter 3: Assignment, Formatting, and Interactive Input.
Control Structures (B) Topics to cover here: Sequencing in C++ language.
C++ Programming, Namiq Sultan1 Chapter 3 Expressions and Interactivity Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin.
Chapter 6 Mathematical Operations. 6.1 Mathematical Expressions In mathematics this expression is valid 0 = -4y + 5 It is invalid in programming Left.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 3 Expressions and Interactivity.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Expressions and Interactivity. 3.1 The cin Object.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
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]
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 3: Expressions and Interactivity.
CMPSC 121- Spring 2015 Lecture 6 January 23, 2015.
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 3: Expressions and Interactivity.
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
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.
SCP1103 Basic C Programming SEM1 2010/2011 Arithmetic Expressions Week 5.
Lecture 3 Expressions, Type Conversion, Math and String
Arithmetic Expressions
Chapter Topics The Basics of a C++ Program Data Types
Chapter 3 Assignment and Interactive Input.
Basic Elements of C++.
Chapter 2 Assignment and Interactive Input
Chapter 3: Expressions and Interactivity.
Basic Elements of C++ Chapter 2.
Expressions and Interactivity
A First Book of ANSI C Fourth Edition
Expressions and Interactivity
CS150 Introduction to Computer Science 1
Standard Version of Starting Out with C++, 4th Edition
Lecture 3 Expressions, Type Conversion, and string
Presentation transcript:

Chapter 3: Expressions and Interactivity

Outline cin object Mathematical expressions Type Conversion and Some coding styles

The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard

How to use cin ? SYNTAX These examples yield the same result. cin >> length ; cin >> width ; or cin >> length >> width ; cin >> Variable >> Variable... ;

The cin Object Information retrieved from cin with >> (the extraction operator) – operator >> attempts to extract the next item from the input stream and store its value in the right operand variable Input is stored in one or more variables

Slide 3- 7 The cin Object cin converts data to the type that matches the variable: int height; cout << "How tall is the room? "; cin >> height;

Slide 3- 8 Displaying a Prompt A prompt is a message that instructs the user to enter data. You should always use cout to display a prompt before each cin statement. cout > height;

Slide 3- 9 The cin Object Can be used to input more than one value: cin >> height >> width; Multiple values from keyboard must be separated by spaces Order is important: first value entered goes to first variable, etc.

Slide 3- 10

Outline cin object Mathematical expressions Type Conversion and Some coding styles

Slide Mathematical Expressions Can create complex expressions using multiple mathematical operators An expression can be a literal, a variable, or a mathematical combination of constants and variables

Mathematical Expressions Can be used in assignment, cout, other statements: area = 2 * PI * radius; cout << "border is: " << 2*(l+w);

Some C++ operators Precedence Operator Description Higher +Positive - Negative * Multiplication / Division %Modulus Addition - Subtraction Lower = Assignment

Example 7 * % 3 * means (7 * 10) - 5 % 3 * % 3 * (5 % 3) * * ( 2 * 4 ) ( )

Parentheses Parentheses can be used to change the usual order; expressions inside ( ) are evaluated first evaluate (7 * (10 - 5) % 3) * ( 7 * 5 % 3 ) * ( 35 % 3 ) * *

Slide Algebraic Expressions Multiplication requires an operator: Area=lw is written as Area = l * w; Parentheses may be needed to maintain order of operations: is written as m = (y2-y1) /(x2-x1);

When calculation average Average = a + b + c / 3.0; (Wrong!!!) Average = (a + b + c) / 3.0; (Correct!!!)

Slide Algebraic Expressions

Algebra and C++ expressions y = 3 * x /2; A = (3*x+2)/(4*a-1); d = b*b -4*a*c;

Outline cin object Mathematical expressions Type Conversion and Some coding styles

Slide When You Mix Apples and Oranges: Type Conversion Operations are performed between operands of the same type. If not of the same type, C++ will convert one to be the type of the other This can impact the results of calculations.

Example double parts; parts = 15/6;//parts=2 double parts; parts = 15.0/6;//parts=2.5

Slide Named (Symbolic) Constants Named constant (constant variable): variable whose content cannot be changed during program execution Often named in uppercase letters Used for representing constant values with descriptive names: const double TAX_RATE = ; const int NUM_STATES = 50;

Slide 3- 26

Slide Multiple Assignment and Combined Assignment The = can be used to assign a value to multiple variables: x = y = z = 5; Value of = is the value that is assigned Associates right to left: x = (y = (z = 5)); value is 5 value is 5 value is 5

Slide Combined Assignment Look at the following statement: sum = sum + 1; This adds 1 to the variable sum.

Slide Other Similar Statements

Slide Other Similar Statements

Slide Combined Assignment The combined assignment operators provide a shorthand for these types of statements. The statement sum = sum + 1; is equivalent to sum += 1;

Slide Combined Assignment Operators

Slide Combined Assignment Operators

Slide Mathematical Library Functions #include Commonly used functions: where n can be an integer, double, float. sin(n) Sine cos(n) Cosine tan(n) Tangent sqrt(n) Square root log(n) Natural (e) log abs(n) pow(b,n) Absolute value (takes and returns an int) Exponents

Mathematical Library Functions In order to user the functions you need to know: name of the function you need what the function does (input/output) input parameter and the data type of parameter data type of returned value Syntax: value = function_name (argument); Slide 3- 35

Mathematical Library Functions Examples: sqrt_value = sqrt(49);//Result is 7 abs_value = abs(-34.5);//Result is 34.5 power_value = pow(2.1,3);//Result is cosine_value = cos( /6); //Result is Slide 3- 36