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

cs1010
SMART_READER_LITE
LIVE PREVIEW

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

CS1010 Programming Methodology AY18/19 Sem 1 Lecture 1 14 August 2018 Admin Matters Unit 1: What is a Program? Unit 2: Computational Problems & Algorithms Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 O ffi ce Hours: Wed 4-5pm


slide-1
SLIDE 1
slide-2
SLIDE 2

AY18/19 Sem 1

CS1010

Programming Methodology

slide-3
SLIDE 3

Lecture 1

Admin Matters Unit 1: What is a Program? Unit 2: Computational Problems 
 & Algorithms 14 August 2018

slide-4
SLIDE 4

AY18/19 Sem 1

Ooi Wei Tsang

  • oiwt@comp.nus.edu.sg

AS6 05-15

CS1010 Office Hours: Wed 4-5pm

slide-5
SLIDE 5

AY18/19 Sem 1

What will you learn?

slide-6
SLIDE 6

AY18/19 Sem 1

  • Comfortable with reading and developing small

programs to solve a given problem

  • Become proficient with C syntax and language

and associated programming tools (editors, debuggers)

slide-7
SLIDE 7

What do you need to know to be here?

  • Nothing about writing programs
  • Comfortable with using a computer
  • Comfortable with secondary school math and

notations

slide-8
SLIDE 8

Weekly Activities

  • 2-hour lecture: Tues 4-6pm, weekly
  • 2-hour tutorial / lab: up to 15 students per lab,

starting Week 3

slide-9
SLIDE 9

UDLs and TAs

  • UDLs: Your seniors who return to help your

learn in CS1010

  • TAs: PhD students who teach part-time / full-

time

slide-10
SLIDE 10

AY18/19 Sem 1

Open Book Exams

Not a memorisation-based module Any APIs needed will be given

slide-11
SLIDE 11

AY18/19 Sem 1

Module Homepage

https://nus-cs1010.github.io/1819-s1/ lecture notes assignments guides/manuals etc.

slide-12
SLIDE 12

AY18/19 Sem 1

Piazza Q&A

https://piazza.com/class/jkqlna92ju045j

slide-13
SLIDE 13

AY18/19 Sem 1

Important Dates

October 2, 2018 (Midterm) November 27, 2018 (Final)

slide-14
SLIDE 14

AY18/19 Sem 1

Important Dates

(tentative)

October 6, 2018 (PE1) November 10, 2018 (PE2)

slide-15
SLIDE 15

AY18/19 Sem 1

Open Book

Bring any materials you wish Nothing to memorise

slide-16
SLIDE 16

Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%

slide-17
SLIDE 17

CS1010 is not graded on a bell curve

10 20 30 40 A+ A to A- B+ to B B- to C D+ to D F

NOT the actual NUS bell curve

slide-18
SLIDE 18

In CS1010, you will get the grade you deserved, irrespective of

  • ther students in the class.
slide-19
SLIDE 19

AY18/19 Sem 1

Strict policy on plagiarism

