Lecture 4 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/... · 2018. 9. 6. · Lecture 4 Admin...

42

Transcript of Lecture 4 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/... · 2018. 9. 6. · Lecture 4 Admin...

Page 1: Lecture 4 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/... · 2018. 9. 6. · Lecture 4 Admin Matters Unit 8: If Else Unit 9: Logical Expression Unit 10: Assertion 4 September
Page 2: Lecture 4 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/... · 2018. 9. 6. · Lecture 4 Admin Matters Unit 8: If Else Unit 9: Logical Expression Unit 10: Assertion 4 September

Lecture 4

Admin MattersUnit 8: If Else

Unit 9: Logical Expression Unit 10: Assertion

4 September 2018

Page 3: Lecture 4 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/... · 2018. 9. 6. · Lecture 4 Admin Matters Unit 8: If Else Unit 9: Logical Expression Unit 10: Assertion 4 September

AY18/19 Sem 1

How to do badly in CS1010

Page 4: Lecture 4 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/... · 2018. 9. 6. · Lecture 4 Admin Matters Unit 8: If Else Unit 9: Logical Expression Unit 10: Assertion 4 September

AY18/19 Sem 1

1. Ignore what Wei Tsang advised or announced

during lecture

Page 5: Lecture 4 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/... · 2018. 9. 6. · Lecture 4 Admin Matters Unit 8: If Else Unit 9: Logical Expression Unit 10: Assertion 4 September

AY18/19 Sem 1

2. Do not read / pay attention

to the guides and notes posted on CS1010 website

Page 6: Lecture 4 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/... · 2018. 9. 6. · Lecture 4 Admin Matters Unit 8: If Else Unit 9: Logical Expression Unit 10: Assertion 4 September

AY18/19 Sem 1

3. Visit Piazza only when you have a question to

ask yourself

Page 7: Lecture 4 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/... · 2018. 9. 6. · Lecture 4 Admin Matters Unit 8: If Else Unit 9: Logical Expression Unit 10: Assertion 4 September

AY18/19 Sem 1

4. Wait for others to ask

questions

Page 8: Lecture 4 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/... · 2018. 9. 6. · Lecture 4 Admin Matters Unit 8: If Else Unit 9: Logical Expression Unit 10: Assertion 4 September

AY18/19 Sem 1

5. Wait for solutions from UDLs instead of trying the problems yourself.

Page 9: Lecture 4 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/... · 2018. 9. 6. · Lecture 4 Admin Matters Unit 8: If Else Unit 9: Logical Expression Unit 10: Assertion 4 September

AY18/19 Sem 1

6. Expect assignments / tests

/ exams to be similar to problem sets / exercises

Page 10: Lecture 4 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/... · 2018. 9. 6. · Lecture 4 Admin Matters Unit 8: If Else Unit 9: Logical Expression Unit 10: Assertion 4 September

AY18/19 Sem 1

7. Copy-paste sample code

from lecture instead of writing it out yourself

Page 11: Lecture 4 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/... · 2018. 9. 6. · Lecture 4 Admin Matters Unit 8: If Else Unit 9: Logical Expression Unit 10: Assertion 4 September

AY18/19 Sem 1

Tutorial 3

Problem Sets from Unit 8-9 Today

Page 12: Lecture 4 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/... · 2018. 9. 6. · Lecture 4 Admin Matters Unit 8: If Else Unit 9: Logical Expression Unit 10: Assertion 4 September

AY18/19 Sem 1

Assignment 1

Arithmetic Ops Recursive Function

If-Else

Page 13: Lecture 4 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/... · 2018. 9. 6. · Lecture 4 Admin Matters Unit 8: If Else Unit 9: Logical Expression Unit 10: Assertion 4 September

AY18/19 Sem 1

Assignment 1

Released this Friday Due next Friday

Page 14: Lecture 4 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/... · 2018. 9. 6. · Lecture 4 Admin Matters Unit 8: If Else Unit 9: Logical Expression Unit 10: Assertion 4 September

AY18/19 Sem 1

Practical Exam 1

Your venue will be announced on Piazza

soon. Acclimatize yourself.

Page 15: Lecture 4 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/... · 2018. 9. 6. · Lecture 4 Admin Matters Unit 8: If Else Unit 9: Logical Expression Unit 10: Assertion 4 September

AY18/19 Sem 1

Midterm

Venue: MPSH 1 (B) 2 October 4pm - 6pm

