Top Banner
53

Lecture 5 - NUS Computing - Home · Lecture 5 Admin Matters Common C Mistakes Unit 9: Logical Expression Unit 10: Assertion Unit 11: Loops Unit 12: Reasoning about Loops 11 September

May 31, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Lecture 5 - NUS Computing - Home · Lecture 5 Admin Matters Common C Mistakes Unit 9: Logical Expression Unit 10: Assertion Unit 11: Loops Unit 12: Reasoning about Loops 11 September
Page 2: Lecture 5 - NUS Computing - Home · Lecture 5 Admin Matters Common C Mistakes Unit 9: Logical Expression Unit 10: Assertion Unit 11: Loops Unit 12: Reasoning about Loops 11 September

Lecture 5

Admin MattersCommon C Mistakes

Unit 9: Logical Expression Unit 10: Assertion

Unit 11: Loops Unit 12: Reasoning about Loops

11 September 2018

Page 3: Lecture 5 - NUS Computing - Home · Lecture 5 Admin Matters Common C Mistakes Unit 9: Logical Expression Unit 10: Assertion Unit 11: Loops Unit 12: Reasoning about Loops 11 September

AY18/19 Sem 1

Online Quizzes now available

Page 4: Lecture 5 - NUS Computing - Home · Lecture 5 Admin Matters Common C Mistakes Unit 9: Logical Expression Unit 10: Assertion Unit 11: Loops Unit 12: Reasoning about Loops 11 September

AY18/19 Sem 1

Source code from Lecture 4

now available

Page 5: Lecture 5 - NUS Computing - Home · Lecture 5 Admin Matters Common C Mistakes Unit 9: Logical Expression Unit 10: Assertion Unit 11: Loops Unit 12: Reasoning about Loops 11 September

AY18/19 Sem 1

Tutorial 4

Problem Sets from Unit 10-12 Today

Page 6: Lecture 5 - NUS Computing - Home · Lecture 5 Admin Matters Common C Mistakes Unit 9: Logical Expression Unit 10: Assertion Unit 11: Loops Unit 12: Reasoning about Loops 11 September

AY18/19 Sem 1

Assignment 1

Due this Friday 6pm

Page 7: Lecture 5 - NUS Computing - Home · Lecture 5 Admin Matters Common C Mistakes Unit 9: Logical Expression Unit 10: Assertion Unit 11: Loops Unit 12: Reasoning about Loops 11 September

AY18/19 Sem 1

Assignment 2

Release this Friday (to be graded on

correctness and style)

Page 8: Lecture 5 - NUS Computing - Home · Lecture 5 Admin Matters Common C Mistakes Unit 9: Logical Expression Unit 10: Assertion Unit 11: Loops Unit 12: Reasoning about Loops 11 September

AY18/19 Sem 1

Assignment 2

Everything up to loops

Page 9: Lecture 5 - NUS Computing - Home · Lecture 5 Admin Matters Common C Mistakes Unit 9: Logical Expression Unit 10: Assertion Unit 11: Loops Unit 12: Reasoning about Loops 11 September

AY18/19 Sem 1

Midterm

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

Page 10: Lecture 5 - NUS Computing - Home · Lecture 5 Admin Matters Common C Mistakes Unit 9: Logical Expression Unit 10: Assertion Unit 11: Loops Unit 12: Reasoning about Loops 11 September

AY18/19 Sem 1

Midterm

Open Book Lecture 1 to 5

Page 11: Lecture 5 - NUS Computing - Home · Lecture 5 Admin Matters Common C Mistakes Unit 9: Logical Expression Unit 10: Assertion Unit 11: Loops Unit 12: Reasoning about Loops 11 September

AY18/19 Sem 1

Previously on CS1010..

Page 12: Lecture 5 - NUS Computing - Home · Lecture 5 Admin Matters Common C Mistakes Unit 9: Logical Expression Unit 10: Assertion Unit 11: Loops Unit 12: Reasoning about Loops 11 September

AY18/19 Sem 1

Functions

A C program is a collection of functions.

A function is a black box with input(s) and output

Page 13: Lecture 5 - NUS Computing - Home · Lecture 5 Admin Matters Common C Mistakes Unit 9: Logical Expression Unit 10: Assertion Unit 11: Loops Unit 12: Reasoning about Loops 11 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 14: Lecture 5 - NUS Computing - Home · Lecture 5 Admin Matters Common C Mistakes Unit 9: Logical Expression Unit 10: Assertion Unit 11: Loops Unit 12: Reasoning about Loops 11 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); :}