Disciplinary action will be taken :(

slide-20
SLIDE 20

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 one at

https://www.github.com/

  • Activate your Piazza account (link emailed to you)
slide-21
SLIDE 21

AY18/19 Sem 1

Policies

  • Read about other CS1010 policies at the module website
  • Using email and Piazza
  • Attendance
  • Late submission
  • Slides / screencast availability
slide-22
SLIDE 22

AY18/19 Sem 1

Some words of advice

slide-23
SLIDE 23

AY18/19 Sem 1

Work hard. Very very hard.

slide-24
SLIDE 24

At NUS SoC, learning is very different from your prior education

slide-25
SLIDE 25

less rote learning; more open-ended problems; more self-learning

slide-26
SLIDE 26

don’t optimise for grades and CAPs

slide-27
SLIDE 27
  • ptimise your skills,

knowledge and experience instead

slide-28
SLIDE 28

invest time to master the tools

(e.g., bash, vim, git, etc) think long term benefits, not how to get things done now

slide-29
SLIDE 29

What is a program? What is a computational problem? What is an algorithm?

slide-30
SLIDE 30

What is a program?

slide-31
SLIDE 31

AY18/19 Sem 1

A Simplified View of a Computer

Central Processing Unit (CPU) Memory

10100101 01001001 data

  • r

instructions data

slide-32
SLIDE 32

Machine Code

  • Instructions that can be interpreted

by a CPU

  • Different CPU architecture may use

different instruction sets

1001010001011110101001010101010100101…

slide-33
SLIDE 33

Assembly Language

  • Uses mnemonic to represent the

instructions such as incr, decr, store, etc., in a more human readable way.

  • Still difficult to write: operation that is

conceptually simple still requires multiple instructions to be issued.

slide-34
SLIDE 34

Higher-Level Language

  • Languages that allow programmers

to express an operation closer to their intention

  • e.g., x = x + y if (y < 0)
slide-35
SLIDE 35

Higher-Level Languages

  • FORTRAN (first popular PL, 1950s)
  • C (1972, still influential today)
  • Many many others
slide-36
SLIDE 36

AY18/19 Sem 1

Compiling High-Level Program to Machine Code

C program compile
 (etc) machine code run

slide-37
SLIDE 37

AY18/19 Sem 1

What will you learn?

slide-38
SLIDE 38

AY18/19 Sem 1

How to write C

slide-39
SLIDE 39

AY18/19 Sem 1

How a C program behaves

slide-40
SLIDE 40

AY18/19 Sem 1

Tools and techniques to help write good and correct C programs

slide-41
SLIDE 41

AY18/19 Sem 1

How to use C to solve computational problems

slide-42
SLIDE 42

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-43
SLIDE 43

What is a computational problem?

slide-44
SLIDE 44

Computational Problems

  • Problems that can be solved step-by-

step by a computer

  • Have well-defined inputs, outputs,

and constraints

slide-45
SLIDE 45

Is 1933091 prime?

slide-46
SLIDE 46

Is there a route from a point A to point B?

slide-47
SLIDE 47

Find a route from a point A to point B

slide-48
SLIDE 48

How many different routes are there from a point A to point B?

slide-49
SLIDE 49

Find the fastest route from a point A to point B

slide-50
SLIDE 50

What is the meaning

  • f life?
slide-51
SLIDE 51

Do I look good in this

  • utfit?
slide-52
SLIDE 52

Computational Problems

  • Problems that can be solved step-by-

step by a computer

  • Have well-defined inputs, outputs,

and constraints

slide-53
SLIDE 53

Find the maximum number

5 9 8 1 3 2

slide-54
SLIDE 54

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-55
SLIDE 55

How to Solve It?

  • Scan the integers one by one
  • Keep track of the “max so far”
  • When we are done scanning, the

“max so far” will be the answer.

slide-56
SLIDE 56

Problem Formulation

  • Input: L = {l0, l1, …, lk-1}
  • Output: m
  • Constraint: m ∈ L and m ≥ li for all i.
slide-57
SLIDE 57

“Scan one-by-one”

  • li : the number being scanned
  • i = 0, 1, … k-1
slide-58
SLIDE 58

“Maximum so far”

  • m: max so far
  • m = l0 initially
slide-59
SLIDE 59

Keeping Track..

  • Compare m with li
  • If m < li, then update m with value of li
slide-60
SLIDE 60

Done Scanning?

  • When we have scanned lk-1
slide-61
SLIDE 61

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

slide-62
SLIDE 62

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-63
SLIDE 63

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

Input: L = { 0, 2, 1}, k = 3

k i li m 3 1 2 3 2 1 2 3 3 ? 2 3

slide-64
SLIDE 64

Variables

  • i, m, and k are called variables
  • They are location in memory that

holds a value

  • You can read a value from a variable
  • You can write a value to a variable
slide-65
SLIDE 65

Assignment

  • We can assign the value of a variable

to a constant (e.g., set i to 1)

  • or to the value of another variable

(e.g., set m to li)

slide-66
SLIDE 66

Comparison

  • We can compare the values of two

variables

  • E.g., i equals k?, li > m?
slide-67
SLIDE 67

Arithmetic Operations

  • We can perform arithmetic operations

(+, -, etc)

  • E.g., set i to i + 1
slide-68
SLIDE 68

Does this algorithm always find the maximum value correctly?

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-69
SLIDE 69

Correctness of Algorithm

  • If an algorithm produces the correct
  • utput for one instance of input, it is

not necessary correct

  • To be correct, it has to produce the

correct output for all possible inputs!

slide-70
SLIDE 70

How to check for correctness?

  • Carefully choose a set of test cases

to try

  • Argue formally about the properties of

the algorithms

slide-71
SLIDE 71

Homework

  • Try out Problem Set 1.1 to 1.3
  • We will discuss in Week 3 !