CSE 115 Introduction to Computer Science I Announcements Lab - - PowerPoint PPT Presentation

cse 115
SMART_READER_LITE
LIVE PREVIEW

CSE 115 Introduction to Computer Science I Announcements Lab - - PowerPoint PPT Presentation

CSE 115 Introduction to Computer Science I Announcements Lab activity #1 -> can finish up in second half of lab next week. This is a one-time, not to be repeated deal. Act now :-) Lab exam #1 -> you will be expected to write 3 short


slide-1
SLIDE 1

CSE 115

Introduction to Computer Science I

slide-2
SLIDE 2

Announcements

Lab activity #1 -> can finish up in second half of lab next

  • week. This is a one-time, not to be repeated deal. Act

now :-) Lab exam #1 -> you will be expected to write 3 short functions (the first of which is *very* straight forward and worth 40/80 points).

slide-3
SLIDE 3

Road map

▶︎ Review ◀ functions and control flow

slide-4
SLIDE 4

Review

slide-5
SLIDE 5

x = 12 y = 14 z = y a = (x + y) - z x < y

True False

Control flow

visualizing selection (if)

x = 12 y = 14 if x < y : z = y a = (x + y) - z

slide-6
SLIDE 6

Control flow

visualizing selection (if-else)

x = 12 y = 14 z = y a = (x + y) - z x < y

True False

x = 12 y = 14 if x < y : z = y else : z = x a = (x + y) - z z = x

slide-7
SLIDE 7

x = 12 y = 14 z = y a = (x + y) - z x < y

True False

x == y

Control flow

visualizing selection (if-elif-else)

x = 12 y = 14 if x < y : z = y elif x == y : z = 0 else : z = x a = (x + y) - z z = 0

True False

z = x

slide-8
SLIDE 8

Road map

Review ▶︎ functions and control flow ◀

slide-9
SLIDE 9

minimum (of 2)

Define a function that takes two values and returns the smaller of the two. For the purposes of this exercise, do not simply use the min built-in function. Q: How might we approach this problem? Brainstorm with your neighbors for a bit, and be prepared to explain (in English, not code) the steps involved.

slide-10
SLIDE 10

minimum (of 2)

Here's one possible approach: def minOf2( x , y ) :

slide-11
SLIDE 11

minimum (of 2)

Here's one possible approach: def minOf2( x , y ) : if ( x < y ) :

slide-12
SLIDE 12

minimum (of 2)

Here's one possible approach: def minOf2( x , y ) : if ( x < y ) : return x

slide-13
SLIDE 13

minimum (of 2)

Here's one possible approach: def minOf2( x , y ) : if ( x < y ) : return x else : return y

slide-14
SLIDE 14

minimum (of 2)

Here's one possible approach: def minOf2( x , y ) : if ( x < y ) : return x else : return y minOf2(4,5) has value 4 minOf2(5,4) has value 4

slide-15
SLIDE 15

minimum (of 3)

Define a function that takes three values and returns the smallest of the three. Q: How might we approach this problem? Brainstorm with your neighbors for a bit, and be prepared to explain (in English, not code) your approach.

slide-16
SLIDE 16

minimum (of 3)

def minOf3( x , y , z ) : if ( x < y < z or x < z < y ) : return x elif ( y < x < z or y < z < x ) : return y else : return z minOf3(4,5,6) has value 4 minOf3(5,4,6) has value 4 minOf3(6,5,4) has value 4

slide-17
SLIDE 17

minimum (of 3)

another approach def minOf3ver2( x , y , z ) : return minOf2(minOf2(x,y),z) minOf3ver2(4,5,6) has value 4 minOf3ver2(5,4,6) has value 4 minOf3ver2(6,5,4) has value 4

slide-18
SLIDE 18

"homework"

(not to hand in, just to get more practice)

Try the next few problems on your own.

slide-19
SLIDE 19

arithmetic mean

Define a function that computes the arithmetic mean of three values. Q: How might we approach this problem? Brainstorm with your neighbors. Be sure to agree on the formula for an arithmetic mean. Be prepared to explain (in English, not code) your approach. If you write code too, that's cool.

slide-20
SLIDE 20

arithmetic mean

def a_mean( x , y , z ) : return ( x + y + z ) / 3

slide-21
SLIDE 21

geometric mean

Define a function that computes the geometric mean of three values. Q: How might we approach this problem? Brainstorm with your neighbors. Be sure to agree on the formula for an geometric mean. Be prepared to explain (in English, not code) your approach. If you write code too, that's cool.

slide-22
SLIDE 22

geometric mean

def g_mean( x , y , z ) : return ( x * y * z ) ** (1/3)

slide-23
SLIDE 23

mean choice

Define a function that computes either the the arithmetic or geometric mean of three values, based on whether the fourth argument to the function is 0 or 1. 0 --> arithmetic mean 1 --> geometric mean Q: How might we approach this problem? Brainstorm with your neighbors. Be sure to agree on the formula for an arithmetic mean. Be prepared to explain (in English, not code) your approach. If you write code too, that's cool.

slide-24
SLIDE 24

choice of mean

# Comment # type == 0 --> arithmetic mean # type == 1 --> geometric mean # could be extended to more means def mean( x , y , z , type ) : if ( type == 0 ) : return a_mean(x,y,z) elif ( type == 1 ) : return g_mean(x,y,z)

Parameterize the type of mean to compute: no change in program is needed. mean(a,b,c,t) vs a_mean(a,b,c) g_mean(a,b,c) Same function is called, but with different arguments. The name of a function is fixed at runtime. The values of arguments can vary at runtime.