Showing posts with label Functions. Show all posts
Showing posts with label Functions. Show all posts

Saturday, February 28, 2015

A Scratch Project for Drawing Tangrams and Compound Figures


Here is my Scratch project that draws two sample tangrams and the compound figures from this assignment (using Pro-Bots) created for grade 5 students.

I have created 5 different procedures for drawing the 5 components of the tangram. Please note that this is just one way of coding the figures; there are multiple correct ways of writing the programs for the same.


The functions (procedures) that are used for drawing the various polygons in the tangram can be found under the blocks titled:
  • parallelogram
  • square
  • largeTriangle
  • medTriangle
  • smallTriangle

The functions (procedures) for the compound figures that use the square and the triangle can be found under the blocks titled:
  • Square+Triangle
  • Square+Triangle_2

The functions (procedures) for the compound figures using the parallelogram and the small triangles can be found under the blocks titled:
  • TriParall_1
  • TriParall_2

I have given the code for two tangrams here as samples: one is the Monk with No Foot from the famous "Two Monks Paradox" and the other is a tangram for a House. The functions (procedures) for these can be found under the blocks titled:
  • MonkNoFoot
  • HouseTangram



Note:   The various dimensions in the assignment for Pro-Bot have been multiplied by a factor of 10 in my Scratch project, as we are dealing with pixels here and not centimeters, as in the case of Pro-Bot. 


Thursday, July 31, 2014

Solutions: Art with Pro-Bot - A Soccer Game

The soccer game picture is a follow-up to the "Art with Pro-Bot: A Dancing Robot" exercise, with one of the figures being the exact same as the Dancing Robot and the other being its mirror image.


Let's name the figure on the left as Team A and the one on the right as Team B. You would recognize from the figure that both Team A and Team B share some parts in common, such as the head and the neck. So, it's easier for the programmer to write the code for these parts just once and store it as a procedure; it can then be used multiple times. 

// Proc 1 - procedure for the neck and head


Fd 1
Lt
Fd 1
Rt
Rpt 3 [
Fd 2
Rt
]
Fd 1





Team A:  


// Program written in Main and using the above procedure. The Start point is marked in the figure.


Rpt 4 [
Fd 10
Bk 4
Rt
]
Fd 3
Lt
Proc 1


Team B:


// Program written in Main and using the above procedure. The Start point is marked in the figure.


Rpt 4 [
Fd 10
Bk 4
Lt
]
Fd 3
Rt
Proc 1


Art with Pro-Bot: A Soccer Game


Let’s use Pro-Bot to draw a picture of robots playing soccer.

We shall work using both Repeat Loops and Procedures in Pro-Bot for this assignment. This "art" project was designed to use the concepts of Repeat Loops and Procedures in a fun way.

There are 2 robots in the picture, one representing Team 1 and the other, Team 2. The two robots are playing each other in a game of soccer. The dimensions are as follows: Each robot's body is a 6 cm side square, the limbs are 4 cm long, the neck is 1 cm long and the head is a 2 cm side square. 

While writing the programs for Pro-Bot to draw the figures, remember to use Repeat Loops for patterns that repeat. You can also see from the pictures that the head and neck is exactly the same for both pictures. Hence, you can write the program for the head and neck once, store it as a procedure in Pro-Bot's memory and call it for drawing both robots. 

Click here for a set of solutions.

Draw in as many members for each team as you like. You can even store the programs for each robot as separate procedures which makes it easier for you to draw multiple figures in your art project. You can put in a background of your choice and draw a soccer ball in the middle once you have drawn both teams. It does not have to be just soccer; students in my class have used this theme to depict scenes from various sporting events. We worked on this project the day after a major basketball game and there were quite a few children who made pictures with basketball scenes... Let your imagination run free in creating your art work. 























Wednesday, July 30, 2014

Solutions: Procedures: Revisiting Squares

The Procedures in Pro-Bot are pre-named from Proc1 to Proc32.