function

function

function

Page 15: Lecture 5 - NUS Computing - Home · Lecture 5 Admin Matters Common C Mistakes Unit 9: Logical Expression Unit 10: Assertion Unit 11: Loops Unit 12: Reasoning about Loops 11 September

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

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

double hypotenuse = hypotenuse_of(base, height);

int main() { :}

nota function

Page 16: Lecture 5 - NUS Computing - Home · Lecture 5 Admin Matters Common C Mistakes Unit 9: Logical Expression Unit 10: Assertion Unit 11: Loops Unit 12: Reasoning about Loops 11 September

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

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

double hypotenuse;

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

nota function

Page 17: Lecture 5 - NUS Computing - Home · Lecture 5 Admin Matters Common C Mistakes Unit 9: Logical Expression Unit 10: Assertion Unit 11: Loops Unit 12: Reasoning about Loops 11 September

int main() { double hypotenuse_of(long base, long height) { long square(long x) { return x*x; } return sqrt(square(base) + square(height)); }

double hypotenuse = hypotenuse_of(base, height);}

No function within a function

Page 18: Lecture 5 - NUS Computing - Home · Lecture 5 Admin Matters Common C Mistakes Unit 9: Logical Expression Unit 10: Assertion Unit 11: Loops Unit 12: Reasoning about Loops 11 September

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

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

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

function

function

function

Not a “black box”

Page 19: Lecture 5 - NUS Computing - Home · Lecture 5 Admin Matters Common C Mistakes Unit 9: Logical Expression Unit 10: Assertion Unit 11: Loops Unit 12: Reasoning about Loops 11 September

AY18/19 Sem 1

example code in class

x = cs1010_read_long();

what (some of) you wrote

cs1010_read_long(x);x = cs1010_input_long();

:

Page 20: Lecture 5 - NUS Computing - Home · Lecture 5 Admin Matters Common C Mistakes Unit 9: Logical Expression Unit 10: Assertion Unit 11: Loops Unit 12: Reasoning about Loops 11 September

AY18/19 Sem 1

example code in class

cs1010_print_long(x);

what (some of) you wrote

cs1010_print_long(long x);cs1010_print_long(“x”);

:

Page 21: Lecture 5 - NUS Computing - Home · Lecture 5 Admin Matters Common C Mistakes Unit 9: Logical Expression Unit 10: Assertion Unit 11: Loops Unit 12: Reasoning about Loops 11 September

AY18/19 Sem 1

learn by example and

“imitate” other code

Page 22: Lecture 5 - NUS Computing - Home · Lecture 5 Admin Matters Common C Mistakes Unit 9: Logical Expression Unit 10: Assertion Unit 11: Loops Unit 12: Reasoning about Loops 11 September

AY18/19 Sem 1

when in doubt, see how it is done in

the notes

Page 23: Lecture 5 - NUS Computing - Home · Lecture 5 Admin Matters Common C Mistakes Unit 9: Logical Expression Unit 10: Assertion Unit 11: Loops Unit 12: Reasoning about Loops 11 September

AY18/19 Sem 1

Previously on CS1010..

Page 24: Lecture 5 - NUS Computing - Home · Lecture 5 Admin Matters Common C Mistakes Unit 9: Logical Expression Unit 10: Assertion Unit 11: Loops Unit 12: Reasoning about Loops 11 September

Lecture 4

Admin MattersUnit 8: If Else

Unit 9: Logical Expression Unit 10: Assertion

4 September 2018

Page 25: Lecture 5 - NUS Computing - Home · Lecture 5 Admin Matters Common C Mistakes Unit 9: Logical Expression Unit 10: Assertion Unit 11: Loops Unit 12: Reasoning about Loops 11 September

AY18/19 Sem 1

The bool data type can take two values

true or false

Page 26: Lecture 5 - NUS Computing - Home · Lecture 5 Admin Matters Common C Mistakes Unit 9: Logical Expression Unit 10: Assertion Unit 11: Loops Unit 12: Reasoning about Loops 11 September

AY18/19 Sem 1

#include <stdbool.h>

bool is_gen_z(long year){ return ((year >= 1995) && (year <= 2005));}

