2015-10-22

20151022 Graph Paper Programming

The main goal of this activity is to help demonstrate how “coding” works.


MATERIALS:
  • Sample Drawings Page (2-sided, at front):
Screenshot from 2015-10-22 00:59:09.png
Screenshot from 2015-10-22 00:59:29.png


  • Large grid graph paper (at front)
  • Markers, pens, or pencils (two or three colors)


REMEMBER:
  • Don’t write on the Sample Drawing Page.
  • Write your Full Name, Student Number and Block on your Grid Paper
  • Sign your name if you check someone else’s code.
  • Hand in your Grid Paper for marking and return  the Sample Drawing Page for use with the next class.


VOCABULARY:
Algorithm—A series of instructions on how to accomplish a task
Coding—Transforming actions into a symbolic language
Debugging—Finding and fixing problems in code
Function—A piece of code that can be called over and over
Parameters—Extra bits of information that you can pass into a function to customize it


INTRODUCTION:
What is a robot? Or more specifically, does a robot really “understand” what people say? The answer to the last question is: “Not the same way that a person does.”
Robots operate off of “instructions,” specific sets of things that they have been preprogrammed to do. In order to accomplish a task, a robot needs to have a series of instructions (sometimes called an algorithm) that it can run.
To get more familiar with the concept of an algorithm, it is helpful to have something to compare it to. For this exercise, we will use the “Lines & Arrows” programming language.
Program-Key.png
In the above Programming Key, the symbols on the left are the elements of the “Lines & Arrows” program language and the words on the right are the instructions used to create the “algorithm”. This means that we could write the algorithm: “Move one square forward, Move one square forward, Fill-in square with colour” and that would correspond to the program:
example_program.png
CODING PRACTICE I:
This is a simple 3x3 or 9-Square drawing.
Screenshot from 2015-10-22 00:43:22.png
A possible algorithm to reproduce this drawing could be:
“start in the top, left corner, 
step forward, fill-in, step forward, next row,
back, back, fill-in, step forward, step forward, fill-in, next row,
back, back, step forward, fill-in, step forward”


You may notice that there are some unnecessary steps, but we will worry about them at the next stage.


Programming the algorithm into the “Lines & Arrows” program language would look like this:
Screenshot from 2015-10-22 00:40:09.png
Let’s work through the example again, and remove unneeded steps from the original program.  For example, we could assume that the “next row” step implies returning to the left hand margin of the page; it is unnecessary to use the “back, back” steps, when we are simply going to “step-forward” over the same square again.  Also, do we need to “step forward” if we are not going to “fill-in”?


A less-redundant algorithm could be:
start in the top,left corner,
step forward, fill-in, next row,
fill-in, step forward, step forward, fill-in, next row,
step forward, fill-in”


On your graph paper, encode the above algorithm into the “Lines & Arrows” program language.


Exchange it with a partner and check each other's work by drawing whatever shape has been encoded in the programming language.


CODING PRACTICE II:


The following image is twice the linear dimensions (6x6 square), and therefore 4x the area (36 squares, instead of 9) of the previous image.


Screenshot from 2015-10-22 00:43:43.png


Let’s look at just three lines from a possible algorithm:
start in the top, left corner,
step forward, step forward, fill-in, step forward, fill-in, next line
step forward, fill-in, step forward, fill-in, step forward, fill-in, step forward, fill-in, next line
fill-in, step forward, fill-in, step forward, fill-in, step forward, fill-in, step forward, fill-in, step forward, fill-in, next line”

It becomes obvious that an image of this size is will require a lot of writing.  One possible way to do this is to use parentheses to group together multiples steps repeated together and numbers to represent the number of repeats.  


For example the previous algorithm could be re-written for the entire image as follows:
start in the top, left corner,
step forward, (step forward, fill-in) twice, next line
(step forward, fill-in) four times, next line
(fill-in, step forward) five times, fill-in, next line
(step forward, fill-in) four times, next line
step forward, fill-in, (step forward) three times, fill-in, next line
step forward, fill-in, (step forward) three times, fill-in, next line


On your graph paper, encode the above algorithm into the “Lines & Arrows” program language.


Exchange it with a partner and check each other's work your work.


CODING PRACTICE III:


Look at the following image:


Screenshot from 2015-10-22 00:46:18.png


On your graph paper, first write an algorithm and then encode it into the “Lines & Arrows” program language.


Exchange it with a partner and check your work.


THE MAGIC


Now comes the magic! You have just discovered functions! You have created a simple representation for a complex grouping of actions. That is exactly what “Functions” are meant to do.
How about the number? It has a name also. That number is called a “Parameter”. In the case of the example above, the parameter lets the function know how many times to repeat (remember Loops & Iterations?).


Armed with this new method, your have two choices for a final acitivity:


CHOICE 1:
  1. Choose image from image pack.
  2. Write out an algorithm to draw that image.
  3. Convert the algorithm into a program using symbols.
  4. Add “functions” to make programs more simple..
  5. Exchange it with a partner and check your work.
  6. Write programs for more complex images.
  7. Exchange it with a partner and check your work again
  8. Repeat as many times as you can.


Screenshot from 2015-10-22 00:53:28.png


CHOICE 2:

  1. Follow the link to the Google Image search for graph paper pixel art
  2. Choose an image.
  3. Write out an algorithm to draw that image (in black and white).
  4. Convert the algorithm into a program using symbols.
  5. Add “functions” to make programs more simple..
  6. Exchange it with a partner and check your work by drawing whatever shape has been encoded in the programming language.