Lecture 6 18 September 2018 Admin Matters Common C Mistakes Unit 13: - - PowerPoint PPT Presentation

lecture 6
SMART_READER_LITE
LIVE PREVIEW

Lecture 6 18 September 2018 Admin Matters Common C Mistakes Unit 13: - - PowerPoint PPT Presentation

Lecture 6 18 September 2018 Admin Matters Common C Mistakes Unit 13: Call Stack Unit 14: Pointer Unit 15: Array Unit 16: String Midterm Venue: MPSH 1 (B) 2 October 4pm - 6pm AY18/19 Sem 1 Midterm Open Book Lecture 1 to 5 AY18/19 Sem 1


slide-1
SLIDE 1
slide-2
SLIDE 2

Lecture 6

Admin Matters Common C Mistakes Unit 13: Call Stack Unit 14: Pointer Unit 15: Array Unit 16: String

18 September 2018

slide-3
SLIDE 3

AY18/19 Sem 1

Midterm

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

slide-4
SLIDE 4

AY18/19 Sem 1

Midterm

Open Book Lecture 1 to 5

slide-5
SLIDE 5

AY18/19 Sem 1

Tutorial 5
 Problem Sets from Units Today

slide-6
SLIDE 6

AY18/19 Sem 1

Assignment 1


being graded

slide-7
SLIDE 7

AY18/19 Sem 1

Assignment 1


solutions & general comments to be made available tomorrow

slide-8
SLIDE 8

AY18/19 Sem 1

Assignment 2


Due this Friday 6pm

slide-9
SLIDE 9

AY18/19 Sem 1

Assignment 3


Release this Friday

(to be graded on correctness and style)

slide-10
SLIDE 10

AY18/19 Sem 1

Assignment 3
 
 Everything

up to arrays

slide-11
SLIDE 11

AY18/19 Sem 1

Assignment 3


Due 5 October 2018

slide-12
SLIDE 12

AY18/19 Sem 1

Practical Exam 1

Midterm Information posted

  • nline
slide-13
SLIDE 13

AY18/19 Sem 1

Previously on CS1010..

slide-14
SLIDE 14

AY18/19 Sem 1

Unit 5: We will use long and double only for CS1010

slide-15
SLIDE 15

AY18/19 Sem 1

Using int will cause your program to fail

  • n large inputs.
slide-16
SLIDE 16

AY18/19 Sem 1

What’s the advantage

  • f int over long?

(besides memory usage)

slide-17
SLIDE 17

AY18/19 Sem 1

Using float will cause your program to loose precision.

slide-18
SLIDE 18

AY18/19 Sem 1

What’s the advantage

  • f float over

double?

(besides memory usage)

slide-19
SLIDE 19

AY18/19 Sem 1

Please follow instructions given in the assignment.

slide-20
SLIDE 20

AY18/19 Sem 1

e.g., not writing function

area_of_rectangle

slide-21
SLIDE 21

AY18/19 Sem 1

e.g., not solving digits recursively

slide-22
SLIDE 22

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); }

not a function

slide-23
SLIDE 23

AY18/19 Sem 1

“This is a very very bad programming

  • habit. So, don’t do

this”

slide-24
SLIDE 24

AY18/19 Sem 1

There are still students who do this

😗

slide-25
SLIDE 25

AY18/19 Sem 1

Strict policy on plagiarism

