Lab 4: Making Computers Do Math
This lab doesn't have an overarching project; it's a collection of different techniques involving functions. The most important ideas, one per page, in order:
- The
mod
(remainder) function
- Snap!'s "export blocks" feature to make libraries
- The higher-order function
combine
By the way, the use of language in which "do math" includes arithmetic functions but not Boolean functions comes from the AP CSP Framework. We do know that logic is part of math!
Pacing
The 3 lab pages could be split across 2–3 days (
60–120 minutes). Expected times to complete follow:
Lab Pages
-
Page 1: The
Mod
Operator.
-
Learning Goals:
- Understand the
mod
function.
- Use
mod
to build a divisibility checker predicate and a predicate to test whether a number is even.
-
Tips:
-
Mathematics Note:
Mod
. For positive whole number inputs, the mod
block returns the remainder when the first input is divided by the second.
If either input is negative, what's meant by "remainder" gets more complicated, and there are actually two different functions, "mod" and "remainder," with different answers. Negative numbers will not be used with mod
in this course.
- Resist the urge to pre-teach
mod
, and instead allow student to explore and experience the function before formalizing. After most students have considered the self-check questions toward the end, post similar questions, such as "What is the value of 13 MOD 2 + 1?" (2) or "What is the value of 13 MOD (2 + 1)?" (1), and have students share and discuss their thinking with the class.
- Mathematics Note: The integers (referenced in the If There Is Time activity) are the counting numbers together with their negatives and zero: ..., -3, -2, -1, 0, 1, 2, 3, ... The number 17 is an integer, and so is −17. But the number 17.3 is not an integer. Neither is π (pi). An integer is a number for which
round
returns the number itself. Students will use this fact to develop their integer?
predicate.
Any reason to keep any of this?
- Question 4 has handholding baby steps hidden behind a link. Students should understand that if they always use the hints throughout the curriculum, they'll end the year not having learned many programming skills. On the other hand, people often get stuck on small points that they really do already understand, but just aren't seeing that day. Maybe yesterday or tomorrow they'd find the same question easy. So it's okay for them to click the hint links when they've really tried, and they and their partner are both really stuck on the problem.
- A "number of" block is almost always done by first making a block to determine the actual items and then using
length
to count them. This is what's behind the hint link. But students should understand it as a general strategy.
-
Page 2: Making a Mathematical Library.
-
Learning Goals:
- Create a library of mathematical procedures.
- Learn about importing and exporting functions.
- Practice using
keep
.
-
Tips:
- See the "Block Libraries" section on the bottom of page 29 in the Snap! Reference Manual for more information about exporting and importing.
-
Page 3: More Mathematical Reporters.
-
Learning Goals:
- Use the
combine
block to develop several list functions.
- Add to students' collection of useful mathematical functions.
-
Tips:
- You may with to introduce the reporter block
, which works just like if
except that it is a reporter that takes only reporters as inputs (rather than being a command that takes commands as inputs).
Map
and keep
are versatile functions; almost anything might be used as its function input. Combine
, though, really makes sense only with +
, ×
, and
, or
, join
, join words
, and the blocks min
and max
that students write in this lab.
- Regarding
average of list
, note that it's incorrect to average two numbers, then average that result with the third number, and so on. That gives too much weight to the last numbers averaged in. Students must first add all the numbers, then, just once, divide by the number of numbers.
Solutions
Correlation with 2020 AP CS Principles Framework
Computational Thinking Practices: Skills
- 2.B: Implement an algorithm in a program.
Learning Objectives:
-
AAP-2.M: For algorithms:
- Create algorithms. (2.A)
- Combine and modify existing algorithms. (2.B)
- AAP-3.D: Select appropriate libraries or existing code segments to use in creating new programs. (2.B)
Essential Knowledge:
- AAP-2.B.5: The evaluation of expressions follows a set order of operations defined by the programming language.
- AAP-2.C.1: Arithmetic operators are part of most programming languages and include addition, subtraction, multiplication, division, and modulus operators.
- AAP-2.C.2: The exam reference sheet provides
a MOD b
, which evaluates to the remainder when a
is divided by b
. Assume that a
is an integer greater than or equal to 0 and b
is an integer greater than 0. For example, 17 MOD 5
evaluates to 2
.
-
AAP-2.C.3: The exam reference sheet provides the arithmetic operators
+, -, *, /
, and MOD
.
a + b
a – b
a * b
a / b
a MOD b
These are used to perform arithmetic on a
and b
. For example, 17 / 5
evaluates to 3.4
.
- AAP-2.C.4: The order of operations used in mathematics applies when evaluating expressions. The
MOD
operator has the same precedence as the *
and /
operators.
-
AAP-2.H.2: The exam reference sheet provides
IF(condition)
{
<block of statements>
}
in which the code in block of statements
is executed if the Boolean expression condition
evaluates to true
; no action is taken if condition
evaluates to false
.
-
AAP-2.H.3: The exam reference sheet provides
IF(condition)
{
<first block of statements>
}
ELSE
{
<second block of statements>
}
in which the code in first block of statements
is executed if the Boolean expression condition
evaluates to true
; otherwise, the code in second block of statements
is executed.
- AAP-2.L.2: Algorithms that appear similar can yield different side effects or results.
- AAP-2.M.1: Algorithms can be created from an idea, by combining existing algorithms, or by modifying existing algorithms.
-
AAP-2.M.2: Knowledge of existing algorithms can help in constructing new ones. Some existing algorithms include:
- determining the maximum or minimum value of 2 or more numbers
- computing the sum or average of 2 or more numbers
- identifying if an integer is or is not evenly divisible by another integer
- AAP-2.M.3: Using existing correct algorithms as building blocks for constructing another algorithm has benefits such as reducing development time, reducing testing, and simplifying the identification of errors.
-
AAP-2.O.4: Knowledge of existing algorithms that use iteration can help in constructing new algorithms. Some examples of existing algorithms that are often used with lists include:
- determining a minimum or maximum value in a list
- computing a sum or average of a list of numbers
- AAP-3.A.7: The exam reference sheet provides the
RETURN(expression)
statement, which is used to return the flow of control to the point where the procedure was called and to return the value of expression
.
-
AAP-3.C.2: The exam reference sheet provides
PROCEDURE procName(parameter1, parameter2, …)
{
<block of statements>
RETURN(expression)
}
which is used to define a procedure that takes zero or more arguments. The procedure contains block of statements
and returns the value of expression
. The RETURN
statement may appear at any point inside the procedure and causes an immediate return from the procedure back to the calling statement.
- AAP-3.D.1: A software library contains procedures that may be used in creating new programs.
- AAP-3.D.2: Existing code segments can come from internal or external sources, such as libraries previously written code.
- AAP-3.D.3: The use of libraries simplifies the task of creating complex programs.
- AAP-3.D.4: Application program interfaces (APIs) are specifications for how the procedures in a library behave and can be used.
- AAP-3.D.5: Documentation for an API/library is necessary in understanding the behavior(s) provided by the API/library and how to use them.