cmsc 104, version 8/061l10arithmeticops.ppt arithmetic operators topics arithmetic operators...

25
CMSC 104, Version 8/06 L10ArithmeticOps.ppt Arithmetic Operators Topics Arithmetic Operators Operator Precedence Evaluating Arithmetic Expressions In-class Project Incremental Programming Reading Section 2.5

Upload: milton-ferguson

Post on 18-Jan-2016

253 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CMSC 104, Version 8/061L10ArithmeticOps.ppt Arithmetic Operators Topics Arithmetic Operators Operator Precedence Evaluating Arithmetic Expressions In-class

CMSC 104, Version 8/06 1L10ArithmeticOps.ppt

Arithmetic Operators

Topics

• Arithmetic Operators• Operator Precedence• Evaluating Arithmetic Expressions• In-class Project• Incremental Programming

Reading

• Section 2.5

Page 2: CMSC 104, Version 8/061L10ArithmeticOps.ppt Arithmetic Operators Topics Arithmetic Operators Operator Precedence Evaluating Arithmetic Expressions In-class

CMSC 104, Version 8/06 2L10ArithmeticOps.ppt

Arithmetic Operators in C

Name Operator Example

Addition + num1 + num2 Subtraction - initial - spent Multiplication * fathoms * 6 Division / sum / count Modulus % m % n

Page 3: CMSC 104, Version 8/061L10ArithmeticOps.ppt Arithmetic Operators Topics Arithmetic Operators Operator Precedence Evaluating Arithmetic Expressions In-class

CMSC 104, Version 8/06 3L10ArithmeticOps.ppt

Division

• If both operands of a division expression are integers, you will get an integer answer. The fractional portion is thrown away.

• Examples : 17 / 5 = 3 4 / 3 = 1 35 / 9 = 3

Page 4: CMSC 104, Version 8/061L10ArithmeticOps.ppt Arithmetic Operators Topics Arithmetic Operators Operator Precedence Evaluating Arithmetic Expressions In-class

CMSC 104, Version 8/06 4L10ArithmeticOps.ppt

Division (con’t)

• Division where at least one operand is a floating point number will produce a floating point answer.

• Examples : 17.0 / 5 = 3.4 4 / 3.2 = 1.25 35.2 / 9.1 = 3.86813• What happens? The integer operand is

temporarily converted to a floating point, then the division is performed.

Page 5: CMSC 104, Version 8/061L10ArithmeticOps.ppt Arithmetic Operators Topics Arithmetic Operators Operator Precedence Evaluating Arithmetic Expressions In-class

CMSC 104, Version 8/06 5L10ArithmeticOps.ppt

Division By Zero

• Division by zero is mathematically undefined.

• If you allow division by zero in a program, it will cause a fatal error. Your program will terminate execution and give an error message.

• Non-fatal errors do not cause program termination, just produce incorrect results.

Page 6: CMSC 104, Version 8/061L10ArithmeticOps.ppt Arithmetic Operators Topics Arithmetic Operators Operator Precedence Evaluating Arithmetic Expressions In-class

CMSC 104, Version 8/06 6L10ArithmeticOps.ppt

Modulus

• The expression m % n yields the integer remainder after m is divided by n.

• Modulus is an integer operation -- both operands MUST be integers.

• Examples : 17 % 5 = 2 6 % 3 = 0 9 % 2 = 1 5 % 8 = 5

Page 7: CMSC 104, Version 8/061L10ArithmeticOps.ppt Arithmetic Operators Topics Arithmetic Operators Operator Precedence Evaluating Arithmetic Expressions In-class

CMSC 104, Version 8/06 7L10ArithmeticOps.ppt

Uses for Modulus

• Used to determine if an integer value is even or odd

5 % 2 = 1 odd 4 % 2 = 0 even

If you take the modulus by 2 of an integer,

a result of 1 means the number is odd and

a result of 0 means the number is even.

• The Euclid’s GCD Algorithm (done earlier)

Page 8: CMSC 104, Version 8/061L10ArithmeticOps.ppt Arithmetic Operators Topics Arithmetic Operators Operator Precedence Evaluating Arithmetic Expressions In-class

CMSC 104, Version 8/06 8L10ArithmeticOps.ppt

Arithmetic Operators Rules of Operator Precedence

Operator(s) Precedence & Associativity

( ) Evaluated first. If nested (embedded), innermost first. If

on same level, left to right.

* / % Evaluated second. If there areseveral, evaluated left to right.

+ - Evaluated third. If there are several, evaluated left to right.

= Evaluated last, right to left.

Page 9: CMSC 104, Version 8/061L10ArithmeticOps.ppt Arithmetic Operators Topics Arithmetic Operators Operator Precedence Evaluating Arithmetic Expressions In-class

CMSC 104, Version 8/06 9L10ArithmeticOps.ppt

Using Parentheses

• Use parentheses to change the order in which an expression is evaluated. a + b * c Would multiply b * c first,

