ICSCT - Using Algorithms to Solve Problems Lesson
Using Algorithms to Solve Problems Lesson
In this lesson, you will write algorithms to solve problems in a programming language called Scratch. Scratch is a 2D programming environment developed by the Lifelong Kindergarten Group at MIT Media Lab, and is currently available online and as a desktop application for Windows, Mac OS X, and Linux.
This is an example of how the three types of control structures, sequence, conditional, and iteration, are used in the Scratch programming language.
To practice creating algorithms and transferring them to a program, you will create algorithms to draw different polygons using the pen object in Scratch. Let's look at how we might explain to someone how to draw a square in our natural language.
- Draw a line
- Turn right
- Draw a line
- Turn right
- Draw a line
- Turn right
- Draw a line
This might be a good algorithm for a human to follow but not a computer. As humans, we know from experience that we need a pen or another type of drawing tool. Since it is a square we know that all the sides are even so we would probably draw it with each line the same length.
A computer cannot infer information. It needs to know every instruction. It needs instructions to get a pen, put the pen down, instructions on how far to draw the line, what direction, and the degree to turn.
Here is the algorithm created using the pre-defined blocks in Scratch to draw a square.
Examining the Algorithm
- The first instruction when clicked is the event that starts the code.
- The clear command is needed so it will clear the screen.
- The pen down sets the pen down so it will draw.
Problem Solving to Create the Square: What information do we need to know?
Finding the Degree of Turn for Regular Polygons
- Step 1: Identify the number of sides in the polygon.
- Step 2: Divide 360 by the number of sides to find the degree of turn for each angle.
Example: Degree of Turn for an Octagon
- An octagon has eight sides.
- Divide 360 by 8:
- Therefore, each angle in an octagon measures 45 degrees.
Remember, this method works for all regular polygons! If you encounter any other shapes, just follow these steps to find their degree of turn.
To create a square you need to know the degree to turn and how far to draw the line. A square has four sides. Use the steps shown above to find the degree of turn. The length of the line depends on how big you want to draw the square.
- Move 100 steps
- Turn 90 degrees
For a sequential programming structure, the move and turn will be repeated four times to create the square.
There is a simpler way to create a square using iteration. Let's change our algorithm to use iteration. We use a repeat 4 times and move 100 and turn 90 degrees. This is a more efficient way to draw a square.
Now let's use a condition to make our code execute. We only want to draw a square if we press the space bar. Scratch uses a structure forever if to execute conditionals.
To help you get started, go to the website for Scratch in the link below. Go through the tutorial titled "Getting Started with Scratch." You will then be able to complete the Creating Algorithms project.
Getting Started With Scratch
Links to an external site. - when you get there, click on the small slide show toward the bottom of the screen.
[CC BY 4.0] UNLESS OTHERWISE NOTED | IMAGES: LICENSED AND USED ACCORDING TO TERMS OF SUBSCRIPTION