1.  Let's write the program for the square and store it in a procedure called Proc1. Access the New Proc or Edit Proc options from the Menu button to access the procedures and  choose Proc1. Here's what Proc1 might look like:

// Remember that you would see Proc1 as the first line on the screen here, not Main.

   Rpt 4 [
   Fd 6
   Rt
   ]


// To run this Procedure, go back to Main by clicking on the Menu button. Call Proc1 from Main using the Proc key and the number 1 on the control pad. Then press Go and watch your Procedure execute.



2.   Here is one way to use Proc1 from the above program to draw the stack of squares.

     Rpt 3 [
     Proc 1
     Fd 6
     Rt
     Fd 6
     Lt
     ]





3.   To draw the coaches for the train using Proc1, you could use the following code:
   
    Rpt 3 [
    Proc 1
    Rt
    Fd 8
    Lt
    ]

Procedures: Revisiting squares

The following programs are intended to provide practice with writing procedures in Pro-Bot, storing them in Pro-Bot's memory and calling them.


(1)  Write a program for Pro-Bot to draw a square of sides 6 cm. Store this program as a procedure on Pro-Bot (say proc1). Now call this procedure (proc1) from Main to test and see if Pro-Bot draws a 6 cm square.



(2)  Now that you have your program for drawing a 6 cm square stored in Pro-Bot's memory, any time you want to use it, you can call it either from your Main program or from another procedure.
You may recall the figure below from the Nested Loops assignment.




















Can you use the procedure that you have in Pro-Bot's memory to draw the above figure now? Rewrite your previous program for Pro-Bot to trace this figure, using the procedure for the square.




(3)   Let's revisit the Trains project from Nested Loops. Can you rewrite your previous program for Pro-Bot to draw the coaches of the train using the above procedure?




















In all of the projects above, you can see that you did not have to write the code for the square anew each time. All you had to do was write the program for the square once, store it as a procedure and then call the procedure any time you needed its functionality. You were able to use the same procedure for different programs.


Here is a set of solutions.

Procedures in Pro-Bot


Let’s start this lesson by taking a look at your typical school day. What are some of the functions that you perform on a given school day? Here are a few: you wake up, make your bed, brush your teeth, take a bath, get dressed, eat breakfast, travel to school, study, play, eat lunch, study again, travel home, play, sports/activities, watch TV, read a book, do homework, eat dinner, brush your teeth, sleep... If you look closely at each of these functions, you notice that each one of these involves multiple steps.

For example, brushing your teeth more or less involves the following steps: (1) pick up toothbrush (2) pick up toothpaste (3) squeeze the toothpaste onto the brush (4) actual brushing of the teeth (5) open the tap for water (6) fill up your glass (7) close tap (8) rinse your mouth (9) put away the toothbrush and toothpaste.

We bunched up all these steps and gave it a unique name: “brush teeth”. When your dad or mom wants you brush your teeth, do they say all these 9 steps one by one, or do they just say “brush teeth”? Why? Because you understand what “brush teeth” means. You have been taught in your younger days what that means and how to do all those 9 steps. All those steps are in your memory. So when you hear it, you know all the steps to do.

Now let us take a look at a computer… like your Pro-Bot. Don’t you think if a computer is first told how to draw a 6 cm side square, then each time you want to draw a 6 cm side square, you should be able to say a unique name like “square” or draw square”, or something like that, instead of telling it each time “Fd 6, Rt, Fd 6, Rt, Fd 6, Rt, Fd 6, Rt”? Guess what? The computer can do that! And that is what we call “Procedure”.

A procedure is a set of computer program instructions that performs a specific task.
  
In your daily life example, “brush teeth” is a procedure. It is a small group of instructions that performs a specific function. Any time your parent wants you to perform this function (which involves those 9 steps), he or she would just call it by the name “brush teeth”, rather than specify every step involved in it. This way, they get to reuse the “procedure” they taught you and made you memorize. Similarly, in a computer program, you write those steps once into the computer’s memory and give it a unique name. Then you can use it multiple times in your program.