then add a to the result. If you really want the sum of a and b to be

multiplied by c, use parentheses to force the evaluation to be done in the order you want.

(a + b) * c• Also use parentheses to clarify a complex

expression.

Page 10: CMSC 104, Version 8/061L10ArithmeticOps.ppt Arithmetic Operators Topics Arithmetic Operators Operator Precedence Evaluating Arithmetic Expressions In-class

CMSC 104, Version 8/06 10L10ArithmeticOps.ppt

Extended Example

Given integer variables a, b, c, d, and e, where a = 1, b = 2, c = 3, d = 4, evaluate the following expression:

e = b % d / c * b – a e = ( b % d ) / c * b – a e = ( ( b % d ) / c ) * b – a e = ( ( ( b % d ) / c ) * b ) – a e = ( ( ( ( b %d ) / c ) * b ) – a ) e = ( ( ( ( 2 % 4 ) / 3 ) * 2 ) – 1 )

Page 11: CMSC 104, Version 8/061L10ArithmeticOps.ppt Arithmetic Operators Topics Arithmetic Operators Operator Precedence Evaluating Arithmetic Expressions In-class

CMSC 104, Version 8/06 11L10ArithmeticOps.ppt

Extended Example (cont’d)

e = ( ( ( ( 2 % 4 ) / 3 ) * 2 ) – 1 ) e = ( ( ( ( 2 ) / 3 ) * 2 ) – 1 ) e = ( ( ( 2 / 3 ) * 2 ) – 1 ) e = ( ( ( 0 ) * 2 ) – 1 ) e = ( ( 0 * 2 ) – 1 ) e = ( ( 0 ) – 1 ) e = ( 0 – 1 ) e = – 1

Note: Always use parenthesis when you have more than two operators!

Page 12: CMSC 104, Version 8/061L10ArithmeticOps.ppt Arithmetic Operators Topics Arithmetic Operators Operator Precedence Evaluating Arithmetic Expressions In-class

CMSC 104, Version 8/06 12L10ArithmeticOps.ppt

Good Programming Practices

Always use parenthesis when you have more than two operators!

Page 13: CMSC 104, Version 8/061L10ArithmeticOps.ppt Arithmetic Operators Topics Arithmetic Operators Operator Precedence Evaluating Arithmetic Expressions In-class

CMSC 104, Version 8/06 13L10ArithmeticOps.ppt

Extended Example

Given integer variables a, b, c, d, and e, where a = 1, b = 2, c = 3, d = 4, evaluate the following expression:

e = b % d / c * b – a e = ( b % d ) / c * b – a e = ( ( b % d ) / c ) * b – a e = ( ( ( b % d ) / c ) * b ) – a e = ( ( ( ( b %d ) / c ) * b ) – a ) e = ( ( ( ( 2 % 4 ) / 3 ) * 2 ) – 1 )

Page 14: CMSC 104, Version 8/061L10ArithmeticOps.ppt Arithmetic Operators Topics Arithmetic Operators Operator Precedence Evaluating Arithmetic Expressions In-class

CMSC 104, Version 8/06 14L10ArithmeticOps.ppt

Another Extended Example

Given integer variables a, b, c, d, and e, where a = 1, b = 2, c = 3, d = 4,

evaluate the following expression:

d = e = 1 + a + b * d % c d = e = 1 + a + ( b * d ) % c d = e = 1 + a + ( ( b * d ) % c ) d = e = ( 1 + a ) + ( ( b * d ) % c ) d = e = ( ( 1 + a ) + ( ( b * d ) % c ) ) d = ( e = ( ( 1 + a ) + ( ( b * d ) % c ) ) ) d = ( e = ( ( 1 + 1 ) + ( ( 2 * 4 ) % 3 ) ) )

Page 15: CMSC 104, Version 8/061L10ArithmeticOps.ppt Arithmetic Operators Topics Arithmetic Operators Operator Precedence Evaluating Arithmetic Expressions In-class

CMSC 104, Version 8/06 15L10ArithmeticOps.ppt

Another Extended Example (cont’d)

d = ( e = ( ( 1 + 1 ) + ( ( 2 * 4 ) % 3 ) ) )d = ( e = ( ( 1 + 1 ) + ( ( 8 ) % 3 ) ) )d = ( e = ( ( 1 + 1 ) + ( 8 % 3 ) ) )d = ( e = ( ( 1 + 1 ) + ( 2 ) ) )d = ( e = ( ( 1 + 1 ) + 2 ) )d = ( e = ( ( 2 ) + 2 ) )d = ( e = ( 2 + 2 ) )d = ( e = ( 4 ) )d = ( e = 4 )d = 4 /* e is now set to 4 and so is d */

Page 16: CMSC 104, Version 8/061L10ArithmeticOps.ppt Arithmetic Operators Topics Arithmetic Operators Operator Precedence Evaluating Arithmetic Expressions In-class

