Processing math: 12%
+ - 0:00:00
Notes for current slide
Notes for next slide

Lecture 2

16 August 2021

Admin Matters
Unit 3: Functions
Unit 4: Types

AY2122 S1              1

Reminders

  • Keep your mic muted unless you need my attention
  • Use Zoom Chat to discuss/chat among yourselves (I won't pay attention to it)
  • Use Pizza Live Q&A to ask me question during class
AY2122 S1              2

Reminders

  • My office hour: Tues 4-5pm, F2F and Online
  • Questions? Issues? Please use Piazza
AY2122 S1              3

Accounts

  • By now you should have your Piazza, GitHub, and SoC accounts setup and ready to go.
  • Target this week:
    • able to ssh into on PE environments
    • go through Unix CLI and vim tutorials
AY2122 S1              4

Why Unix CLI?

AY2122 S1              5

Why vim?

AY2122 S1              6

via reddit reddit

AY2122 S1              7

Stackoverflow 2021 Developer Survey

reddit

AY2122 S1              8

Tutorials and Labs

  • Starts next week
  • Appeal via ModReg if you still haven't gotten a slot.
  • We might appeal to you to switch rooms to balanced out the groups (please say yes)
AY2122 S1              9

Tutorial 1

  • Problem Set from Unit 2

Lab 1

  • Setup, Unix, vim, Exercise 0
AY2122 S1              10

flowchart
https://xkcd.com/518

AY2122 S1              11

Flowchart

flowchart

AY2122 S1              12

Functions

AY2122 S1              13

flowchart

AY2122 S1              14

flowchart

AY2122 S1              15

Find The Range

  • The range of a given list of k integers is the difference between the max and the min values in the list.

  • Give an algorithm to find the range given a list L of k integers.

AY2122 S1              16

max(L, k) - min(L, k)

AY2122 S1              17

Refer to a previous solution to a sub-problem, which we assume we already know how to solve.

AY2122 S1              18

Wishful Thinking

AY2122 S1              19

Wishful Thinking

  • A powerful tool to help us think at a higher level.
  • We can focus on what a function does, not how it does it.
AY2122 S1              20

A C program is just a collection of functions!

AY2122 S1              21

A C program is just a collection of functions

  • written by you
  • provided by the standard C library
  • provided by other libraries
AY2122 S1              22

Find the Mean

  • Give an algorithm to find the mean value given a list L of k integers.
AY2122 S1              23

\frac{sum(L,k)}{k}

AY2122 S1              24

Assume len(L) returns the size of the list.

\frac{sum(L)}{len(L)}

AY2122 S1              25

Find the Std Dev

  • Give an algorithm to find the standard deviation of a list L of k integers.

\sqrt{\frac{\sum_{i=0}^{k-1}(l_i - \mu)^2}{k}}

AY2122 S1              26

Let's break it down into sub-problems

AY2122 S1              27

\sqrt{\frac{\sum_{i=0}^{k-1}(l_i - \mu)^2}{k}}

AY2122 S1              28

\sum_{i=0}^{k-1}(l_i - \mu)^2

set \mu to mean(L)

set L' to subtract(L, \mu)

set L'' to square(L')

set total to sum(L'')

AY2122 S1              29

\sum_{i=0}^{k-1}(l_i - \mu)^2

sum(square(subtract(L, mean(L))))

AY2122 S1              30

\frac{\sum_{i=0}^{k-1}(l_i - \mu)^2}{k}

mean(square(subtract(L, mean(L))))

AY2122 S1              31

\sqrt\frac{\sum_{i=0}^{k-1}(l_i - \mu)^2}{k}

sqrt(mean(square(subtract(L,mean(L)))))

AY2122 S1              32

flowchart

AY2122 S1              33

Wishful Thinking

  • square(L)
  • subtract(L, \mu)
  • sqrt(x)
AY2122 S1              34

flowchart

AY2122 S1              35

IMPORTANT: Break down a problem into sub-problems and solve them one-by-one with functions. Then compose the functions to solve the original given task.

AY2122 S1              36

Find the maximum among these numbers

5 9 8
1 3 2
AY2122 S1              37

Find the maximum among these numbers

12 20 11 20 14 2 24 36 43 16 27 6 11 7
10 15 38 10 22 7 16 26 32 30 11 9 30 6
20 6 27 19 26 10 27 6 19 34 5 9 3 22
10 4 13 1 10 22 43 36 39 29 41 12 13 25
17 8 30 31 29 38 2 42 7 45 24 33 9 40
34 29 37 2 26 17 19 34 6 7 34 22 21 41
38 5 15 13 9 1 42 39 5 29 38 4 22 29
41 10 26 32 30 26 16 16 18 22 32 34 14 10
5 17 25 16 19 6 31 16 3 13 8 42 41 0
13 30 44 1 41 14 5 39 40 38 6 37 38 9
.
AY2122 S1              38

Compare 12 to the maximum among

20 11 20 14 2 24 36 43 16 27 6 11 7
10 15 38 10 22 7 16 26 32 30 11 9 30 6
20 6 27 19 26 10 27 6 19 34 5 9 3 22
10 4 13 1 10 22 43 36 39 29 41 12 13 25
17 8 30 31 29 38 2 42 7 45 24 33 9 40
34 29 37 2 26 17 19 34 6 7 34 22 21 41
38 5 15 13 9 1 42 39 5 29 38 4 22 29
41 10 26 32 30 26 16 16 18 22 32 34 14 10
5 17 25 16 19 6 31 16 3 13 8 42 41 0
13 30 44 1 41 14 5 39 40 38 6 37 38 9
.
AY2122 S1              39

Wishful Thinking

We know how to find max(L,k) for smaller k

AY2122 S1              40

Let max'(L, i, j) returns the maximum among l_i, l_{i+1} \ldots l_j

So max(L, k) is just max'(L, 0, k-1)

AY2122 S1              41

if i \not = j, then max'(L, i, j) is the larger between

  • l_i and
  • max'(L, i+1, j)
AY2122 S1              42

if i equals j, max'(L, i, j) is just l_i

AY2122 S1              43
AY2122 S1              44

flowchart

AY2122 S1              45

Find the maximum among

12 20 11 20 14 2 24 36 43 16 27 6 11 7
10 15 38 10 22 7 16 26 32 30 11 9 30 6
20 6 27 19 26 10 27 6 19 34 5 9 3 22
10 4 13 1 10 22 43 36 39 29 41 12 13 25
17 8 30 31 29 38 2 42 7 45 24 33 9 40
34 29 37 2 26 17 19 34 6 7 34 22 21 41
38 5 15 13 9 1 42 39 5 29 38 4 22 29
41 10 26 32 30 26 16 16 18 22 32 34 14 10
5 17 25 16 19 6 31 16 3 13 8 42 41 0
13 30 44 1 41 14 5 39 40 38 6 37 38 9
.
AY2122 S1              46

Wishful Thinking

We know how to find max(L,k) for smaller k

AY2122 S1              47

Compare the maximum among these numbers

12 20 11 20 14 2 24
10 15 38 10 22 7 16
20 6 27 19 26 10 27
10 4 13 1 10 22 43
17 8 30 31 29 38 2
34 29 37 2 26 17 19
38 5 15 13 9 1 42
41 10 26 32 30 26 16
5 17 25 16 19 6 31
13 30 44 1 41 14 5
.
AY2122 S1              48

With the maximum among these numbers

36 43 16 27 6 11 7
26 32 30 11 9 30 6
6 19 34 5 9 3 22
36 39 29 41 12 13 25
42 7 45 24 33 9 40
34 6 7 34 22 21 41
39 5 29 38 4 22 29
16 18 22 32 34 14 10
16 3 13 8 42 41 0
39 40 38 6 37 38 9
.
AY2122 S1              49

if j \not= i, max(L,i,j) is the larger between

  • max'(L, i, n) and
  • max'(L, n+1, j)

where n = \left\lfloor\frac{i+j}{2}\right\rfloor

AY2122 S1              50

if j equals i, max(L,i,j) is just l_i.

AY2122 S1              51
AY2122 S1              52

flowchart

AY2122 S1              53
AY2122 S1              54

Divide and Conquer

AY2122 S1              55

Find the factorial of n

  • n! = n \times n-1 \times \ldots \times 2 \times 1

  • n! = n \times (n-1)!

  • Special case: 0! = 1

AY2122 S1              56

flowchart

AY2122 S1              57

Recursion

A function calling itself

AY2122 S1              58

Types

AY2122 S1              59

1010010101010101001111010.. can mean

  • 1933091
  • 34.95108
  • hello
  • an image
AY2122 S1              60

A variable needs a type to be interpreted correctly.

AY2122 S1              61

A type is represented by a fixed, finite, number of bits.

Different types can be represented by a different number of bits.

AY2122 S1              62

1 bit: 1 or 0

black or white
true or false
yes or no
S or U

AY2122 S1              63

2 bits: 11, 10, 01, or 00

north, south, east, west
spring, summer, fall, winter

AY2122 S1              64

In general, k bits can represent 2^k values

AY2122 S1              65

more bits \rightarrow more memory, but can represent bigger range

fewer bits \rightarrow less memory, but represent smaller range

AY2122 S1              66

When writing programs for embedded systems, IoT devices, sensors, etc, you need to be frugal with memory usage.

In CS1010, however, we will go with more bits for better precision.

AY2122 S1              67

Integers

AY2122 S1              68

8 bits: 256 different integers

0 to 255 (for unsigned)
-128 to 127 (for signed)

AY2122 S1              69

64 bits signed

-9,223,372,036,854,775,808
to
9,223,372,036,854,775,807

AY2122 S1              70

Characters

AY2122 S1              71

8 bits: 127 different symbols using ASCII standards

0-9, A-Z, a-z
<>?:”{}!@#$%^&*
()_+-=[]\';/.,

return, tab, escape, etc

AY2122 S1              72

up to 32 bits: Unicode (UTF-8)

11111011000000000 😀

AY2122 S1              73

Real numbers

(also called floating point numbers)

AY2122 S1              74

There are infinitely many real numbers but only a finite sequence of bits.

Not all of real numbers can be represented.

AY2122 S1              75

Not every real number can be presented precisely

E.g., 0.1 + 0.2 is 0.30000000000000004

AY2122 S1              76

foodpanda

AY2122 S1              77

Rule: Use integer type unless you absolutely need to use a real number type.

AY2122 S1              78

We normally use 32, 64, or 128 bits to represent real numbers.

AY2122 S1              79

A variable needs a type to be interpreted correctly.

AY2122 S1              80

set total to sum(L, k)
set avg to mean(L, k)

k is an integer
total is an integer
avg must be a real number

AY2122 S1              81

Homework

  • Problem Set in Unit 3
  • ssh into CS1010 PE hosts and follow the Unix CLI and vim tutorial
  • Diagnostic Quiz for Lecture 2 (due this Wed)
  • Diagnostic Quiz for using CS1010 PE (due next Wed)
AY2122 S1              82

Version: 1.1

Last Updated: Tue Aug 18 12:24:32 +08 2020

AY2122 S1              83

Reminders

  • Keep your mic muted unless you need my attention
  • Use Zoom Chat to discuss/chat among yourselves (I won't pay attention to it)
  • Use Pizza Live Q&A to ask me question during class
AY2122 S1              2
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow