Introduction to Computer Programming
Scratch Lab #9
Lists
Table of Contents
Pair Programming:
Summing a List
Individual Lab:
Problem #1-Even or Odd
Problem #2-Distance and Regular Polygons
Question #1
Question #2
Question #3
Question #4
Question #5
Pair Programming:
Summing a List
Write a program in Scratch that will use a list. The list will store a group of random numbers and then sum up that list of random numbers.
Step One: Go to the data section and make a list named numbers. Numbers will be used by all sprites and will be used to store a list of random numbers.
Step Two:
- Add in code so that when the green flag is clicked 10 random numbers ranging from 1 to 50 are added to the list.
- Add in code so that when the spacebar is pressed all numbers are removed from list.
- Add in code so that when the down arrow is pressed all of the numbers are added to a variable named sum.
- Add in code so that when the up arrow is pressed, sum is reset to 0.

Individual Lab:
You must complete 1 of the following 2 problems. If you adequately complete those problems and still have time, you are expected to continue working on the other problems. You are assessed on both the correctness of your code as well as full use of the lab time given during class.
You need to be able to answer all of the following questions. Your answers will be posted on your website.
Problem #1-Even or Odd
You will write a simple calculator that can determine if a number is even or odd.
- You will need to use some modular arithmetic (looking at the remainder of a division problem). What is special about even numbers and odd numbers when they are divided by two.
- Your program should have the following output:
- User enters 3 (or any odd number)

- User enter 4 (or any even number)

- Try to do this with only one condition. That is, you don’t need two if statements to make this work. Be sure to try it for multiple types of numbers.
Once you have your calculator working, create your own block called “calculate Even or Odd”.

- Create a list and have prompt the user for numbers. Think about how you will end this prompt. Look at the NOTES if you get stuck
- Loop through the list and using your even/odd block determine if each entry is even or odd.
- Track the number of even and odd numbers with different variables.
- If more even numbers are given than odd numbers then have the sprite change costume.
Problem #2-Distance and Regular Polygons
Use the more blocks function within scratch to create a block that calculates the distance between two points. Note, you should access values from a list. One option is to have a list of x-values and another list that stores the y-values. You should call this function on a sprite as follows:

To test your calculator, the following outputs should occur:
- (0, 0) and (3,4) give a distance of 5
- (0, 0) and (5, 12) give a distance of 13
Using your code block from the above question, create a program that allows a user to determine if a polygon is a regular polygon (all sides equal length) by entering the coordinates of the polygon. The pseudocode for this is something like:
- Prompt user for the number of sides on polygon
- Ask the user for, and then populate x and y coordinates in the appropriate lists (assume vertices are given consecutively)
- Loop through lists and determine if the distance between between vertices is the same
- If the distance are the same, state that it is a regular polygon, otherwise it is not
Question #1
What is the length of an “empty” list?
Question #2
Why are lists more powerful than variables?
Question #3
Can a list contain different data types? For example, could it store both numbers and words?