Download presentation
Presentation is loading. Please wait.
1
Intro to Algorithms in SNAP!
Unit 2: AlgorithmDevelopment
2
Warm Up: What did your parent mean by “clean your room”?
CleanRoom There is a lot “hidden” “behind” the instruction “clean your room” Fold clothes – Fold jeans + Fold shirts – Fold in half vertically with the arms together. Fold the arms back onto shirt. Fold horizontally, so that the hem of the shirt touches the neck. Flatten the shirt. Vacuum + Dust + This is called a “procedure” or a “function”
3
Reminder: Algorithms CANNOT be ambiguous
EX: Algorithms need to be expressed in a context-free, unambiguous way for all participants!
4
What is Abstraction? The hiding/layering of information
Ex: swipe on an iPhone, light switch, pushing any button. These single actions set a chain of many “abstracted” events by programming or invention. The ability to abstract away many of the details of algorithms in subprograms makes it possible to construct, read, and understand large programs. This Photo by Unknown Author is licensed under CC BY-SA
5
Why do we need to understand abstractions in the form of Processes/Procedures?
to bundle processes to manage complexity to create modularity to facilitate collaborative programming of large projects, etc. Basically, they provide a way for a program to specify that some process is to be done, without providing the details of how it is to be done. THIS IS CRUCIAL TO THE PROGRAMMING PROCESS.
6
You Try. What is an abstraction you use every day
You Try! What is an abstraction you use every day? Write An Algorithm for one! Getting Ready for School GetReadySchool How is GetReady School an abstraction? GetReadySchool is an abstraction (in that it hides other parts of for an algorithm with several other functions, such as GetDressed and EatBreakfast. Once GetDressed is completed, the algorithm moves to the next item in the sequence, EatBreakfast, and so on, completing each task in the order presented in the algorithm. GetDressed - Find favorite pair of pants Choose a shirt from the closet EatBreakfast + BrushTeeth + Grab backpack
7
What should I name my functions?
8
Last one! What is an algorithm for drawing a square?
draw_square
9
How do we make a square in Snap?
This is just ONE way to draw a square in Snap! How do we make a square in Snap? Grab your computer, go to Play around with the blocks until you have drawn a square. Hint: use the blue Motion blocks Don’t forget to put the pen down If you need review :
10
Which way is “better”? What makes correctly working code “good” code—is it the brevity, the clarity? or some combination? This is the use of iteration (defined on next slide)
11
Building Blocks of Algorithms
Sequencing – the application of each step and an algorithm in a given order Selection – the use of Boolean condition to select which of two parts to do Iteration – the act of repeating a process Recursion - when a function calls itself one or more times in its body
12
Could we create a procedure (aka function) to always draw a square in this way?
In this dialog box, you can choose the block's palette, shape and name. There is also an “Other” option that makes grey blocks in the Variables palette for blocks that don’t fit any category There are three block shapes: - The jigsawpuzzle-piece shaped blocks are Commands, and don’t report a value. - The oval blocks are Reporters - The hexagonal blocks are Predicates, which is the technical term for reporters that report Boolean (true or false) values.
13
Build your Function that will draw a square!
This is a type of “abstraction”
14
What is a Function in Math?
A relationship that has exactly one output for every input. EXS: y = x + 2 y = x² y = sin(x) Plus5 is the function N the input (a number Returns a single value
15
In Programming, a function is
An abstraction that takes in 0 or more inputs, returning exactly 1 output The same inputs MUST yield same outputs Past history of function is (like other inputs that have been called on) don’t affect the current/new one Think of this like a walled world. It can’t do anything else. Just does its job (EX: won’t make something move around or send something to the printer unless that was its job)
16
What is the difference between an algorithm and a function?
An algorithm is a concept of how to accomplish a task. Is language agnostic, usually written in pseudocode EX: A function (aka Procedure) is the implementation of an algorithm in a particular language EX: Different langauges are better suited for different algorithms
17
Why use Functions? Let’s say you want to make two kinds of Milkshakes: Strawberry or banana Does it make sense to write out two full recipes if you are using the same base, just changing one line for a different fruit? No Way! Its better to use generalization and a common pattern!
18
Why use functions? They allow for generalization of code They are like building blocks of our programs They can be composed together They help break big problems down into smaller ones (functional decomposition)
19
Data Types sentences words characters letters
If were talking about what were going to pass to a function, we need names of those A SINGLE LETTER CAN ALSO BE A WORD (EX: a )
20
What is allowed in a function as input and output?
THINK DOMAIN AND RANGE FROM MATH CLASS DOMAIN – The characterization or “class” of inputs that a function takes in RANGE – the possible “return” values that a function outputs EXAMPLES y = 𝑥 D: non-negative numbers R:Non-negative numbers Length_of D: Sentence, word, number R:non-negative integer _ < _ D: sentence, word, number R: Boolean (T or F) EX: going to a country with your phone charger and it doesn’t work EX: two words being compared in the dictionary
21
You can edit your block so that you can adjust the size by adding a parameter! A parameter is a variable that must be given a specific value during the execution of a program Instead of making code for every single size square you want … make a function that takes in a parameter “size”
22
Then you can make a new block that draws any shape
QuickQuizQuestion: What is a custom block called? A procedure What are the inputs called? parameters This might seem familiar from your Unit1/2 modules!
23
U1 L3 and L5 for classwork! Sprite Line Art and Ping Pong for HW!
24
What happens if you forget where to find the block?
If you forget what palette (color) a block is, but you remember at least part of its name, type control-F and enter the name in the text block that appears in the palette area.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.