CMSC 104, Version 8/06 16L10ArithmeticOps.ppt

Practice With Evaluating Expressions

Given integer variables a, b, c, d, and e, where a = 1, b = 2, c = 3, d = 4,

evaluate the following expressions:

a + b - c + d a * b / c 1 + a * b % c a + d % b - c e = b = d + c / b - a

Page 17: CMSC 104, Version 8/061L10ArithmeticOps.ppt Arithmetic Operators Topics Arithmetic Operators Operator Precedence Evaluating Arithmetic Expressions In-class

CMSC 104, Version 8/06 17L10ArithmeticOps.ppt

A Sample Project

• Let’s write a program that computes and displays the volume and surface area of a box. (I’ll help with prompting the user and displaying the results.)

• Procedure:o Use the pseudocode that we developed in

“Algorithms, Part 3 of 3”o Convert the algorithm to codeo Clean up the code (spacing, indentation,

commenting)

Page 18: CMSC 104, Version 8/061L10ArithmeticOps.ppt Arithmetic Operators Topics Arithmetic Operators Operator Precedence Evaluating Arithmetic Expressions In-class

CMSC 104, Version 8/06 18L10ArithmeticOps.ppt

The Box - Pseudocode

Display “Enter the height: “

Read <height>

While (<height> <= 0 )

Display “The height must be > 0”

Display “Enter the height: “

Read <height>

End_while

Page 19: CMSC 104, Version 8/061L10ArithmeticOps.ppt Arithmetic Operators Topics Arithmetic Operators Operator Precedence Evaluating Arithmetic Expressions In-class

CMSC 104, Version 8/06 19L10ArithmeticOps.ppt

The Box - Pseudocode (con’t)

Display “Enter the width: “

Read <width>

While (<width> <= 0 )

Display “The width must be > 0”

Display “Enter the width: “

Read <width>

End_while

Page 20: CMSC 104, Version 8/061L10ArithmeticOps.ppt Arithmetic Operators Topics Arithmetic Operators Operator Precedence Evaluating Arithmetic Expressions In-class

CMSC 104, Version 8/06 20L10ArithmeticOps.ppt

The Box - Pseudocode (con’t)

Display “Enter the depth: “

Read <depth>

While (<depth> <= 0 )

Display “The depth must be > 0”

Display “Enter the depth: “

Read <depth>

End_while

Page 21: CMSC 104, Version 8/061L10ArithmeticOps.ppt Arithmetic Operators Topics Arithmetic Operators Operator Precedence Evaluating Arithmetic Expressions In-class

CMSC 104, Version 8/06 21L10ArithmeticOps.ppt

The Box - Pseudocode (con’t)

<volume> = <height> X <width> X <depth>

<surface1> = <height> X <width>

<surface2> = <width> X <depth>

<surface3> = <height> X <depth>

<surface area> = 2 X (<surface1> + <surface2>

+ <surface3>)

Page 22: CMSC 104, Version 8/061L10ArithmeticOps.ppt Arithmetic Operators Topics Arithmetic Operators Operator Precedence Evaluating Arithmetic Expressions In-class

CMSC 104, Version 8/06 22L10ArithmeticOps.ppt

The Box - Pseudocode (con’t)

Display “Height = “, <height>

Display “Width = “, <width>

Display “Depth = “, <depth>

Display “Volume = “, <volume>

Display “Surface Area = “, <surface area>

Page 23: CMSC 104, Version 8/061L10ArithmeticOps.ppt Arithmetic Operators Topics Arithmetic Operators Operator Precedence Evaluating Arithmetic Expressions In-class

CMSC 104, Version 8/06 23L10ArithmeticOps.ppt

Good Programming Practice

• It is best not to take the “big bang” approach to coding.

• Use an incremental approach by writing your code in incomplete, yet working, pieces.

• For example, for your projects,o Don’t write the whole program at once.o Just write enough to display the user prompt on

the screen.o Get that part working first (compile and run).o Next, write the part that gets the value from the

user, and then just print it out.

Page 24: CMSC 104, Version 8/061L10ArithmeticOps.ppt Arithmetic Operators Topics Arithmetic Operators Operator Precedence Evaluating Arithmetic Expressions In-class

CMSC 104, Version 8/06 24L10ArithmeticOps.ppt

Good Programming Practice (con’t)

o Get that working (compile and run). o Next, change the code so that you use the

value in a calculation and print out the answer.o Get that working (compile and run). o Continue this process until you have the final

version.o Get the final version working.

• Bottom line: Always have a working version of your program!

Page 25: CMSC 104, Version 8/061L10ArithmeticOps.ppt Arithmetic Operators Topics Arithmetic Operators Operator Precedence Evaluating Arithmetic Expressions In-class

CMSC 104, Version 8/06 25L10ArithmeticOps.ppt

Using the Incremental Approach

• Let’s think about how we could have developed the volume and surface area program incrementally.