Page 16: Lecture 4 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/... · 2018. 9. 6. · Lecture 4 Admin Matters Unit 8: If Else Unit 9: Logical Expression Unit 10: Assertion 4 September

AY18/19 Sem 1

Catch Up Session

This Saturday UNIX / vim (the basic)

Page 17: Lecture 4 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/... · 2018. 9. 6. · Lecture 4 Admin Matters Unit 8: If Else Unit 9: Logical Expression Unit 10: Assertion 4 September

AY18/19 Sem 1

Catch Up Session

Must have read through the UNIX

tutorial and have gone through vimtutor.

Page 18: Lecture 4 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/... · 2018. 9. 6. · Lecture 4 Admin Matters Unit 8: If Else Unit 9: Logical Expression Unit 10: Assertion 4 September

AY18/19 Sem 1

Catch Up Session

Please register online by end of tomorrow.

(see Piazza post)

Page 19: Lecture 4 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/... · 2018. 9. 6. · Lecture 4 Admin Matters Unit 8: If Else Unit 9: Logical Expression Unit 10: Assertion 4 September

AY18/19 Sem 1

Reminder

Use XShell on the PCs in our labs to ssh into

the PE hosts and code.

Page 20: Lecture 4 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/... · 2018. 9. 6. · Lecture 4 Admin Matters Unit 8: If Else Unit 9: Logical Expression Unit 10: Assertion 4 September

AY18/19 Sem 1

Reminder

Read plagiarism and late submission policy on the

CS1010 website.

Page 21: Lecture 4 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/... · 2018. 9. 6. · Lecture 4 Admin Matters Unit 8: If Else Unit 9: Logical Expression Unit 10: Assertion 4 September

AY18/19 Sem 1

Readings

Overview about your programming assignments.

Page 22: Lecture 4 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/... · 2018. 9. 6. · Lecture 4 Admin Matters Unit 8: If Else Unit 9: Logical Expression Unit 10: Assertion 4 September

AY18/19 Sem 1

https://kahoot.it

Page 23: Lecture 4 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/... · 2018. 9. 6. · Lecture 4 Admin Matters Unit 8: If Else Unit 9: Logical Expression Unit 10: Assertion 4 September

long square(long x) { return x*x;}

double hypotenuse_of(long base, long height) { return sqrt(square(base) + square(height));}

int main() { : hypotenuse = hypotenuse_of(base, height); :}

Page 24: Lecture 4 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/... · 2018. 9. 6. · Lecture 4 Admin Matters Unit 8: If Else Unit 9: Logical Expression Unit 10: Assertion 4 September

Where are we in CS1010?

Behavioural / Mental model Tools / Good Practice

Problem Solving C language / syntax

Page 25: Lecture 4 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/... · 2018. 9. 6. · Lecture 4 Admin Matters Unit 8: If Else Unit 9: Logical Expression Unit 10: Assertion 4 September

Where are we in CS1010?

Behavioural / Mental model Tools / Good Practice

decompositionrecursionflowchart

Problem Solving C language / syntax

Page 26: Lecture 4 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/... · 2018. 9. 6. · Lecture 4 Admin Matters Unit 8: If Else Unit 9: Logical Expression Unit 10: Assertion 4 September

Where are we in CS1010?

Behavioural / Mental model Tools / Good Practice

decompositionrecursionflowchart

types in Cfunctions in Carithmetic ops

Problem Solving C language / syntax

Page 27: Lecture 4 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/... · 2018. 9. 6. · Lecture 4 Admin Matters Unit 8: If Else Unit 9: Logical Expression Unit 10: Assertion 4 September

Where are we in CS1010?

Behavioural / Mental model Tools / Good Practice

decompositionrecursionflowchart

types in Cfunctions in Carithmetic ops

machine codedata in memory

types

Problem Solving C language / syntax

Page 28: Lecture 4 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/... · 2018. 9. 6. · Lecture 4 Admin Matters Unit 8: If Else Unit 9: Logical Expression Unit 10: Assertion 4 September

Where are we in CS1010?

Problem Solving C language / syntax

Behavioural / Mental model Tools / Good Practice

decompositionrecursionflowchart

types in Cfunctions in Carithmetic ops

machine codedata in memory

types

clangvim

bash

Page 29: Lecture 4 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/... · 2018. 9. 6. · Lecture 4 Admin Matters Unit 8: If Else Unit 9: Logical Expression Unit 10: Assertion 4 September

