Cpts121-2-2 - CptS 121 Fall 09 Lecture 2-2 HK Chapter 2 Numeric Expressions Lecture Outline I II III IV Arithmetic Expressions Number Formatting

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
1 CptS 121 L2-2 – 9/2/09 Prof. Chris Hundhausen CptS 121 Fall ‘09 Lecture 2-2 HK Chapter 2: Numeric Expressions Lecture Outline I. Arithmetic Expressions II. Number Formatting III. Input and Output IV. Common Errors
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 CptS 121 L2-2 – 9/2/09 Prof. Chris Hundhausen Arithmetic Expressions Expressions involving operators ( + , -, *, /, % ) and int or double operands The result of / differs depending on whether it is applied to ints or doubles: 9/2 equals 4, but 9.0/4.0 equals 4.5 % ("mod" or "remainder") operator can be applied to int s: %: what is left over when you divide one number by another Undefined when the divisor is 0 Examples: - 9 % 2 equals 1 (the remainder when you divide 9 by 2) - 8 % 3 equals 2 (the remainder when you divide 8 by 3) - 299 % 150 equals 149 (the remainder when you divide 299 by 150)
Background image of page 2
3 CptS 121 L2-2 – 9/2/09 Prof. Chris Hundhausen Distinction Between Integer and Floating Point Arithmetic If all operands are int s, then result will be an int automatic conversion to double will occur if variable to which we're assigning the result is a double Example: double x; x = 5 + 3; /* x == 8.0 */
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 CptS 121 L2-2 – 9/2/09 Prof. Chris Hundhausen Distinction Between Integer and Floating Point Arithmetic (cont.) If all operands are doubles, then result will be a double Automatic conversion to int will occur if variable to which we're assigning the result is an int Example: int x; x = 7 / 2; /* x == 3 -- .5 is truncated */ x = 3 * 0.5; /* x == 1 -- .5 is truncated */
Background image of page 4
5 CptS 121 L2-2 – 9/2/09 Prof. Chris Hundhausen Type Casts Convert an expression of one type to another type Place desired type in parentheses prior to expression you want to convert Examples double average; int sum, num_students; /* Type cast num_students so that fractional portion of division is not
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 CptS 121 L2-2 – 9/2/09 Prof. Chris Hundhausen Type Cast Example double x; int truncated_x; int rounded_x; x = 3.5; /* Typecast x to int, forcing fractional part to be truncated */ truncated_x = (int) x; /* Now add 0.5 to x before performing the typecast. This is the equivalent of rounding x. */ rounded_x = (int) (x + 0.5);
Background image of page 6
7 CptS 121 L2-2 – 9/2/09
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/25/2011 for the course CPTS 121 taught by Professor Hundhaus during the Spring '09 term at Washington State University .

Page1 / 21

Cpts121-2-2 - CptS 121 Fall 09 Lecture 2-2 HK Chapter 2 Numeric Expressions Lecture Outline I II III IV Arithmetic Expressions Number Formatting

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online