chapter 3: expressions and interactivity. outline cin object mathematical expressions type...

35
Chapter 3: Expressions and Interactivity

Upload: ernest-blankenship

Post on 14-Dec-2015

225 views

Category:

Documents


1 download

TRANSCRIPT

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

Chapter 3:

Expressions and Interactivity

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

Outline

• cin object• Mathematical expressions • Type Conversion and Some coding styles

Page 3: 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

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

How to use cin?

SYNTAX

These examples yield the same result.cin >> length ;cin >> width ;

orcin >> length >> width ;

cin >> Variable >> Variable . . . ;

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

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

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

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 << "How tall is the room? ";cin >> height;

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

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.

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

Slide 3- 10

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

Outline

• cin object• Mathematical expressions • Type Conversion and Some coding styles

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

Slide 3- 12

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

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

Mathematical Expressions

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

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

Some C++ operatorsPrecedence Operator Description Higher + Positive - Negative

----------------------------------------------------------- * Multiplication / Division % Modulus

------------------------------------------------------------ + Addition

- Subtraction-------------------------------------------------------------

Lower = Assignment

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

Example

7 * 10 - 5 % 3 * 4 + 9means (7 * 10) - 5 % 3 * 4 + 9

70 - 5 % 3 * 4 + 970 - (5 % 3) * 4 + 9 70 - 2 * 4 + 9 70 - ( 2 * 4 ) + 9 70 - 8 + 9 ( 70 - 8 ) + 9

62 + 9 71

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

Parentheses

• Parentheses can be used to change the usual order; expressions inside ( ) are evaluated first

• evaluate (7 * (10 - 5) % 3) * 4 + 9 ( 7 * 5 % 3 ) * 4 + 9

( 35 % 3 ) * 4 + 9 2 * 4 + 9 8 + 9 17

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

Slide 3- 17

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 asm = (y2-y1) /(x2-

x1); 12

12

xx

yym

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

When calculation average

• Average = a + b + c / 3.0; (Wrong!!!)

• Average = (a + b + c) / 3.0; (Correct!!!)

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

Slide 3- 19

Algebraic Expressions

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

Algebraic Expressions

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

Algebra and C++ expressions

23x

y y = 3 * x /2;

A = (3*x+2)/(4*a-1);

d = b*b -4*a*c;

14

23

a

xa

acbd 42

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

Outline

• cin object• Mathematical expressions • Type Conversion and Some coding styles

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

Slide 3- 23

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.

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

Example

• double parts;• parts = 15/6; //parts=2

• double parts;• parts = 15.0/6; //parts=2.5

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

Slide 3- 25

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 = 0.0675;const int NUM_STATES = 50;

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

Slide 3- 26

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

Slide 3- 27

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));

valueis 5

valueis 5

valueis 5

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

Slide 3- 28

Combined Assignment

• Look at the following statement:

sum = sum + 1;

This adds 1 to the variable sum.

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

Slide 3- 29

Other Similar Statements

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

Slide 3- 30

Other Similar Statements

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

Slide 3- 31

Combined Assignment

• The combined assignment operators provide a shorthand for these types of statements.

• The statementsum = sum + 1;

is equivalent to sum += 1;

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

Slide 3- 32

Combined Assignment Operators

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

Slide 3- 33

Combined Assignment Operators

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

Slide 3- 34Slide 3- 34

Mathematical Library Functions #include <cmath> 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

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

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

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

Mathematical Library Functions

Examples:sqrt_value = sqrt(49); //Result is 7abs_value = abs(-34.5); //Result is 34.5power_value = pow(2.1,3); //Result is 9.261cosine_value = cos(3.1415196/6);

//Result is 0.866

Slide 3- 36