Today

Problem Solving C language / syntax

Behavioural / Mental model Tools / Good Practice

decompositionrecursionflowchart

conditionalsassertion

types in Cfunctions in Carithmetic ops

if elselogical expressions

machine codedata in memory

types

clangvim

bash

Page 30: Lecture 4 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/... · 2018. 9. 6. · Lecture 4 Admin Matters Unit 8: If Else Unit 9: Logical Expression Unit 10: Assertion 4 September

Lecture 4

Admin MattersUnit 8: If Else

Unit 9: Logical Expression Unit 10: Assertion

4 September 2018

Page 31: Lecture 4 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/... · 2018. 9. 6. · Lecture 4 Admin Matters Unit 8: If Else Unit 9: Logical Expression Unit 10: Assertion 4 September

m = l0 i = 1

i equals k?

return m

YES

is li > m ? m = li i += 1YESNO

NO

input k and l0..lk-1

Page 32: Lecture 4 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/... · 2018. 9. 6. · Lecture 4 Admin Matters Unit 8: If Else Unit 9: Logical Expression Unit 10: Assertion 4 September

n equals 0?

return 1

NO

YES

input n

return n * factorial(n-1)

Page 33: Lecture 4 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/... · 2018. 9. 6. · Lecture 4 Admin Matters Unit 8: If Else Unit 9: Logical Expression Unit 10: Assertion 4 September

long factorial(long n) { if (n == 0) { return 1; } return n * factorial(n - 1);}

Page 34: Lecture 4 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/... · 2018. 9. 6. · Lecture 4 Admin Matters Unit 8: If Else Unit 9: Logical Expression Unit 10: Assertion 4 September

AY18/19 Sem 1

Need to reason about all possibilities

Page 35: Lecture 4 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/... · 2018. 9. 6. · Lecture 4 Admin Matters Unit 8: If Else Unit 9: Logical Expression Unit 10: Assertion 4 September

Score Letter Grade

8 or higher A

Less than 8but 5 or higher B

Less than 5 but 3 or higher C

Less than 3 D

Page 36: Lecture 4 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/... · 2018. 9. 6. · Lecture 4 Admin Matters Unit 8: If Else Unit 9: Logical Expression Unit 10: Assertion 4 September

Score Letter Grade

5 or higher B

Less than 5 See Table 2

Score Letter Grade

8 or higher A

Less than 8 See Table 1

Table 1 (Less than 8)

Page 37: Lecture 4 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/... · 2018. 9. 6. · Lecture 4 Admin Matters Unit 8: If Else Unit 9: Logical Expression Unit 10: Assertion 4 September

Score Letter Grade

5 or higher B

Less than 5 See Table 2

Table 1 (Less than 8)

Table 2 (Less than 5)

Score Letter Grade

3 or higher C

Less than 3 D

Page 38: Lecture 4 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/... · 2018. 9. 6. · Lecture 4 Admin Matters Unit 8: If Else Unit 9: Logical Expression Unit 10: Assertion 4 September

if (score >= 8) if (late_penalty != 0) cs1010_println_string("late submission”);else cs1010_println_string("you can do better!”);

Page 39: Lecture 4 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/... · 2018. 9. 6. · Lecture 4 Admin Matters Unit 8: If Else Unit 9: Logical Expression Unit 10: Assertion 4 September

if (score >= 8) { if (late_penalty != 0) { cs1010_println_string("late submission”); } else { cs1010_println_string("you can do better!”); }}

if (score >= 8) { if (late_penalty != 0) { cs1010_println_string("late submission”); }} else { cs1010_println_string("you can do better!”);}

Page 40: Lecture 4 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/... · 2018. 9. 6. · Lecture 4 Admin Matters Unit 8: If Else Unit 9: Logical Expression Unit 10: Assertion 4 September

AY18/19 Sem 1

The bool data type can take two values

true or false

Page 41: Lecture 4 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/... · 2018. 9. 6. · Lecture 4 Admin Matters Unit 8: If Else Unit 9: Logical Expression Unit 10: Assertion 4 September

AY18/19 Sem 1

#include <stdbool.h>

Page 42: Lecture 4 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/... · 2018. 9. 6. · Lecture 4 Admin Matters Unit 8: If Else Unit 9: Logical Expression Unit 10: Assertion 4 September

&&||!