jordan jozwiak cs50. watch problem set 4's walkthrough from sun 10/2, 7pm - 8:30pm in northwest...

34
Week 4 Jordan Jozwiak CS50

Upload: roger-green

Post on 03-Jan-2016

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Jordan Jozwiak CS50. Watch Problem Set 4's walkthrough from Sun 10/2, 7pm - 8:30pm in Northwest Science B103; video will be up by Tue 10/4, if not sooner

Week 4Jordan Jozwiak

CS50

Page 2: Jordan Jozwiak CS50. Watch Problem Set 4's walkthrough from Sun 10/2, 7pm - 8:30pm in Northwest Science B103; video will be up by Tue 10/4, if not sooner

Announcements• Watch Problem Set 4's walkthrough from Sun 10/2, 7pm -

8:30pm in Northwest Science B103; video will be up by Tue 10/4, if not sooner.

• Problem Set 1's and 2's feedback and scores have been returned.

• Expect Problem Set 3 feedback soon.• Quiz 0 coming up on Wed 10/12; details to be announced

in lecture this week. See past years' quizzes at cs50.net/quizzes.

• Take advantage of help.cs50.net.• SUPERSection next week (Columbus Day) – Doodle poll• Consider using Dropbox to backup files from the Appliance

• https://manual.cs50.net/CS50_Appliance_2.3#How_to_Synchronize_Files_with_Dropbox

Page 3: Jordan Jozwiak CS50. Watch Problem Set 4's walkthrough from Sun 10/2, 7pm - 8:30pm in Northwest Science B103; video will be up by Tue 10/4, if not sooner

A Note on Grading• Normalized between TFs• Professor Malan stresses that the focus should be

on improvement!• Grades should be considered as (1: Poor), (2: Fair),

(3: Average), (4: Good), (5: Excellent)• Discussion at end of semester to look at holistic

grade

Page 4: Jordan Jozwiak CS50. Watch Problem Set 4's walkthrough from Sun 10/2, 7pm - 8:30pm in Northwest Science B103; video will be up by Tue 10/4, if not sooner

Pset2 – Feedback• Return values for main• Return 0 at end of main (Stylistically correct, just know

that it is implicit)• Use different return values for different errors (Return 1, Return 2, etc.)

• Program header at top of files (file name, your name, brief description of functionality)

