Lesson 4: Array Algorithms
45 minutes
Overview
How can I use arrays to find specific elements?
Students apply their knowledge of traversing 1D arrays to develop algorithms to find elements that have specific properties. Students learn how to identify the preconditions and postconditions for an algorithm. Additionally, students differentiate between executing a return
statement inside a loop and at the end of a method.
Standards
CON-2 - Programmers incorporate iteration and selection into code as a way of providing instructions for the computer to process each of the many possible input values
CON-2.C - Represent iterative processes using a whileloop.
- CON-2.C.5 - Executing a return statement inside an iteration statement will halt the loop and exit the method or constructor.
CON-2.I - For algorithms in the context of a particular specification that requires the use of array traversals: • Identify standard algorithms. • Modify standard algorithms. • Develop an algorithm.
- CON-2.I.1 - There are standard algorithms that utilize array traversals to: • Determine a minimum or maximum value • Compute a sum, average, or mode • Determine if at least one element has a particular property • Determine if all elements have a particular property • Access all consecutive pairs of elements • Determine the presence or absence of duplicate elements • Determine the number of elements meeting specific criteria
MOD-1 - Some objects or concepts are so frequently represented that programmers can draw upon existing code that has already been tested, enabling them to write solutions more quickly and with a greater degree of confidence
MOD-1.E - Call non-static void methods without parameters.
- MOD-1.E.2 - Procedural abstraction allows a programmer to use a method by knowing what the method does even if they do not know how the method was written.
MOD-2 - Programmers use code to represent a physical object or nonphysical concept, real or imagined, by defining a class based on the attirbutes and/or behaviors of the object or concept
MOD-2.C - Desribe the functionality and use of program code thorugh comments.
- MOD-2.C.3 - A precondition is a condition that must be true just prior to the execution of a section of program code in order for the method to behave as expected. There is no expectation that the method will check to ensure preconditions are satisfied.
- MOD-2.C.4 - A postcondition is a condition that must always be true after the execution of a section of program code. Postconditions describe the outcome of the execution in terms of what is being returned or the state of an object.
- MOD-2.C.5 - Programmers write method code to satisfy the postconditions when preconditions are met.
MOD-2.F - Define behaviors of an object through non-void methods with parameters written in a class.
- MOD-2.F.2 - Non-void methods with parameters receive values through parameters, use those values, and return a computed value of the specified type.
Agenda
Objectives
Students will be able to:
- Differentiate between executing a
return
statement inside of a loop and at the end of a method - Identify the preconditions and postconditions for an algorithm
- Implement an algorithm to determine if all elements in a one-dimensional (1D) array have a particular property
Preparation
- Print copies of the CS Bingo Cards (one card for each student)
- Print the CS Bingo Definitions resource and mix them up
- Print copies of the Exploring Algorithms handout (one for each pair of students)
- Create code review groups if you are not reusing the same groups
- Check the Teacher's Lounge for verified teachers on the CSA Forum to find additional strategies or resources shared by fellow teachers
Links
Heads Up! Please make a copy of any documents you plan to share with students.
For the students
- CS Bingo Cards - Handout
- Exploring Algorithms - Handout
- U3L4 Extra Practice - Handout
Vocabulary
- Postcondition - a condition that must always be true just after the execution of a code segment
- Precondition - a condition that must always be true just before the execution of a code segment
Teaching Guide
Warm Up (10 minutes)
CS Bingo
Remarks
We have learned a lot of new terms so far! Let's review some of these terms through a game of CS Bingo.
Distribute: Give each student a unique CS Bingo Card.
Do This: Review the instructions for playing CS Bingo.
Do This: Play the music clip to cue the CS Bingo activity. Choose a random definition and read it aloud to the class. Give students a moment to check their bingo card for the correct term, then state the correct term. Repeat until a student yells "Java."
To allow for multiple students to "win" the game, continue pulling definitions until a couple more students win as well.
Activity (30 minutes)
Exploring Algorithms (15 minutes)
Remarks
Software engineers create programs to find information in a data set, such as finding how many elements meet specific criteria. These tasks are so common that there are standard algorithms that work for many scenarios.
Do This: Review the lesson objectives.
Do This: Direct students to Level 1 on Code Studio to predict the program's outcome, then run the program to compare their predictions to the actual outcome.
Discuss: Click through the animated slide to display the prompts.
- What do you notice about the code in this program?
- What do you wonder about the code in this program?
Discussion Goal: Students notice that the program determines if all elements in the 1D array have specific criteria. Students notice a return
statement inside the loop that stops traversing the 1D array once an element is found that does not meet the condition. Students may wonder how they might use this algorithm in other scenarios or how this algorithm might change if they wanted to find how many elements have specific criteria.
Do This: Click through the animated slide to define precondition and postcondition.
Have students identify daily scenarios and tasks and their preconditions and postconditions. For example, ask students to identify preconditions and postconditions for doing homework or going to the movies.
Remarks
Preconditions and postconditions are useful for identifying what conditions must be met for a method to execute as intended and the expected outcome. Software engineers write method code to satisfy the postconditions when preconditions are met.
Discuss: Click through the animated slide to display the prompts. Use the Hold That Thought strategy to discuss the prompts.
- What is the precondition of this code segment?
- What is the postcondition of this code segment?
Discussion Goal: Students identify preconditions of the code segment, such as that the 1D array should not be empty. Students identify postconditions of the code segment, such as that the 1D array should not be modified.
If students struggle to identify preconditions and postconditions of the code segment, ask additional probing questions to help them determine conditions that must be true before and after the algorithm executes. For example:
- Should this algorithm be applied to an empty array?
- Should this algorithm only work for certain data types?
- Should this algorithm make changes to the array?
Group: Place students in pairs.
Distribute: Give each pair a copy of the Exploring Algorithms handout.
Do This: Direct students to Level 2 on Code Studio. Students work with their partners to explore the program and complete the Exploring Algorithms handout.
Remarks
When we have non-void methods with parameters, these methods receive values through the parameters, use these values, and return a computed value of the specified return type.
Discuss: What is the difference between executing a return statement inside a loop and at the end of a method?
Discussion Goal: Students note that returning inside a loop exits the loop as soon as the value is found and does not check the rest of the array. Students also note that returning at the end of a method ensures that the entire array is checked before returning the result.
Writing Algorithms with Arrays (15 minutes)
Remarks
Traversing a 1D array to determine if at least one or all elements have certain criteria is a standard algorithm that works for many scenarios.
Do This: Direct students to Level 3 on Code Studio. Students complete a choice level to implement an algorithm to find if at least one element has certain criteria.
Encourage students to write pseudocode for the algorithm on the Exploring Algorithms handout before writing a solution in Java.
Do This: Click through the animated slide to have students participate in the Code Review Call and Response.
Do This: Direct students to complete a code review on Level 4.
Wrap Up (5 minutes)
Software Engineering Skills
Remarks
You have learned a lot about how software engineers work with data in their programs! Let's take a moment to reflect on your progress in developing your software engineering skills.
Do This: Have students respond to the reflection prompts on the Unit 3 Guide.
Do This: Review the concepts covered in this lesson.
Display: Key Vocabulary
Assessment: Check for Understanding
Check For Understanding Question(s) and solutions can be found in each lesson on Code Studio. These questions can be used for an exit ticket.
AP Classroom Topic Questions
To assign questions from the AP Classroom Question Bank that align with this lesson, create a custom quiz in AP Classroom by searching the Question Bank for the Essential Knowledge statements listed at the top of this lesson plan. You can find instructions and video demonstrations to do this on AP Central.
The following Topic Questions in AP Classroom can be assigned as a formative assessment for this lesson:
- Topic Questions 4.1
- Topic Questions 5.3
Note: Some Learning Objectives and Essential Knowledge statements in the suggested Topic Questions are covered in later units.
This work is available under a Creative Commons License (CC BY-NC-SA 4.0).
If you are interested in licensing Code.org materials for commercial purposes contact us.