CS1010 Programming Methodology AY18/19 Sem 1 Lecture 2 21 August - - PowerPoint PPT Presentation

cs1010
SMART_READER_LITE
LIVE PREVIEW

CS1010 Programming Methodology AY18/19 Sem 1 Lecture 2 21 August - - PowerPoint PPT Presentation

CS1010 Programming Methodology AY18/19 Sem 1 Lecture 2 21 August 2018 Admin Matters Unit 3: Functions Unit 4: Types Piazza Q&A https://piazza.com/class/jkqlna92ju045j AY18/19 Sem 1 Important Dates October 6 , 2018 (PE1) 0900 - 1200


slide-1
SLIDE 1
slide-2
SLIDE 2

AY18/19 Sem 1

CS1010

Programming Methodology

slide-3
SLIDE 3

Lecture 2

Admin Matters Unit 3: Functions Unit 4: Types 21 August 2018

slide-4
SLIDE 4

AY18/19 Sem 1

Piazza Q&A

https://piazza.com/class/jkqlna92ju045j

slide-5
SLIDE 5

AY18/19 Sem 1

Important Dates

October 6, 2018 (PE1) 0900 - 1200 November 10, 2018 (PE2) 1300 - 1600

slide-6
SLIDE 6

AY18/19 Sem 1

Accounts

  • Register for an SoC UNIX account if you do not

have one at https://mysoc.nus.edu.sg/ ~newacct/

  • Register for a GitHub account if you do not have
  • ne at https://www.github.com/
  • Activate your Piazza account (link emailed to

you)

slide-7
SLIDE 7

Tutorial starts next week

slide-8
SLIDE 8

Balloting / Allocation Issues ?
 contact Mr Chow Yuan Bin

chowyb@comp.nus.edu.sg

slide-9
SLIDE 9

Tutorial 1 Problem Set 1 UNIX walkthrough

slide-10
SLIDE 10

