Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 3: Expressions and Interactivity.

Similar presentations


Presentation on theme: "Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 3: Expressions and Interactivity."— Presentation transcript:

1 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 3: Expressions and Interactivity

2 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. 3.1 The cin Object

3 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. The cin Object Standard input object Requires iostream header file cin like cout is a member of the standard namespace; therefore, always: std::cin It is a std::istream object Used to read input from keyboard

4 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. The cin Object Information retrieved from cin with >> >> is called the Extraction Operator Extracts data from the input stream and moves the data to variables Input is stored in one or more variables May input data into multiple variables One variable after each >>

5 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. The cin Object in Program 3-1

6 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. The cin Object cin and the extraction operator convert data to the type that matches the variable: int height; std::cout << "How tall is the room? "; std::cin >> height; cin reads until it reaches a character that it cannot convert to the appropriate type. That character is put back into the buffer.

7 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Displaying a Prompt A prompt is a message that instructs the user to enter data. Always use cout to display a prompt before each cin statement. std::cout > height;

8 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. The cin Object Can be used to input more than one value: std::cin >> height >> width; Multiple values from keyboard must be separated by whitespace Space, Tab, or Carriage Return (Enter) Order is important: first value entered goes into first variable, etc.

9 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. The cin Object Gathers Multiple Values in Program 3-2

10 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. The cin Object Reads Different Data Types in Program 3-3

11 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. 3.2 Mathematical Expressions

12 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Mathematical Expressions Can create complex expressions using multiple mathematical operators An expression can be a literal, a variable, a constant or a mathematical combination of any of these Can be used in assignment, cout, other statements: area = 2 * PI * radius; std::cout << "border is: " << 2 * (l + w);

13 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Order of Operations In an expression with more than one operator, evaluate in this order: - (unary negation), in order, left to right * / %, in order, left to right + -, in order, left to right In the expression: 2 + 2 * 2 – 2 evaluate first evaluate second evaluate third

14 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Order of Operations Examples 5 + 2 * 4 = 5 + 8 = 13 10 / 2 – 3 = 5 – 3 = 2 8 + 12 * 2 – 4 = 8 + 24 – 4 = 32 - 4 = 28

15 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Order of Operations Examples 4 + 17 % 2 – 1 = 4 + 1 – 1 = 5 – 1 = 4 6 – 3 * 2 + 7 – 1 = 6 – 6 + 7 – 1 = 0 + 7 – 1 = 7 – 1 = 6

16 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Associativity of Operators - (unary negation) associates right to left *, /, % associate left to right +, - associate left to right parentheses ( ) can be used to override the order of operations (do what is in parentheses first): 2 + 2 * 2 – 2 = 4 (2 + 2) * 2 – 2 = 6 2 + 2 * (2 – 2) = 2 (2 + 2) * (2 – 2) = 0

17 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Grouping with Parentheses (5 + 2) * 4 = 7 * 4 = 28 10 / (5 – 3) = 10 / 2 = 5 8 + 12 * (6 – 2) = 8 + 12 * 4 = 8 + 48 = 56

18 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Grouping with Parentheses (4 + 17) % 2 – 1 = 21 % 2 – 1 = 1 – 1 = 0 (6 – 3) * (2 + 7) / 3 = 3 * (2 + 7) / 3 = 3 * 9 / 3 = 27 / 3 = 9

19 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Algebraic Expressions Multiplication requires an operator: A=lw is written as area = len * wid; There is no exponentiation operator: A=s 2 is written as area = std::pow(side, 2); Parentheses may be needed to maintain order of operations: is written as slope = (y2 - y1) / (x2 - x1);

20 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Algebraic Expressions

21 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. 3.3 When You Mix Apples with Oranges: Type Conversion

22 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. When You Mix Apples with 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.

23 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Hierarchy of Types Highest: Lowest: Ranked by maximum value they can hold long double double float unsigned long signed long unsigned int signed int unsigned short signed short unsigned char signed char bool

