22
APS105 Calculating 1

APS105 Calculating 1. Basic Math Operations 2 Basic Arithmetic Operators Operators: –Addition: + –Subtraction: - –Multiplication: * –Division: / Recall

Embed Size (px)

Citation preview

Page 1: APS105 Calculating 1. Basic Math Operations 2 Basic Arithmetic Operators Operators: –Addition: + –Subtraction: - –Multiplication: * –Division: / Recall

1

APS105

Calculating

Page 2: APS105 Calculating 1. Basic Math Operations 2 Basic Arithmetic Operators Operators: –Addition: + –Subtraction: - –Multiplication: * –Division: / Recall

2

Basic Math Operations

Page 3: APS105 Calculating 1. Basic Math Operations 2 Basic Arithmetic Operators Operators: –Addition: + –Subtraction: - –Multiplication: * –Division: / Recall

3

Basic Arithmetic Operators

• Operators:– Addition: +– Subtraction: -– Multiplication: *– Division: /

• Recall order of operations:– * and / have highest precedence– left to right otherwise

• Speficy grouping/precedence: ( )– Not: { } or [ ]

Page 4: APS105 Calculating 1. Basic Math Operations 2 Basic Arithmetic Operators Operators: –Addition: + –Subtraction: - –Multiplication: * –Division: / Recall

4

Examples

Page 5: APS105 Calculating 1. Basic Math Operations 2 Basic Arithmetic Operators Operators: –Addition: + –Subtraction: - –Multiplication: * –Division: / Recall

5

The Modulo Operator %

• x % y returns the remainder of x / y• has same precedence as / and *• x and y must be ints

– signs of x and y are ignored if either is negative– but result takes the sign of x (is negative if x is)

Page 6: APS105 Calculating 1. Basic Math Operations 2 Basic Arithmetic Operators Operators: –Addition: + –Subtraction: - –Multiplication: * –Division: / Recall

6

Modulo Examples

.

Page 7: APS105 Calculating 1. Basic Math Operations 2 Basic Arithmetic Operators Operators: –Addition: + –Subtraction: - –Multiplication: * –Division: / Recall

7

Calculating with Char Values

• Recall ‘A’ is encoded in ASCII as decimal 65char letter = ‘A’; // letter = 65

• Since char value is a number can do math.

Page 8: APS105 Calculating 1. Basic Math Operations 2 Basic Arithmetic Operators Operators: –Addition: + –Subtraction: - –Multiplication: * –Division: / Recall

8

Increment and Decrement

• increment: ++– adds one to the variable– AND changes the variable to this new value– i++; // means the same as i = i + 1;

i = 10; i++; // i = 11 afterwards

• decrement: --– subtracts one from the variable– AND changes the variable to this new value– i--; // means the same as i = i - 1;

i = 10; i--; // i = 9 afterwards

Page 9: APS105 Calculating 1. Basic Math Operations 2 Basic Arithmetic Operators Operators: –Addition: + –Subtraction: - –Multiplication: * –Division: / Recall

9

Assignment

Page 10: APS105 Calculating 1. Basic Math Operations 2 Basic Arithmetic Operators Operators: –Addition: + –Subtraction: - –Multiplication: * –Division: / Recall

10

Assignment

• An assignment statement contains an ‘=‘• Evaluation order:

– right-hand-side (RHS) of = is evaluated first– Then assignment of result to left-hand-side (LHS)

• Example: x = 5;

x = x + 2;

Page 11: APS105 Calculating 1. Basic Math Operations 2 Basic Arithmetic Operators Operators: –Addition: + –Subtraction: - –Multiplication: * –Division: / Recall

11

Operation-Assignment

• Some operations can be combined with ‘=‘– Ie., +=, -=, *=, /=, %=

• Examples:x += 2;

x += 2 * y;

Page 12: APS105 Calculating 1. Basic Math Operations 2 Basic Arithmetic Operators Operators: –Addition: + –Subtraction: - –Multiplication: * –Division: / Recall

12

Library Functions

Page 13: APS105 Calculating 1. Basic Math Operations 2 Basic Arithmetic Operators Operators: –Addition: + –Subtraction: - –Multiplication: * –Division: / Recall

13

Library Functions

• Library functions:– Commonly-used functions– Packaged together as a “library”– Gain access to them by:

• Including the library in your program• AND telling the compiler to link to the library

• Example: using the math library

Page 14: APS105 Calculating 1. Basic Math Operations 2 Basic Arithmetic Operators Operators: –Addition: + –Subtraction: - –Multiplication: * –Division: / Recall

14

Common <math.h> Functions

• Note: all arguments/return-values are doubles

(absolute value)

Page 15: APS105 Calculating 1. Basic Math Operations 2 Basic Arithmetic Operators Operators: –Addition: + –Subtraction: - –Multiplication: * –Division: / Recall

15

Random Numbers

• Generating truly random nums is hard!– Eg., gambling machine pattern discovered– Why?

• Most programs use “pseudo-random” nums– not perfectly random, close enough

• rand() function– in program (at top): #include <stdlib.h>– rand() returns an integer within 0..RAND_MAX

Page 16: APS105 Calculating 1. Basic Math Operations 2 Basic Arithmetic Operators Operators: –Addition: + –Subtraction: - –Multiplication: * –Division: / Recall

16

Example using rand()

• Generate a random int between 0 and 1

• Generate a random int between 0 and 100

• Generate a random int between 1 and 10

• Generate a random even int bet. 2 and 10

Page 17: APS105 Calculating 1. Basic Math Operations 2 Basic Arithmetic Operators Operators: –Addition: + –Subtraction: - –Multiplication: * –Division: / Recall

17

Mixing Types and Casting

Page 18: APS105 Calculating 1. Basic Math Operations 2 Basic Arithmetic Operators Operators: –Addition: + –Subtraction: - –Multiplication: * –Division: / Recall

18

Mixing Types

• If multiple types are used in an expression– the wider (more accurate) type is “contagious”

• Example1:2 + 5.3 .

Page 19: APS105 Calculating 1. Basic Math Operations 2 Basic Arithmetic Operators Operators: –Addition: + –Subtraction: - –Multiplication: * –Division: / Recall

19

Mixing Types: Example2

8 / 3 + 1.0 //

Page 20: APS105 Calculating 1. Basic Math Operations 2 Basic Arithmetic Operators Operators: –Addition: + –Subtraction: - –Multiplication: * –Division: / Recall

20

Casting

• Casting lets you change one type into another– use casting to get the result type you want!

int x = (int) 5.7; //

• Precedence of cast: – higher than * /– lower than ++ --

Page 21: APS105 Calculating 1. Basic Math Operations 2 Basic Arithmetic Operators Operators: –Addition: + –Subtraction: - –Multiplication: * –Division: / Recall

21

Cast Examples

int x = (int) 5.7 + 8.9;

int x = (int) (5.7 + 8.9);

Page 22: APS105 Calculating 1. Basic Math Operations 2 Basic Arithmetic Operators Operators: –Addition: + –Subtraction: - –Multiplication: * –Division: / Recall

22

More Cast Examples

int x = (3 / 4) * 8; //