Maybe, the dentist has advised you and your parents to include flossing into your routine. Flossing is also a procedure just like “brush teeth”. It involves some of these steps: (1) take the box of floss, (2) cut off about 10 inches of the flossing thread (3) coil it around your fingers (4) insert it between 2 of your teeth and pull (5) repeat this for all the gaps between your teeth (6) uncoil the string from your fingers (7) throw it into the garbage. Your parents work with you to teach you these steps until you have learned it. Then while brushing your teeth, he or she may remind you to “floss”. They don’t have to tell you all those 7 steps every time anymore. So here you can see that the “floss” procedure is called from within the “brush teeth” procedure.

Now let’s go back to the computer. Let’s say you want it to make a beeping sound after you draw the square. What would you do? You could write a procedure to make that beeping sound. Then call that “beep” procedure from within your “square” procedure after all those steps to draw the square. In this way, you just easily modified your first procedure to do something different.

Thus, you can call other procedures from within a procedure.

Back to our “brush teeth” example. You may decide to floss before you do other steps in your “brush teeth” procedure, or after you have done those steps. Similarly you may decide to make the Pro-Bot “beep” before it starts drawing the square instead of after drawing the square. Can you see how easy that would be? You just have to call the “beep” procedure first instead of last within your “square” procedure.

You can think of “school day” as a Program, and functions like “wake up”, “make bed”, “brush teeth”, etc., as Procedures inside the program. Now, try writing two different programs using the procedures for the daily functions: one program for a school day and the other program for a holiday. You can easily see that you would not call every one of the procedures for either program. There might be some procedures that are common to both programs and there might be some that are exclusive to each. Such as, you don’t “travel to school” on a holiday, but you “brush teeth” every day. You might also call them in different orders for the two programs. For example, you may watch TV at different times during a holiday, than on a school day.

Procedures thus help us to customize our programs easily.

We already mentioned that procedures and programs can call other procedures from within. They can be called multiple times as well. You can write a procedure for drinking water. You might call this procedure multiple times throughout the day, while performing other functions. This means that you can call “drink water” from within your “eat lunch” procedure or from within your “play” procedure or from within your “do homework” procedure, etc.


How to create and edit a procedure for Pro-Bot
Pro-Bot lets you create and store up to 32 procedures in its memory. In the case of Pro-Bot, these procedures are named Proc 1, Proc 2, …, …, Proc 32.
Pro-Bot’s Menu button provides two options to access procedures:
·      New Proc
·      Edt Proc

Let’s discuss each of these options.
1.   New Proc:
Press the Menu button & then scroll down to “New Proc”. Choose this option when you want to create a new procedure. It will show you a list of 32 procedures on the screen. Choose any procedure name from this list (Proc 1 to Proc 32). Let’s say that you choose Proc 3. Your Pro-Bot screen will now show Proc 3. Type in the instructions for the specific function that you want Proc 3 to perform. Next, press Menu. This will take you back to the Main program. You can now call Proc 3 from your Main program anytime you want to use it. You do this by pressing the “Proc” button and then the number key 3.

2.   Edt Proc:
Press the Menu button & then scroll down to “Edt Proc”. Choose this option when you want to edit a procedure that you created earlier. It will show you the list of 32 procedures on the screen. Choose the procedure that you want to edit from this list. Let’s say that you choose Proc 3. Your Pro-Bot screen will now show the instructions that you put in earlier for Proc 3 (or an empty program if you put in no instructions earlier). Make the changes that you want for this procedure. Next, press Menu to go back to the Main program. You can now call Proc 3 from your Main program and it will provide you with the updated functionality.

For example; you can write a procedure to draw a square of side 6 cm. Let’s say we name this procedure “Proc 1”. Proc 1’s code will be “Rpt 4 [ Fd 6   Rt ]”. You can store this procedure in memory and anytime you need to draw a 6 cm square, you just call Proc 1 from your Main program.