24 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Type Coercion Type Coercion: Automatic conversion of an operand to another data type Promotion: convert to a higher type Demotion: convert to a lower type Only happens when the programmer forces it

25 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Coercion Rules 1) char, short, unsigned short automatically promoted to int 2) When operating on values of different data types, the lower one is promoted to the type of the higher one. 3) When using the = operator, the value of the result of the expression on right will be converted to type of variable on left at time of assignment (the type of the rhs does not change)

26 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. 3.4 Overflow and Underflow

27 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Overflow and Underflow Occurs when assigning a value that is too large (overflow) or too small (underflow) to be held in a variable Variable contains value that is ‘wrapped around’ set of possible values Systems may display a warning/error message, stop the program, or continue execution when overflow or underflow occurs

28 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. 3.5 Type Casting

29 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Type Casting Used for programmer data type conversion Useful for floating point division using int s: double slope; int x1, x2, y1, y2; slope = static_cast (y2 - y1) / (x2 - x1); Useful to see int value of a char variable: char ch = 'C'; std::cout (ch);

30 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Type Casting in Program 3-9

31 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. C-Style and Prestandard Type Cast Expressions C-Style cast: data type name in () std::cout << ch << " is " << (int)ch; Prestandard C++ cast: value in () std::cout << ch << " is " << int(ch); Both are still supported in C++, although static_cast is preferred and will be used in this class.

32 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. 3.6 Multiple Assignment and Combined Assignment

33 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Multiple Assignment The = can be used to assign a value to multiple variables: x1 = y1 = z1 = 5.2; Value of item to the immediate right of the = is the value that is assigned All values to the left of an = must be variables Cannot be done at time of variable definition double a3 = b3 = 3.2; // invalid double a3 = 3.2, b3 = 3.2; // valid

34 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Multiple Assignment Associates right to left: double x1; float y1; int z1; x1 = (y1 = (z1 = 5.2)); Better way: z 1 = (y1 = (x1 = 5.2)); value is 5 value is 5 value is 5 value is 5.2 value is 5.2 value is 5

35 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Compound Assignment Operators The following statement: sum = sum + 1; results in adding 1 to the variable sum. The result of sum + 1 is loaded to a temporary location. The value in that temporary location is what is assigned to sum.

36 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Other Similar Statements 10 7 70 35 3

37 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Compound Assignment Operators The compound assignment operators provide a shorthand for these types of statements. The result of the statement sum = sum + 1; is equivalent to the statement sum += 1; The first statement creates a temporary value to assign to sum, but the second one does not.

38 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Compound Assignment Operators Be cautious when the right hand side of a compound assignment operator is an equation The result of the statement result *= var + 5; is equivalent to the statement result = result * (var + 5);

39 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Combined Assignment Operators

40 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. 3.7 Formatting Output

41 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Formatting Output Can control how output displays for numeric, string data: size position number of digits Requires iomanip header file

42 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Stream Manipulators Used to control how an output field is displayed Some affect only the next value displayed: std::setw(x) output in a field at least x spaces wide Uses more spaces if field is not wide enough

43 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. The setw Stream Manipulator in Program 3-13 Continued…

44 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. The setw Stream Manipulator in Program 3-13

45 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Stream Manipulators Some affect values until changed again: std::fixed use decimal notation for floating-point values with a fixed number of decimal places std::setprecision(x) Used with std::fixed, outputs floating-point value using x digits after the decimal. Used without std::fixed, outputs floating- point value using x significant digits std::showpoint always output decimal for floating-point values [RD: only needed with the previous two when x = 0 ]

46 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. More Stream Manipulators in Program 3-17 Continued…

47 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. More Stream Manipulators in Program 3-17

48 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Stream Manipulators Only affects the next value output. All of the above require std::

49 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. 3.8 Working with Characters and string Objects