Activate your access to the SoC computer clusters (https:// mysoc.nus.edu.sg/~myacct/ services.cgi), which include the CS1010 programming environment (PE).

slide-11
SLIDE 11

CS1010 PE Hosts

pe111.comp.nus.edu.sg
 pe112.comp.nus.edu.sg
 .. pe120.comp.nus.edu.sg

slide-12
SLIDE 12

Access them via ssh (secure shell)

Mac / Linux: command line
 Windows: XShell

slide-13
SLIDE 13

Try out the UNIX commands:

https://nus-cs1010.github.io/ 1819-s1/unix/

slide-14
SLIDE 14

If you want to get ahead (Tutorial 2), learn vim
 with vimtutor

slide-15
SLIDE 15

Why command line interface (CLI)? Why vim?

slide-16
SLIDE 16

Previously, on CS1010

slide-17
SLIDE 17

AY18/19 Sem 1

A Simplified View of a Computer

Central Processing Unit (CPU) Memory

10100101 01001001 data

  • r

instructions data

slide-18
SLIDE 18

AY18/19 Sem 1

Compiling High-Level Program to Machine Code

C program compile
 (etc) machine code run

slide-19
SLIDE 19

AY18/19 Sem 1

Learning to write a program that does what you want it to do is actually not difficult. Knowing what you want your program to do is the more challenging part!

slide-20
SLIDE 20

Computational Problems

  • Problems that can be solved step-by-

step by a computer

  • Have well-defined inputs, outputs,

and constraints

slide-21
SLIDE 21

The steps to solve a computational problem are called “algorithm”

slide-22
SLIDE 22

Flowchart

set m to l0 set i to 1 i equals k?

  • utput m

YES

is li > m ? set m to li increment i

YES NO NO

input k and l0..lk-1

slide-23
SLIDE 23

Functions

slide-24
SLIDE 24

set m to l0 set i to 1 i equals k?

  • utput m

YES

is li > m ? set m to li increment i

YES NO NO

input k and l0..lk-1

slide-25
SLIDE 25
  • utput max

input k and l0..lk-1

max

  • utput min

input k and l0..lk-1

min

  • utput sum

input k and l0..lk-1

sum

max(L, k) min(L, k) sum(L, k)

slide-26
SLIDE 26

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
slide-27
SLIDE 27

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

slide-28
SLIDE 28

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

slide-29
SLIDE 29

“Wishful Thinking”

slide-30
SLIDE 30

Powerful tool to help us think at a higher level

slide-31
SLIDE 31

Can worry about what a function does. Not how it does it.

slide-32
SLIDE 32

A C program is just a collection of functions.

slide-33
SLIDE 33

Functions written by you Functions provided by the standard C library or other libraries

slide-34
SLIDE 34

Find the mean

  • Give an algorithm to find the mean

value in a given list L of k integers.

slide-35
SLIDE 35

sum(L, k) / k

slide-36
SLIDE 36

Find the Std Dev

  • Give an algorithm to find the standard

deviation of a given list L of k integers.

sPk−1

i=0 (li − µ)2

k

slide-37
SLIDE 37

Break it down to subproblems

slide-38
SLIDE 38

sPk−1

i=0 (li − µ)2

k

slide-39
SLIDE 39

Pk−1

i=0 (li − µ)2

set mu to mean(L, k) set L’ to subtract(L, k, mu) set L’’ to square(L’, k) set total to sum(L’’, k)

slide-40
SLIDE 40

Pk−1

i=0 (li − µ)2

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

slide-41
SLIDE 41

Pk−1

i=0 (li − µ)2

set total to sum(square(subtract(L,k,mean(L,k)),k),k)

slide-42
SLIDE 42

Pk−1

i=0 (li−µ)2

k

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

slide-43
SLIDE 43

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

q Pk−1

i=0 (li−µ)2

k

slide-44
SLIDE 44

square(L, k) subtract(L, k, mu) sqrt(x)

“Wishful Thinking”

slide-45
SLIDE 45

set i to 0 let M be m0..mk-1 i equals k?

  • utput M

YES

set mi to li2 increment i

NO

input k and L = l0..lk-1

slide-46
SLIDE 46

https://xkcd.com/518/

slide-47
SLIDE 47

Find the maximum number

5 9 8 1 3 2

slide-48
SLIDE 48

Find the maximum

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 13 30 44 1 41 14 5 39 40 38 6 37 38 9

slide-49
SLIDE 49

Find the maximum

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 13 30 44 1 41 14 5 39 40 38 6 37 38 9

44 45

slide-50
SLIDE 50

Find the maximum

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 13 30 44 1 41 14 5 39 40 38 6 37 38 9

slide-51
SLIDE 51

Find the maximum

52 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 13 30 44 1 41 14 5 39 40 38 6 37 38 9

45

slide-52
SLIDE 52

max(L, k) for smaller k “Wishful Thinking”

slide-53
SLIDE 53

max’(L, i, j) finds the max value for li … lj

slide-54
SLIDE 54

max’(L, i, j) finds the max value for li … lj


if i equals j then li is the max

slide-55
SLIDE 55

if i does not equals j then max is either li or max’(L, i+1, j)

slide-56
SLIDE 56

i equals j?

  • utput li

NO

is li > m ? set m to max’(L, i+1,j)

YES YES NO

input L, i, j

  • utput m

The function max’(L, i, j)

slide-57
SLIDE 57

Find the Factorial

  • Give n, find n!
  • n! = n x (n-1) x (n-2) x … 2 x 1


= n x (n-1)!

  • Special case: 0! = 1
slide-58
SLIDE 58

n equals 0?

  • utput 1

NO YES

input n

  • utput 


n x factorial(n-1)

The function factorial(n)

slide-59
SLIDE 59

A function calling itself Recursion

slide-60
SLIDE 60

Tutorial 2 Problem Set 3 vim Warm Up Assignment

slide-61
SLIDE 61

Types

slide-62
SLIDE 62

1010010101010101001111010… 1933091

slide-63
SLIDE 63

1010010101010101001111010… 1933091 34.95108

slide-64
SLIDE 64

1010010101010101001111010… 1933091 34.95108 “hello”

slide-65
SLIDE 65

1010010101010101001111010… 1933091 34.95108 “hello”

slide-66
SLIDE 66

Variable needs a type to be interpreted correctly.

slide-67
SLIDE 67

A type is represented a finite number of bits. Can be different number

  • f bits for different types
slide-68
SLIDE 68

1 bit: 1 or 0 black or white true or false yes or no S or U

slide-69
SLIDE 69

2 bits: 11 00 01 10 north, south, east, west spring, summer, fall, winter

slide-70
SLIDE 70

In general, k bits can represent 2k values

slide-71
SLIDE 71

more bits -> use more memory can represent bigger values fewer bits -> less memory, but limited range of values

slide-72
SLIDE 72

When you program embedded systems, IoT, sensors, etc. You need to be frugal with memory usage. Not an issue in CS1010 (we will use 32 or 64 bits)

slide-73
SLIDE 73

Integers

slide-74
SLIDE 74

8 bits: 256 different integers 0 to 255 (for unsigned)

  • 128 to 127 (for signed)
slide-75
SLIDE 75

64 bits: 


  • 9,223,372,036,854,775,808

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

slide-76
SLIDE 76

Characters

slide-77
SLIDE 77

8 bits: 127 different symbols using ASCII standards 0-9, A-Z, a-z <>?:”{}!@#$%^&* ()_+-=[]\';/., return, tab, escape etc

slide-78
SLIDE 78

up to 32 bits: Unicode

11111011000000000 😁

slide-79
SLIDE 79

Real Numbers

(also called floating point numbers due to how it is represented)

slide-80
SLIDE 80

There are infinitely many but

  • nly finite number of

representations with bits

slide-81
SLIDE 81

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

slide-82
SLIDE 82

Variable need a type to be interpreted correctly.

slide-83
SLIDE 83

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