Upload
jasmin-shepherd
View
213
Download
0
Embed Size (px)
Citation preview
2012 CSE-BEACON camp 1
Computer Science Concepts
1. What is an algorithm?2. Binary information coding3. Programming concepts via
ScratchDesigned for learners 10 years old up to 100.
Materials for hands on activities
2012 CSE-BEACON camp 2
1) a set of cards with numbers, say, 42, 17, 43, 55, 20, 32, 122) A set of cards with binary place values: 1, 2, 4, 8, 16
3) Scratch downloaded from www.scratch.mit.edu
4) www.learnscratch.org for tutorials for homework
5) www.cse.msu.edu/~stockman for this and other ppt sets
2012 CSE-BEACON camp 3
What is an algorithm?
What is an algorithm? Algorithms for average and min
An algorithm for sorting
2012 CSE-BEACON camp 4
Algorithm for computing the average of two numbers
Let the first number be A; say A=12 And the second number be B; say B=15 To compute the average _____________ So the average of 12 and 15 = ________ WHAT IS THE AVERAGE ALGORITHM?
2012 CSE-BEACON camp 5
What is an algorithm?
An algorithm computes some output from the given input using precise steps that a machine can follow (machine or other agent)
Hands on activity: finding MIN
Organize into 2-person teams Team has “person A” and “person
B” Each team has a stack of cards There is one number on each card No one looks at numbers yet
2012 CSE-BEACON camp 6
2012 CSE-BEACON camp 7
problem: finding min Person B is the “worker” for A. Person A has cards, but cannot look at the
numbers. A must find the min of all cards. B can look at only 2 numbers at a time --
B returns to A the smaller smaller first. HOW CAN PERSON A FIND THE MIN? Shuffle the cards A and B switch roles: new person A finds
MIN
2012 CSE-BEACON camp 8
review: team finding the min Person A can look at no numbers! No numbers can be seen by A, EVER! Person B can look at only two numbers on
cards given by person A. Person B gives back to A first the card with the
smaller number (if numbers are = then it doesn’t matter which is given first)
Person A must find the min and show it to the instructor WITHOUT EVER SEEING IT
A and B should switch roles and repeat.
Thought Questions If A has 10 cards with numbers, how
many times does A call on B? _____ If A has 100 cards, how many times
does A call on B? _____ If A has N cards, how many times
does A call on B? _____ Finding the MIN of a list of numbers
has “complexity” _______
2012 CSE-BEACON camp 9
Thought Question
Suppose A has 2 assistants – B and C
Can A find the MIN any faster? HOW?
Computers and computer programs can be structured to do more than one operation at a time.
2012 CSE-BEACON camp 10
2012 CSE-BEACON camp 11
Problem: how to sort?
Use set of cards for each team. How can A sort using B
as comparer?TRY IT: instructor will check
your final output
2012 CSE-BEACON camp 12
Sorting two-person activity This is a 2-person exercise. Person A CANNOT
see the numbers – EVER! To solve the sorting puzzle, person A must form
an ordered list of cards, which the instructor will check for order (this is the OUTPUT).
Person B can only compare two numbers by seeing the numbers. Person B gives A the smaller number card first (precise operations).
Person A can only ask Person B which of two cards has the smaller number. Person A never ever looks at any numbers.
To solve the sorting puzzle, person A must start from an unordered list of cards (this is the INPUT).
2012 CSE-BEACON camp 13
Sorting thought questions How many times will A call on B to sort 10
numbers? _____ How many calls to sort 100 numbers?
_____ How many calls to sort N numbers? _____
There are many sorting algorithms. Some are fast when N is small but slow when N is big. Some are the opposite. Computer scientists learn these algorithms and many others.
Algorithms often do operations over and over
Called “looping” Also called “iteration” or
“repetition” “Recursion” is a related idea – first
select the MIN from the list and then “recurse” on the rest of the list
2012 CSE-BEACON camp 14
2012 CSE-BEACON camp 15
Binary Information Coding
1. Only 2 symbols: WHY?2. Can we code numbers,
characters, music, images, movies, car designs using just 2 symbols?
Coding activities
1. Guessing the number of fingers in decimal2. Guessing 1 finger versus 0 fingers3. Encoding small integers using binary cards4. Coding using animate versus inanimate
objects (“My teacher plays rock, paper, scissors with me.”)
* Instructor quickly brings, say, 7 fingers from behind the back. Students make errors in guessing the number. Few errors in 0 versus 1 discrimination.
2012 CSE-BEACON camp 16
Fundamental coding principle
Can rep 2 “things” with 1 bit: yes/no; smiley/sad; high/low; odd/even
Can rep 4 “things” with 2 bits; 00:clubs; 01:diamonds; 10:hearts; 11:spades
Can rep 8 things with 3 bits 0 00 1 00
0 01 1 01 0 10 1 10 0 11 1 11
2012 CSE-BEACON camp 17
Fundamental coding principle
Each time we add one more bit, we __________ the number of binary strings
There are _____ binary strings of length N=7 (think about ASCII code)
There are thus ______ possible binary strings of length N bits
2012 CSE-BEACON camp 18
2012 CSE-BEACON camp 19
ASCII code for characters
CHAR CODE CHAR CODE
A 001000001 a 001100001 B 001000010 b 001100010 C 001000011 c 001100011 + 000101011 . 000101110 * 000101010 % 000100101
How many characters does a computer need to use?
Old Univac system used 6 bit char – a design disaster: _____ symbols max
IBM 8-bit code enabled ____ symbols (digits, caps, small letters, punctuation, graphic symbols, bell, null, etc.)
Unicode uses 16 bits to code all of the above and international characters:
______ symbols.
2012 CSE-BEACON camp 20
2012 CSE-BEACON camp 21
CD has many tracks of spots
Shiny spot is 0 Burned spot is 1 5 billion spots total 700 MB total 44,000 x 16 spots
for just 1 second of high fidelity music
Laser reads shiny versus dull spots
CD
CD spins
What’s a “CD”?
Even parity to detect errors
To the 7-bit ASCII code, append one parity bit to have an even # of 1 bits
Sender always sends an even number of bits
If some disturbance causes a bit to change, receiver gets an odd number or 1-bits in the char code.
If error, receiver asks for retransmit.
2012 CSE-BEACON camp 22
Bit strings as numbers
Binary Places Decimal equivalent
16 8 4 2 1 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 1 0 2 0 0 1 1 1 7 1 0 1 0 1 21
2012 CSE-BEACON camp 23
2012 CSE-BEACON camp 24
Binary Ops easy for machine Addition has few
rules.
Multiplication has few rules
These can be carried out in nanoseconds by computer circuits
0+0 = 0 0+1 = 1+0 = 1 1+1 = 0 with 1
carry
0x0 = 0x1 = 0 1x0 = 0 1x1 = 1
0 is 1 is
What number? ______
What number? ______
What number? ______
What number? ______
272012 CSE-BEACON camp
Binary coding in a computer
Symbol 0 can be represented by +5V and 1 by -5V
0 can be darkness and 1 lightness, as used on CDs and DVDs or in optical fibers
2012 CSE-BEACON camp 28
2012 CSE-BEACON camp 29
Conversion of decimal to binary: Algorithm #1
Let D1 be a decimal integer
Let R1 = remainder of D1 divided by 2; R1 is the 1’s bit
Let D2 be the dividend of D1/2
Let R2 = remainder of D2 divided by 2; R2 is the bit in the 2’s place. & so on
Decimal to binary conversion
2012 CSE-BEACON camp 30
INPUT: a decimal numberOUTPUT: a binary numberPRECISE STEPS: repeated division by 2; recording the remainderTERMINATES?: always (why?)CORRECT?: why?
Guessing games
2012 CSE-BEACON camp 31
• Guessing birthdays: just day of month• Guessing month and day • Guessing face value of cards• Guessing suit and face value
32
Programming concepts via Scratch
Free download of software available atwww.scratch.mit.eduVideo tutorials for learning available atwww.learnscratch.org
2012 CSE-BEACON camp
33
What can we do with Scratch?
• Learn some computing concepts.• Learn some practical algorithms.• Use Scratch as computing tool.• Have fun with Scratch creating
stories, games, art.
2012 CSE-BEACON camp
34
Start scratch and let’s go!
• In the lab, Click on the cat icon
• Scratch programming environment comes up
• First do simple things
2012 CSE-BEACON camp
35
Explore Scratch environment
• Stage is at upper right (where actors act and information is displayed)
• Sprite is another name for actor.• Instruction menus/panel at left
(instructions for the actors)• A script is another name for program or
method; a script tells the actor what to do.
• Programming area in center; here is where we construct scripts for the sprites
2012 CSE-BEACON camp
The VARIABLES Menu Implement an average algorithm Make a variable “FIRST” and set it to 12 Make a variable “Second” and set to 15 Make variable “Average” Use OPERATIONS Menu to set the
Average to (FIRST+SECOND)/2 Test Scratch and algorithm
2012 CSE-BEACON camp 36
Square root by repetition Guess sqrt(N) to be 1.0 Set root1 = 1.0 (first factor) Set root2 = N/root1 (second factor) root1 is not likely to = root2 Reset root1 to the average of root1 and root2
(it will be in between them, why?) Reset root2 = N/root1 Repeat all of the above until root1 and root2
are “very close”
2012 CSE-BEACON camp 37
Algorithm study is RICH! Algorithm to find your BEST mate? What if you had the coding of genes for
all possible mates? Algorithm to make bet on Big 10 game? Algorithm to find best molecule to bond
with a given input molecule? Algorithm to bond 2 molecules to create
“best properties”.
2012 CSE-BEACON camp 38
Information theory is RICH
SETI: Search for Extra Terrestrial Intelligence
Jodi Foster movie “Contact” What are the chances that a
random signal from space codes “I love you”? Or “X^2+Y^2 = R^2”?
2012 CSE-BEACON camp 39
40
The LOOKS menuHas instructions for setting the
color, size, and visibility of a sprite. Costumes will be used later.
2012 CSE-BEACON camp
41
The “hello” script (program)
• Choose the Looks menu• Click on the “say hello” lego block• Check your sprite behavior at the
right• Then click “say hello for 2 secs”
Your very first Scratch program!
2012 CSE-BEACON camp
42
Try some other looks operations (click on menu items)
• Change color effect by 25• Change color effect by 25 again• Hide• Show• Change size by 10• Change size by 10 again• Set size to 100%
2012 CSE-BEACON camp
43
Let’s write a script to
• Say “hello” for 2 seconds• Then change color by 25• Then think “Hmm..” for 4 seconds• Then change color by 75• Then change size by 200
Drag each instruction from the menu to the center script area. Connect them into a single block. Edit the parameters to get the numbers we want.
2012 CSE-BEACON camp
45
A sequence or block is a simple script or program
• The first instruction is done first• The second instruction is done
second• The last instruction is done last.• (if any one instruction is done, then
every on of them is done)
2012 CSE-BEACON camp
46
The WAIT instruction
• WAIT is needed to slow down the acting so we can see or hear it properly (computers are too fast sometimes)
• Get the wait instruction from the CONTROL menu. Ignore the other menu options for now.
• Insert a wait in our looks script
2012 CSE-BEACON camp
48
Student exercise: write a script to do the following
• Double the size of the sprite• Wait 2 seconds• Change the color of the sprite to
green• Wait 4 seconds• Change the whirl effect to 100• Say “That’s all folks!”
2012 CSE-BEACON camp
49
The MOTION menuHow to locate and orient a sprite; and, how to move a
sprite.
2012 CSE-BEACON camp
50
Position on the stage
• Using the Looks menu, shrink our cat to 25%.
• Click on the Motion menu.
• Click to check the box for x-position and y-position
Drag your cat around and note its x-y position.
2012 CSE-BEACON camp
51
Exercises: goto instruction(do you know about coordinates?)
• In the Motion menu, drag the “goto xy” instruction to the script panel.
• Edit the coordinates and click to see the sprite’s position
• A) goto x=200, y=0• B) goto x=-200, y=0• C) goto x=200, y=-100
Where does the cat go?
2012 CSE-BEACON camp
52
The “glide-to” instruction
• Drag the “glide-to” instruction into your script panel.
• Edit the coordinate values and click to see where your sprites goes.
2012 CSE-BEACON camp
53
Exercise
• Create a script to glide the sprite along the sides of a triangle. The first vertex of the triangle is (-100, -100). The second vertex is (200, -100). The third vertex is (50, 100). Make sure you complete the triangle.
• Change the speed of gliding and run again.
• New feature: click on Pen Down in the Pen Menu and run it again
2012 CSE-BEACON camp
Exercise: A fun looping program
Position your sprite at the lower left Put the pen down Do the following forever Rotate +5 degrees Move around an equilateral triangle
as before (turning 120 degrees each angle)
2012 CSE-BEACON camp 54
Exercise: bat flying randomly
2012 CSE-BEACON camp 55
Make a bat fly by flapping between 2 bat “costumes”. Use random values of coordinates x and y.Use arrows keys to control the bat’s position.* Use a forever loop to make the bat continue to fly when the flag is clicked* See if you can create a “score” variable and use it to count how many times the user clicks on the bat.
Exercise: find the MIN of a list
Explore how to create a list variable
Explore how to input numbers to the list
Explore how to pass through the list to set a variable MIN to the minimum in the list
Some hints follow2012 CSE-BEACON camp 56