bool is_not_gen_z(long year){ return ((year < 1995) || (year > 2005));}

Page 27: Lecture 5 - NUS Computing - Home · Lecture 5 Admin Matters Common C Mistakes Unit 9: Logical Expression Unit 10: Assertion Unit 11: Loops Unit 12: Reasoning about Loops 11 September

&&||!

Page 28: Lecture 5 - NUS Computing - Home · Lecture 5 Admin Matters Common C Mistakes Unit 9: Logical Expression Unit 10: Assertion Unit 11: Loops Unit 12: Reasoning about Loops 11 September

Short-circuiting

a && b

a || b

Page 29: Lecture 5 - NUS Computing - Home · Lecture 5 Admin Matters Common C Mistakes Unit 9: Logical Expression Unit 10: Assertion Unit 11: Loops Unit 12: Reasoning about Loops 11 September

if (number < 100000 && is_prime(number)) { :}

Page 30: Lecture 5 - NUS Computing - Home · Lecture 5 Admin Matters Common C Mistakes Unit 9: Logical Expression Unit 10: Assertion Unit 11: Loops Unit 12: Reasoning about Loops 11 September

if (is_prime(number) && number < 100000) { :}

Page 31: Lecture 5 - NUS Computing - Home · Lecture 5 Admin Matters Common C Mistakes Unit 9: Logical Expression Unit 10: Assertion Unit 11: Loops Unit 12: Reasoning about Loops 11 September

De Morgan’s Law

Page 32: Lecture 5 - NUS Computing - Home · Lecture 5 Admin Matters Common C Mistakes Unit 9: Logical Expression Unit 10: Assertion Unit 11: Loops Unit 12: Reasoning about Loops 11 September

!(e1 && e2)

(!e1) || (!e2)

is the same as

Page 33: Lecture 5 - NUS Computing - Home · Lecture 5 Admin Matters Common C Mistakes Unit 9: Logical Expression Unit 10: Assertion Unit 11: Loops Unit 12: Reasoning about Loops 11 September

!(e1 || e2)

(!e1) && (!e2)

is the same as

Page 34: Lecture 5 - NUS Computing - Home · Lecture 5 Admin Matters Common C Mistakes Unit 9: Logical Expression Unit 10: Assertion Unit 11: Loops Unit 12: Reasoning about Loops 11 September

!((year >= 1995) && (year <= 2005))

is the same as

Page 35: Lecture 5 - NUS Computing - Home · Lecture 5 Admin Matters Common C Mistakes Unit 9: Logical Expression Unit 10: Assertion Unit 11: Loops Unit 12: Reasoning about Loops 11 September

4 questions for writing loops

Page 36: Lecture 5 - NUS Computing - Home · Lecture 5 Admin Matters Common C Mistakes Unit 9: Logical Expression Unit 10: Assertion Unit 11: Loops Unit 12: Reasoning about Loops 11 September

1. what do we want

to repeat?

Page 37: Lecture 5 - NUS Computing - Home · Lecture 5 Admin Matters Common C Mistakes Unit 9: Logical Expression Unit 10: Assertion Unit 11: Loops Unit 12: Reasoning about Loops 11 September

set m to l0 set i to 1

i equals k?

output m

YES

is li > m ? set m to li increment iYESNO

NO

input k and l0..lk-1

what do we want to repeat?

Page 38: Lecture 5 - NUS Computing - Home · Lecture 5 Admin Matters Common C Mistakes Unit 9: Logical Expression Unit 10: Assertion Unit 11: Loops Unit 12: Reasoning about Loops 11 September

2. what do we need to set

up before repeating?

Page 39: Lecture 5 - NUS Computing - Home · Lecture 5 Admin Matters Common C Mistakes Unit 9: Logical Expression Unit 10: Assertion Unit 11: Loops Unit 12: Reasoning about Loops 11 September

set m to l0 set i to 1

i equals k?

output m

YES

is li > m ? set m to li increment iYESNO

NO

input k and l0..lk-1

what do we need to set up?

Page 40: Lecture 5 - NUS Computing - Home · Lecture 5 Admin Matters Common C Mistakes Unit 9: Logical Expression Unit 10: Assertion Unit 11: Loops Unit 12: Reasoning about Loops 11 September

3. what changes from one repetition to another?