In addition to the 32 procedures that you can create and edit, Pro-Bot also provides 5 other procedures with pre-defined names related to the touch, light & sound sensors. You can edit these using the Edt Proc option from Menu. These procedures are:
·      33 FRONT (runs when the front touch sensor is triggered)
·      34 REAR (runs when the rear touch sensor is triggered)
·      35 DARK (runs when the light sensor goes from light to dark)
·      36 LIGHT (runs when the light sensor goes from dark to light)
·      37 SOUND (runs when the sound sensor is triggered)

There are also 3 built-in, pre-defined procedures that you cannot edit, but can call from Main. These are:
·      38 HEXGN: draws a hexagon
·      39 DIAMND: draws a diamond
·      40 FLOWER: draws eight diamonds using Proc39

Note: You can call other procedures from within any other procedure. Just take care not to run procedures that call each other, in which case you would get into an endless loop.






Tuesday, July 29, 2014

Art with Pro-Bot: Snails

Write a program for Pro-Bot to draw a picture of snails as shown below. 

The bigger snail has a square head of side 4 cm and a 4 cm long neck. The outer square of its shell has 12 cm sides, the middle one 8 cm & the inner one 4 cm. The smaller snail has a square head of side 2 cm & a 2 cm long neck. The outer square of its shell has 4 cm sides & the inner one 2 cm.


Remember to use Repeat Loops when you write programs for drawing squares using Pro-Bot. We shall reuse the pattern for the larger snail's shell from the Repeat Loops Maze project. 



Once you are done drawing the body of the snail using Pro-Bot, you can draw in other features such as eyes, mouth, antennae, etc. You can also draw in more features in the background to make your picture more interesting. 

























I designed this project to let the children practice Repeat Loops and Procedures in a fun way using Pro-Bot. Repeat Loops were used for drawing the squares. The 4 cm side square was stored as a procedure and used multiple times in the figure. We did the same for the 2 cm side square as well. 


Here is a set of solutions.


Solutions: Art with Pro-Bot - Snowmen

Let's first write the programs for the squares of sides 6 cm, 4 cm and 2 cm.  We shall store these are three different procedures, say in Proc1, Proc2 and Proc 3.

Here is one way of writing the programs:


Proc 1      // procedure for 6 cm side square
Rpt 4 [
Fd 6
Rt
]


Proc 2      // procedure for 4 cm side square
Rpt 4 [
Fd 4
Rt
]


Proc 3      // procedure for 2 cm side square
Rpt 4 [
Fd 2
Rt
]



Now let's draw the small snowman using the above procedures. From Main, the program shall be written as:


// Program for the Small Snowman


Proc 2
Fd 4
Rt
Fd 1
Lt
Proc 3


You can use the above procedures for drawing the large snowman as well. From Main, the program for the large snowman can be written as:


// Program for the Large Snowman


Proc 1
Fd 6
Rt
Fd 1
Lt
Proc 2
Fd 4
Rt
Fd 1
Lt
Proc 3









Art with Pro-Bot: Snowmen


Write a program for Pro-Bot to draw some snowmen using three different sized squares. The squares have sides measuring 6 cm, 4 cm and 2 cm. Remember to use Repeat Loops while programming the squares. Draw in other features by hand for the snowmen (eyes, nose, mouth, scarf, hat, etc.) after you draw the snowmen’s bodies using Pro Bot.

























I designed this "art" project to allow the children to practice using Procedures as well as Repeat Loops. The squares were all drawn using Repeat Loops. The 4 cm side square and the 2 cm side square were stored as Procedures and reused in the figures. In fact, they can be even reused for other projects, like the Snails.

Some of the children imagined these to be wedding cakes instead of snowmen and drew in features accordingly... There was even a picture of a snowman eating a two-tiered cake.


Here is a set of solutions to the above.