1 / 39

Week 3

CS50. Week 3. Jordan Jozwiak. This Week. GDB Arrays Call Stack Recursion Search Sort Measuring Running Time. Last Week - Questions. What does it mean to pass a value to a function? What does it mean to return a value from a function? So, the difference? How do you make a function?

maite-york
Download Presentation

Week 3

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS50 Week 3 Jordan Jozwiak

  2. This Week • GDB • Arrays • Call Stack • Recursion • Search • Sort • Measuring Running Time

  3. Last Week - Questions • What does it mean to pass a value to a function? What does it mean to return a value from a function? So, the difference? • How do you make a function? • What are argc and argv[]? When are they used? What are their types? • What is main()? • What is the meaning of void? When is it used?

  4. Pset0 & Pset1 – grades and comments • Questions? • Magic numbers • Style: comments, white space • For loop: initialization, scope • Corner cases! • Read spec carefully! • …questions??? On Pset2?

  5. GDB • GDB - GNU Debugger • Lets you walk through a program one step at a time and ‘pause’ it. • You can also peek inside your variables – this is much more powerful than printf!

  6. GDB – Basic Commands break – tell the program to ‘pause’ at a certain point (either a function or a line number) step – ‘step’ to the next executed statement next – moves to the next statement WITHOUT ‘stepping into’ called functions continue – move ahead to the next breakpoint print – display some variable’s value backtrace – trace back up function calls

  7. Arrays • Set of variables of the same type sharing a name. • Access each individual element in an array using an index value. • Passed by reference, rather than by value.

  8. Passing By Reference Pass by Value – passing a copy of the data stored in a variable. Pass by Reference – passing the address at which the original copy of the data is stored.

  9. Passing By Reference Street Address vs. Replica of the House 42 Dunster Street Cambridge, MA 02138 Reference Value

  10. Passing By Reference Mailing Address vs. Contents of Mailbox 153 Kirkland Mail Center Cambridge, MA 02138 Reference Value

  11. Arrays • Arrays are passed by reference. • Passing an array gives a function the address of the start of the array. int numbers[6]; numbers[1] numbers[2] numbers[3] numbers[4] numbers[5] numbers[0] numbers

  12. Arrays • Name of the array refers to starting position. • numbers[i] accesses the ith element int numbers[6]; numbers[1] numbers[2] numbers[3] numbers[4] numbers[5] numbers[0] numbers

  13. Recursion

  14. What is Recursion? • Recursion - a method of defining functions in which the function being defined is applied within its own definition • A recursive function is a function which calls itself

  15. Components of Recursive Function • Recursive Call – part of function which calls the same function again. • Base Case – part of function responsible for halting recursion when appropriate.

  16. Recursion Example #1 void countdown(int num) { printf(“%d!”, num); if(num > 0) countdown(num – 1); else return; } Recursive Call Base Case

  17. Recursion Example #1 void countdown(int num) { while(num > 0) { printf(“%d!”, num); num--; } } void countdown(int num) { printf(“%d!”, num); if(num > 0) countdown(num – 1); else return; } Recursive Non-Recursive

  18. Recursion Example #2 • Natural recursive function: Factorial • n! = 1 • n * (n-1)! n <= 1 otherwise

  19. Recursion Example #2 int factorial (int n) { //Base Case if (n == 1) return 1; //Recursive Call else return n * factorial(n - 1); }

  20. Recursion Example #2

  21. Call Stack • Each call to a functions gets its own stack ‘frame’ containing local variables. • When a function calls a function, it creates a new stack frame.

  22. Call Stack main() func1() func2() func3()

  23. Search • We are given an array of n sorted elements and a particular element x. • How do we determine whether x is contained in the array?

  24. Linear Search • Want to find a particular element in an array? • Search through each element until we find it! • But this isn’t very good... Can we do better?

  25. Binary Search • Like searching through the phone book. • Identify whether it’s in the first or second half, dividing the problem in two. Repeat until we’re down to one element. 1 2 4 8 16 45 72 83 906 4012 How could we apply binary search to find the value 72 here?

  26. Runtime of Search So... What’s the asymptotic running time of: • Linear Search? • Binary Search?

  27. Runtime of Search So... What’s the asymptotic running time of: • Linear Search? O(n) • Binary Search?

  28. Runtime of Search So... What’s the asymptotic running time of: • Linear Search? O(n) • Binary Search? O(log n)

  29. Sort • Before, we assumed the arrays we wanted to search were sorted. What if they aren’t? • Given an array of n elements, how do we manipulate the array to ensure the elements end up sorted?

  30. Bubble Sort • Sorting an array of size n. • Iterate through the array, comparing each pair as you go. If array[i + 1] > array[i], swap them! • Repeat up to n times (or until array is completely sorted...).

  31. Selection Sort • Grab the smallest and swap it with whatever is at the front of the array. • Now grab the next smallest and swap with what is in position 2. • Repeat until entire array is sorted.

  32. Asymptotic Notation • Some of our sorting and searching algorithms seem better than others. • Some algorithms for the same problems become much better than others as the size of the problem increases. • Asymptotic notation allows us to represent and compare these kinds of running times!

  33. Asymptotic Notation • Used to describe the runtime of an algorithm. • Runtime measured in terms of how the amount of time taken changes as input size increases. • Allows us to compare how runtime increases as the size of input becomes large.

  34. Asymptotic Notation • Big O notation describes an upper bound on runtime. • Ω describes a lower bound on runtime. • θ describes both an upper and lower bound.

  35. Asymptotic Notation • 1 – Constant Time • log n – Logarithmic Time • n – Linear Time • n2 – Quadratic Time • np – Polynomial Time • 2n – Exponential Time • n! – Factorial Time

  36. Runtime

  37. Runtime

  38. Coding time!!! • Use CS50 Appliance • cloud.cs50.net/~jjozwiak • Work with partners!

  39. 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?

More Related