Section 7.7
Modular Arithmetic
Learning Goals
Students will evaluate expressions that use arithmetic operators (modulus). (AAP-2.C)
Students will write and determine the results of iteration statements. (AAP-2.K)
AAP-2.C: Evaluate expressions that use arithmetic operators.
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
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.
Objectives and Description
The objective of this lesson is to both introduce students to the modulo operator while also reinforcing previously taught concepts (specifically conditionals and loops). Students will be creating a looped drum beat, something they are already familiar with, and modifying the code to add a drum fill to a specific measure.
Activities
Activity 7.7.1 (Budget 50 minutes)
Navigate to AP Classroom and introduce students to Mathematical Expressions 3.3 Daily Video 3. You can stop the video at any time to have students evaluate expressions that use arithmetic operators.
Teacher will open their example script from lesson 7.3, which should contain a looped drum beat
Teacher will walk through how to use the modulo operator in boolean expressions
Modulo returns the remainder of dividing the left hand operand by right hand operand.
x % y returns the remainder of x/y
Examples:
7 % 4 returns 3
8 % 4 returns 0
The number returned can be used as an operand in a boolean expression, and specifically in EarSketch, this can be applied as a method to execute code on certain measures
Create a new variable that contains the beat string for a new snare part
Add conditional to the for loop, existing code will go into the “else:” section. Boolean expression “measure % 2 == 0” should be used, which returns TRUE on even measures
Inside of the “if” section of the conditional add a line that uses makeBeat() to play the new beat string created in step 3
The resulting code should play the original drum beat on odd measures and the new drum beat on even measures
Activity 7.7.2 (Budget 45 minutes)
Students will open their completed script from lesson 7.3, which should contain a drum beat looped over 8 measures.
Students will create 3 new variables with beat strings for use in makeBeat()
This beat string will be used in a drum fill, which is commonly used in music to signal the end of a phrase or a transition into a new section
Instruct students to add a conditional statement within their for loop that plays their original drum beat for 3 measures, then plays their new drum fill on the 4th measure.
As this is building off code from 7.3, their script will create a drum beat looped over 8 measures, so their drum fill should play twice
The drum fill should be added to the same tracks as their original drum beat
Bonus: instruct students to add risers or additional instrumentals to their song, specifically to be played alongside their drum fills
Resources
7.7.1: College Board's 3.3: Daily Video 3 titled, "Mathematical Expressions" available through AP Central and AP Classroom (Student and Teacher resource)
7.7.1: Modular Arithmetic Slide Deck (Student and Teacher Resource)
7.7.2: Activity Solution (Teacher Resource)
note: Solution is only a guide for implementation of the modulo operator and conditionals. Student beat strings will probably differ