• Make variable names useful (e.g. a variable for the length of an array: key_length

• Include white space before comments• Use functions for repeated code!

Page 5: Jordan Jozwiak CS50. Watch Problem Set 4's walkthrough from Sun 10/2, 7pm - 8:30pm in Northwest Science B103; video will be up by Tue 10/4, if not sooner

Pset2 – Feedback continued…• Vigenere

• Ways to use alphabetic key – convert to upper, lower, or array of ints from 0-25

• ASCII values• ‘a’, ‘z’, ‘A’, ‘Z’ are like useful variable names• 97, 65, etc. seem like arbitrary numbers (if there weren’t the above

notation then you would want to use magic numbers!)

• Else statements after an if statement that returns if it is true

• When I say “know that …” I’m not necessarily saying that what you’re doing is incorrect, I just want you to be aware of another way of doing something.

• Questions???

Page 6: Jordan Jozwiak CS50. Watch Problem Set 4's walkthrough from Sun 10/2, 7pm - 8:30pm in Northwest Science B103; video will be up by Tue 10/4, if not sooner

This Week• Style• Hexadecimal Numbers• Merge Sort• Dynamic Memory Allocation–Malloc–Free

• Stack and Heap• Pointers

Page 7: Jordan Jozwiak CS50. Watch Problem Set 4's walkthrough from Sun 10/2, 7pm - 8:30pm in Northwest Science B103; video will be up by Tue 10/4, if not sooner

Style

Good Style:• Consistent Indentation• Comments Appropriately Detailed• Self-Explanatory Variable NamesBad Style:• Single-letter or cryptic variable names• Lack of comments; paragraph comments

Page 8: Jordan Jozwiak CS50. Watch Problem Set 4's walkthrough from Sun 10/2, 7pm - 8:30pm in Northwest Science B103; video will be up by Tue 10/4, if not sooner

Hexadecimal Numbers• Hexadecimal is a way of representing numbers in

base 16, with each digit ranging from 0 – F.• We usually represent numbers in base 10 and in

binary we represent numbers in base 2, this is just one more way!

0 1 2 3 4 5 6 7 8 9 10

11

12

13

14

15

0 1 2 3 4 5 6 7 8 9 A B C D E F

Page 9: Jordan Jozwiak CS50. Watch Problem Set 4's walkthrough from Sun 10/2, 7pm - 8:30pm in Northwest Science B103; video will be up by Tue 10/4, if not sooner

Hexadecimal Numbers• Hexadecimal is useful because it gives us a

concise way of representing values in memory.• Every set of 4 bits (a ‘nibble’, or half a ‘byte’) can

be represented by 1 hex digit!

1 0 0 1 1 1 1 0 0 1 1 1 0 0 0 1

9 E 7 1

Page 10: Jordan Jozwiak CS50. Watch Problem Set 4's walkthrough from Sun 10/2, 7pm - 8:30pm in Northwest Science B103; video will be up by Tue 10/4, if not sooner

Hexadecimal Numbers• Notationally, we write hexadecimal numbers

following ‘0x’, which denotes only that what follows is a hexadecimal number.

• The below value might be written: 0x9E71

1 0 0 1 1 1 1 0 0 1 1 1 0 0 0 1

9 E 7 1

Page 11: Jordan Jozwiak CS50. Watch Problem Set 4's walkthrough from Sun 10/2, 7pm - 8:30pm in Northwest Science B103; video will be up by Tue 10/4, if not sooner

Merge Sort

• Merge Sort – another sorting algorithm, like bubble sort or selection sort… but better.

• Works by recursively breaking lists into halves, then recombining the sorted base cases into larger sorted lists.

Page 12: Jordan Jozwiak CS50. Watch Problem Set 4's walkthrough from Sun 10/2, 7pm - 8:30pm in Northwest Science B103; video will be up by Tue 10/4, if not sooner

Merge Sort

Page 13: Jordan Jozwiak CS50. Watch Problem Set 4's walkthrough from Sun 10/2, 7pm - 8:30pm in Northwest Science B103; video will be up by Tue 10/4, if not sooner

Merge Sort – thank you, Wikipedia• Conceptually, a merge sort works as follows

1. If the list is of length 0 or 1, then it is already sorted. Otherwise:

2. Divide the unsorted list into two sublists of about half the size.

3. Sort each sublist recursively by re-applying the merge sort.4. Merge the two sublists back into one sorted list.

• Merge sort incorporates two main ideas to improve its runtime:1. A small list will take fewer steps to sort than a large list.2. Fewer steps are required to construct a sorted list from two

sorted lists than from two unsorted lists. For example, you only have to traverse each list once if they're already sorted.

Page 14: Jordan Jozwiak CS50. Watch Problem Set 4's walkthrough from Sun 10/2, 7pm - 8:30pm in Northwest Science B103; video will be up by Tue 10/4, if not sooner

Merge Sort

What’s the runtime of this algorithm?

Page 15: Jordan Jozwiak CS50. Watch Problem Set 4's walkthrough from Sun 10/2, 7pm - 8:30pm in Northwest Science B103; video will be up by Tue 10/4, if not sooner

Merge Sort

What’s the runtime of this algorithm?O(n log n)

Page 16: Jordan Jozwiak CS50. Watch Problem Set 4's walkthrough from Sun 10/2, 7pm - 8:30pm in Northwest Science B103; video will be up by Tue 10/4, if not sooner

Merge Sort

What’s the runtime of this algorithm?O(n log n)

Why?

Page 17: Jordan Jozwiak CS50. Watch Problem Set 4's walkthrough from Sun 10/2, 7pm - 8:30pm in Northwest Science B103; video will be up by Tue 10/4, if not sooner

Merge Sort

What’s the runtime of this algorithm?O(n log n)

Why?Think of the number of elements in our list, and the number of steps we have to perform for each element.

Page 18: Jordan Jozwiak CS50. Watch Problem Set 4's walkthrough from Sun 10/2, 7pm - 8:30pm in Northwest Science B103; video will be up by Tue 10/4, if not sooner

Dynamic Memory Allocation

• When we want to reserve a chunk of memory to store something but don’t want to keep it on the stack, we can reserve it on the heap.

• This lets us reserve memory which we can still access when our function returns.

Page 19: Jordan Jozwiak CS50. Watch Problem Set 4's walkthrough from Sun 10/2, 7pm - 8:30pm in Northwest Science B103; video will be up by Tue 10/4, if not sooner

Dynamic Memory Allocation• malloc(int num_bytes)• Reserves a region of memory on the heap of size

num_bytes.• Returns the address of this region of memory.

• free(void* pointer)• Takes as an argument the address of the start of some

region of memory reserved by malloc.• Frees up the memory which was reserved.

Page 20: Jordan Jozwiak CS50. Watch Problem Set 4's walkthrough from Sun 10/2, 7pm - 8:30pm in Northwest Science B103; video will be up by Tue 10/4, if not sooner

Dynamic Memory Allocation

// reserve enough space to store 10 charschar* x = malloc(sizeof(char)*10);

// give up this region of memoryfree(x);

Page 21: Jordan Jozwiak CS50. Watch Problem Set 4's walkthrough from Sun 10/2, 7pm - 8:30pm in Northwest Science B103; video will be up by Tue 10/4, if not sooner

Dynamic Memory Allocation

When using malloc:• Always check whether it returns ‘null’.• Free allocated memory exactly once.

Why are these things important?

Page 22: Jordan Jozwiak CS50. Watch Problem Set 4's walkthrough from Sun 10/2, 7pm - 8:30pm in Northwest Science B103; video will be up by Tue 10/4, if not sooner

Stack vs. HeapHeap

• Contains local variables.

• Function calls create new ‘frames’ on the stack.

• Contains global variables.

• Dynamically allocated memory reserved on heap.

Memory belonging to process.

Stack

Lower Memory Addresses

Higher Memory Addresses

Page 23: Jordan Jozwiak CS50. Watch Problem Set 4's walkthrough from Sun 10/2, 7pm - 8:30pm in Northwest Science B103; video will be up by Tue 10/4, if not sooner

Pointers• Data stored in memory has both a value and a

byte-addressed location.• Pointers contain the memory address of some

piece of data.

• <type>* pointers contain addresses to a piece of data of the given type.

Page 24: Jordan Jozwiak CS50. Watch Problem Set 4's walkthrough from Sun 10/2, 7pm - 8:30pm in Northwest Science B103; video will be up by Tue 10/4, if not sooner

Pointers

• & - Address operator; gets the address of a variable.

• * - Dereference operator; gets value found at the memory address stored in a pointer.

Page 25: Jordan Jozwiak CS50. Watch Problem Set 4's walkthrough from Sun 10/2, 7pm - 8:30pm in Northwest Science B103; video will be up by Tue 10/4, if not sooner

Pointers

‘A’

char letter = ‘A’;char* ptr = &letter; // get address of variablechar new_letter = *ptr; // dereference to get value

letter ptr new_letter

‘A’

Page 26: Jordan Jozwiak CS50. Watch Problem Set 4's walkthrough from Sun 10/2, 7pm - 8:30pm in Northwest Science B103; video will be up by Tue 10/4, if not sooner

Pointers

Pointer arithmetic: adding n to a pointer shifts the pointer over by

n*sizeof(<type of the pointer>) bytes

Page 27: Jordan Jozwiak CS50. Watch Problem Set 4's walkthrough from Sun 10/2, 7pm - 8:30pm in Northwest Science B103; video will be up by Tue 10/4, if not sooner

Pointers

int x;int* y = &x;y += 1;

If the address of x is 0x04, what is y now?

Page 28: Jordan Jozwiak CS50. Watch Problem Set 4's walkthrough from Sun 10/2, 7pm - 8:30pm in Northwest Science B103; video will be up by Tue 10/4, if not sooner

Pointers

char x;char* y = &x;y += 1;

If the address of x is 0x04, what is y now?What about now?

Page 29: Jordan Jozwiak CS50. Watch Problem Set 4's walkthrough from Sun 10/2, 7pm - 8:30pm in Northwest Science B103; video will be up by Tue 10/4, if not sooner

Pointer Practice

a b c pa

pb pc

a = b * c;

a *= c;

b = *pa;

pc = pa;

*pb = b * c;

c = (*pa) * (*pc);

*pc = a * (*pb);

int a = 3, b = 4, c = 5;int *pa = &a, *pb = &b, *pc = &c;

Page 30: Jordan Jozwiak CS50. Watch Problem Set 4's walkthrough from Sun 10/2, 7pm - 8:30pm in Northwest Science B103; video will be up by Tue 10/4, if not sooner

Pointer Practice

a b c pa

pb pc

a = b * c; 20 4 5 &a &b &c

a *= c; 100 4 5 &a &b &c

b = *pa; 100 100 5 &a &b &c

pc = pa; 100 100 5 &a &b &a

*pb = b * c; 100 500 5 &a &b &a

c = (*pa) * (*pc);

100 500 10000 &a &b &a

*pc = a * (*pb);

50000 500 10000 &a &b &a

int a = 3, b = 4, c = 5;int *pa = &a, *pb = &b, *pc = &c;

Page 31: Jordan Jozwiak CS50. Watch Problem Set 4's walkthrough from Sun 10/2, 7pm - 8:30pm in Northwest Science B103; video will be up by Tue 10/4, if not sooner

Arrays are Pointers!

numbers[0] numbers[1] numbers[2] numbers[3] numbers[4] numbers[5]

numbers

int numbers[6];

Page 32: Jordan Jozwiak CS50. Watch Problem Set 4's walkthrough from Sun 10/2, 7pm - 8:30pm in Northwest Science B103; video will be up by Tue 10/4, if not sooner

Arrays are Pointers!

array[i] == *(array + i)

Page 33: Jordan Jozwiak CS50. Watch Problem Set 4's walkthrough from Sun 10/2, 7pm - 8:30pm in Northwest Science B103; video will be up by Tue 10/4, if not sooner

Coding Time!!!• In the CS50 Appliance…• cloud.cs50.net/~jjozwiak

• Work with partners!• Discussion• Pointers.c• Bar.c• Chart.c• Local.c• Malloc.c• Heap.c• Array1.c• Array2.c

• /practice• Swap_TODO.c

Page 34: Jordan Jozwiak CS50. Watch Problem Set 4's walkthrough from Sun 10/2, 7pm - 8:30pm in Northwest Science B103; video will be up by Tue 10/4, if not sooner

On your way out…• Grab a cheat sheet!

• Feedback• How comfortable are you with the material?• How is the pace of section?• Any other questions, suggestions or concerns?