Administrative Notes January 19, 2017 Reminder: In the News due on Friday Reminder: Reading Quizzes due next Monday Anne will be away next Monday and Tuesday and will not have office hours on Monday Jessica will teach Tuesday s class on the Internet
How do programs work? Part 2 Snap! code for Sorting 2
Learning goals [CT Building Block] Explain what a variable is in computer programming. [CT Building Block] Be able to trace through code using sequences of instructions, variables, loops, and conditional statements in short programs specified in a visual programming language such as Snap, or in other clearly expressed processes (which may or may not be computer related) 3
Programs, data and lists You ve seen programs that do simple things with data (your age, shoe size, the mouse position, text, etc.) Sorting typically involves lists of data items. We can create lists in Snap! For now, we'll create a short list of five items manually, But a list containing hundreds or more items could imported to Snap from a file
We can give our list a name
We can give our list a name
Why give a name to a list? Names enable us write programs that do useful things with lists, like sort them We ll first use some clicker questions to see how to query what s in a list, compare two items in the list, and more Note that the items in our list are indexed consecutively, starting from 1
Snap! operations on lists Clicker question What will the sprite say, when this code is run? A. 5 B. 7 C. 22 8
Snap! operations on lists Clicker question Suppose we make a new variable, current. What does the sprite say now? A. 3 B. 4 C. 5 D. 7 9
Snap! operations on lists Clicker question And now? A. 2 B. 3 C. 5 D. 7 10
Snap! operations on lists Clicker question Suppose we make yet another new variable, called marked and run the following code fragment Does the following inequality hold? (True or False) A. True B. False 11
Testing if a list is sorted Suppose we have a mystery list with five different positive numbers. How could we test whether it is sorted in increasing order? In groups, develop an algorithm to do this. Write down your algorithm in English. (Imagine that you have a list of cards.)
Testing if a list is sorted Student algorithm What are your algorithms?
Testing if a list is sorted Here s an algorithm that we ll code in Snap: Compare items 1 and 2; if item 1 is greater than item 2, then report that the list is not sorted and stop Compare items 2 and 3; if item 1 is greater than item 2, then report that the list is not sorted and stop Continue in this way until the end of the list is reached, and (if the algorithm has not already stopped) report that the list is sorted
Testing if a list is sorted Code for the step of comparing items 1 and 2:
Testing if a list is sorted Code for the step of comparing items 1 and 2: How would you change this code to do the right thing for items 2 and 3?
Testing if a list is sorted Code for the step of comparing items 1 and 2, plus 2 and 3:
Testing if a list is sorted For a list with five items, how many if blocks would we need?
Testing if a list is sorted For a list with five items, how many if blocks would we need? And for a list with a million items? Writing all that code would be tedious! Is there a better way to code repetitive tasks?
Testing if a list is sorted Student ideas (better coding of repetition) Express the code in terms of add one each time, rather than listing each step explicitly Refer to the length of list rather than using a number Use a repeat loop
Testing if a list is sorted: using a repeat loop Set a counter i to be 1 Repeat the following 4 times: Compare items i and i+1; if item i is greater than item i+1, then report The list is not sorted and stop Increment i by 1 Report The list is sorted
Testing if a list is sorted: using a repeat loop First try: make variable i, set it to 1, and compare items i and i+1
Testing if a list is sorted: using a repeat loop Second try: add the repeat loop:
How many variables are in this code? Clicker question A. 1 C. 3 B. 2 D. 4
Finding the smallest item in a list Recall how we did this with cards
Finding the smallest item in a list Recall how we did this with cards We ll use a variable called marker to store the index of the smallest item found in the list so far We ll use another variable called current to help us work through the other items in the list and compare them with the marked item
Finding the smallest item in a list How to initially set marker and current?
Finding the smallest item in a list How many times should we repeat?
Finding the smallest item in a list
What if we want to change a list?
What if we want to change a list? We can add and delete things from lists
What if we want to change a list? We can add and delete things from lists
What if we want to change a list? What will the list look like after the following code? A. B.
What if we want to change a list? What will the list look like after the following code? A. B.
What if we want to change a list? What will the list look like after the following code? A. B.
What if we want to change a list? What will the list look like after the following code? A. B.
We re ready to sort!
We re ready to sort! First, create a new list, called sorted-list, which initially has no items in it
We re ready to sort! Then repeat the following: Find the smallest item in the unsorted list Add it to (the end of) the sorted list Delete it from the unsorted list
We re ready to sort!
Learning Goals Revisited [CT Building Block] Explain what a variable is in computer programming. [CT Building Block] Be able to trace through code using sequences of instructions, variables, loops, and conditional statements in short programs specified in a visual programming language such as Snap, or in other clearly expressed processes (which may or may not be computer related) 41