Page 41: Lecture 5 - NUS Computing - Home · Lecture 5 Admin Matters Common C Mistakes Unit 9: Logical Expression Unit 10: Assertion Unit 11: Loops Unit 12: Reasoning about Loops 11 September

set m to l0 set i to 1

i equals k?

output m

YES

is li > m ? set m to li increment iYESNO

NO

input k and l0..lk-1

what changes?

Page 42: Lecture 5 - NUS Computing - Home · Lecture 5 Admin Matters Common C Mistakes Unit 9: Logical Expression Unit 10: Assertion Unit 11: Loops Unit 12: Reasoning about Loops 11 September

4. when to stop repeating?

Page 43: Lecture 5 - NUS Computing - Home · Lecture 5 Admin Matters Common C Mistakes Unit 9: Logical Expression Unit 10: Assertion Unit 11: Loops Unit 12: Reasoning about Loops 11 September

set m to l0 set i to 1

i equals k?

output m

YES

is li > m ? set m to li increment iYESNO

NO

input k and l0..lk-1

when to stop?

Page 44: Lecture 5 - NUS Computing - Home · Lecture 5 Admin Matters Common C Mistakes Unit 9: Logical Expression Unit 10: Assertion Unit 11: Loops Unit 12: Reasoning about Loops 11 September

i >= 2? factorial *= iYES

NO

factorial = n i = n - 1 i -= 1 output

factorial

input n

Page 45: Lecture 5 - NUS Computing - Home · Lecture 5 Admin Matters Common C Mistakes Unit 9: Logical Expression Unit 10: Assertion Unit 11: Loops Unit 12: Reasoning about Loops 11 September

i >= 2? factorial *= iYES

NO

factorial = n i = n - 1 i -= 1 output

factorial

input n

condition loop body

YES

NO

…initialize update

Page 46: Lecture 5 - NUS Computing - Home · Lecture 5 Admin Matters Common C Mistakes Unit 9: Logical Expression Unit 10: Assertion Unit 11: Loops Unit 12: Reasoning about Loops 11 September

condition loop body

YES

NO

…initialize update

for (<initialize>; <condition>; <update>) { <body>}

Page 47: Lecture 5 - NUS Computing - Home · Lecture 5 Admin Matters Common C Mistakes Unit 9: Logical Expression Unit 10: Assertion Unit 11: Loops Unit 12: Reasoning about Loops 11 September

condition loop body

YES

NO

…initialize update

while (<condition>) { <body>}

Page 48: Lecture 5 - NUS Computing - Home · Lecture 5 Admin Matters Common C Mistakes Unit 9: Logical Expression Unit 10: Assertion Unit 11: Loops Unit 12: Reasoning about Loops 11 September

condition loop body

YES

NO

…initialize update

<initialize>while (<condition>) { <body> <update>}

Page 49: Lecture 5 - NUS Computing - Home · Lecture 5 Admin Matters Common C Mistakes Unit 9: Logical Expression Unit 10: Assertion Unit 11: Loops Unit 12: Reasoning about Loops 11 September

<initialize>do { <body> <update>} while (<condition>);

loop body

YES

NO…initialize update condition

Page 50: Lecture 5 - NUS Computing - Home · Lecture 5 Admin Matters Common C Mistakes Unit 9: Logical Expression Unit 10: Assertion Unit 11: Loops Unit 12: Reasoning about Loops 11 September

read a guess

YES

NO

answer = random number

guess < answer?

guess > answer?

“too high!” “too low!”

YES

NO“congrats”

guess != answer?

YES

NO

Page 51: Lecture 5 - NUS Computing - Home · Lecture 5 Admin Matters Common C Mistakes Unit 9: Logical Expression Unit 10: Assertion Unit 11: Loops Unit 12: Reasoning about Loops 11 September

Assertion

// { … }

Expression that must be true at a given point in the program.

Page 52: Lecture 5 - NUS Computing - Home · Lecture 5 Admin Matters Common C Mistakes Unit 9: Logical Expression Unit 10: Assertion Unit 11: Loops Unit 12: Reasoning about Loops 11 September

long x = 1;// { x == 1 }

(obvious)

Page 53: Lecture 5 - NUS Computing - Home · Lecture 5 Admin Matters Common C Mistakes Unit 9: Logical Expression Unit 10: Assertion Unit 11: Loops Unit 12: Reasoning about Loops 11 September

x = 3/2;// { x == 1.5 }

(maybe not so obvious)