Disciplinary action will be taken :(

slide-26
SLIDE 26

AY18/19 Sem 1

There are still students who do this

😗

slide-27
SLIDE 27
slide-28
SLIDE 28

AY18/19 Sem 1

Call Stack

slide-29
SLIDE 29

int main() { long x = 1; long y; }

slide-30
SLIDE 30

int main() { long x = 1; long y; }

Call Stack

x

stack frame

1

x y

slide-31
SLIDE 31

long add(long a, long b) { 
 long sum; 
 sum = a + b; 
 return sum; 
 } 
 int main() { 
 long x = 1; 
 long y; 
 y = add(x, 10); 
 }

slide-32
SLIDE 32

long add(long a, long b) { 
 long sum; 
 sum = a + b; 
 return sum; 
 } 
 int main() { 
 long x = 1; 
 long y; 
 y = add(x, 10); 
 } x

1

x y

x

1 10

a b

11

sum

slide-33
SLIDE 33

long add(long a, long b) { 
 long sum; 
 sum = a + b; 
 return sum; 
 } 
 int main() { 
 long x = 1; 
 long y; 
 y = add(x, 10); 
 } x

1 11

x y

slide-34
SLIDE 34

long add(long a, long b) { 
 long sum; 
 sum = a + b; a = 42; 
 return sum; 
 } 
 int main() { 
 long x = 1; 
 long y; 
 y = add(x, 10); 
 }

slide-35
SLIDE 35

long add(long a, long b) { 
 long sum; 
 sum = a + b; a = 42; 
 return sum; 
 } 
 int main() { 
 long x = 1; 
 long y; 
 y = add(x, 10); 
 } x

1

x y

x

1 10

a b

11

sum

slide-36
SLIDE 36

long add(long a, long b) { 
 long sum; 
 sum = a + b; a = 42; 
 return sum; 
 } 
 int main() { 
 long x = 1; 
 long y; 
 y = add(x, 10); 
 } x

1

x y

x

42 10

a b

11

sum

slide-37
SLIDE 37

long add(long a, long b) { 
 long sum; 
 sum = a + b; a = 42; 
 return sum; 
 } 
 int main() { 
 long x = 1; 
 long y; 
 y = add(x, 10); 
 } x

1 11

x y

slide-38
SLIDE 38

void add(long sum, long a, long b) { sum = a + b; } int main() { long x = 1; long sum; add(sum, x, 10); }

slide-39
SLIDE 39

void add(long sum, long a, long b) { sum = a + b; } int main() { long x = 1; long sum; add(sum, x, 10); } x

1

x sum

slide-40
SLIDE 40

void add(long sum, long a, long b) { sum = a + b; } int main() { long x = 1; long sum; add(sum, x, 10); } x

1 ??

x sum

x

1 10

a b

??

sum

slide-41
SLIDE 41

void add(long sum, long a, long b) { sum = a + b; } int main() { long x = 1; long sum; add(sum, x, 10); } x

1 ??

x sum

x

1 10

a b

11

sum

slide-42
SLIDE 42

void add(long sum, long a, long b) { sum = a + b; } int main() { long x = 1; long sum; add(sum, x, 10); } x

1 ??

x sum

slide-43
SLIDE 43

& address of a variable (i.e, address of a memory location)
 * memory location at an 
 address (i.e., the variable 
 at the address)

slide-44
SLIDE 44

& address of a variable if x is a variable, then &x gives us the address of x. (where does x live?)

slide-45
SLIDE 45

Suppose we have an address a, how to find

  • ut who lives there?

answer: *a

slide-46
SLIDE 46

x

x ptr

int main() { long x; long *ptr; ptr = &x; *ptr = 1; }

slide-47
SLIDE 47

#include "cs1010.h" 
 
 void add(long sum, long a, long b) 
 { 
 sum = a + b; 
 cs1010_println_long((long)&sum); 
 } 
 int main() 
 { 
 long x = 1; 
 long sum; 
 add(sum, x, 10); 
 cs1010_println_long((long)&sum); 
 }

slide-48
SLIDE 48

Some Rules about Pointers

  • A pointer to some type T can only points to a

variable of type T

  • We cannot change the address of a variable (but

can change what a pointer is pointing to)

  • We can perform add and subtract on pointers
slide-49
SLIDE 49

AY18/19 Sem 1

Arrays

slide-50
SLIDE 50

AY18/19 Sem 1

array decay

slide-51
SLIDE 51

AY18/19 Sem 1

long a[10]; a 
 is equivalent to 
 &a[0]

slide-52
SLIDE 52

AY18/19 Sem 1

long a[2] = {0, 1}; long b[2] = {0, 1}; if (a == b) { // always false : } b = a; // not possible

slide-53
SLIDE 53

AY18/19 Sem 1

a[i] = 10; *(a + i) = 10; x = a[42]; x = *(a + 42);

slide-54
SLIDE 54

AY18/19 Sem 1

long max(long list[], long length) { : } int main() { long a[10] = { … }; max(a, 10); }

slide-55
SLIDE 55

AY18/19 Sem 1

long max(long *list, long length) { : } int main() { long a[10] = { … }; max(a, 10); }

slide-56
SLIDE 56

AY18/19 Sem 1

long a[ ? ];

slide-57
SLIDE 57

AY18/19 Sem 1

long size = cs1010_read_long(); : long a[size];

variable-size array

slide-58
SLIDE 58

AY18/19 Sem 1

long size = cs1010_read_long(); : long *a = cs1010_read_long_array(size);

variable-size array

slide-59
SLIDE 59

AY18/19 Sem 1

Strings

slide-60
SLIDE 60

AY18/19 Sem 1

A string is an array of char terminated by ‘\0’

slide-61
SLIDE 61

char *str = “hello!”; char str[7] = { ‘h’, ‘e’, ‘l’, 
 ‘l’, ‘o’, ‘!’, ’\0’}