50 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Working with Characters and string Objects Using cin with the >> operator to input strings can cause problems: It passes over and ignores any leading whitespace characters (spaces, tabs, or line breaks) It stops at any trailing whitespace. To work around this problem, you can use a C++ function named getline.

51 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Using std::getline in Program 3-19

52 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Working with Characters and string Objects To read a single character: Use cin : char ch; std::cout << "Strike any key to continue"; std::cin >> ch; Problem: will skip over leading whitespace Use cin.get() : std::cin.get(ch); Will read the next character entered, even if whitespace

53 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Using cin.get() in Program 3-21

54 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Working with Characters and string Objects Mixing cin >> and cin.get() in the same program can cause input errors that are hard to detect When std::cin >> hits a new line char, it leaves it in the buffer To skip over unneeded characters that are still in the keyboard buffer, use cin.ignore() std::cin.ignore(); // removes next char std::cin.ignore(10, '\n'); // removes the next 10 characters // or until a '\n' is reached and // removes it from the buffer

55 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. string Member Functions and Operators To find the length of a string : To concatenate (join) multiple string s: std::string state = "Texas"; int size = state.length(); greeting2 = greeting1 + name1; greeting2 = greeting2 + name2; Or using the += combined assignment operator: greeting2 += name2;

56 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. 3.9 More Mathematical Library Functions

57 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. More Mathematical Library Functions Require cmath header file Take a double as input, return a double Commonly used functions: sin Sine cos Cosine tan Tangent sqrt Square root log Natural (e) log abs fabs Absolute value (takes and returns an int) Float Absolute value (takes and returns a double or float)

58 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. More Mathematical Library Functions These require cstdlib header file std::rand() returns a random number ( int ) between 0 and RAND_MAX inclusive (can be the largest int the compute holds). Yields same sequence of numbers each time program is run. std::srand(seed); initializes random number generator with an unsigned int Different values for seed create different random number sequences.

59 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. 3.10 Hand Tracing a Program

60 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Hand Tracing a Program Hand trace a program: Act as if you are the computer, executing a program: Step through and ‘execute’ each statement, one-by-one Record the contents of variables after each statement execution, using a hand trace chart (table) Useful in locating logic errors

61 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Program 3-27 with Hand Trace Chart

62 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. 3.11 A Case Study

63 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. A Case Study General Crates, Inc. (GCI) builds custom- designed wooden crates. You have been asked to write a program that calculates the: Volume (in cubic feet) Cost Customer price Profit on any crate GCI builds

64 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Variables

65 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Program Design The program must perform the following general steps: Step 1: Ask the user to enter the dimensions of the crate Step 2: Calculate: the crate’s volume the cost of building the crate the customer’s charge the profit made Step 3: Display the data calculated in Step 2.

66 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. General Hierarchy Chart

67 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Get Crate Dimensions

68 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Calculate Volume, Cost, Customer Charge, and Profit

69 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Display Calculated Data

70 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Psuedocode Ask the user to input the crate's length. Ask the user to input the crate's width. Ask the user to input the crate's height. Calculate the crate's volume. Calculate the cost of building the crate. Calculate the customer's charge for the crate. Calculate the profit made from the crate. Display the crate's volume. Display the cost of building the crate. Display the customer's charge for the crate. Display the profit made from the crate.

71 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Calculations The following formulas will be used to calculate the crate’s volume, cost, charge, and profit: volume = length × width × height cost = volume × 0.23 charge = volume × 0.5 profit = charge − cost

72 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. The Program Continued…

73 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. The Program Continued…

74 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. The Program

75 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. End of Chapter 3 Followed by In Class Lab Assignment

76 Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. In Class Lab Assignment Write a program that asks the user for two integers where the second is larger than the first. Output the full equation for: The sum of the two integers The difference of the second minus the first The product of the two integers The quotient of the second divided by the first The remainder of the second divided by the first The quotient as a double Run the program multiple times with different input


Download ppt "Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 3: Expressions and Interactivity."

Similar presentations


Ads by Google