< Unit 3 - Arrays and Algorithms ('22-'23)

Lesson 2: Array Elements

45 minutes

Overview

How do I modify elements in a one-dimensional (1D) array?

Students explore how to modify and obtain elements stored in a one-dimensional (1D) array. Students learn about common errors when working with 1D arrays and identify strategies for debugging these errors. Students practice modifying and printing elements in a 1D array individually and using a while loop.

CSA Conceptual Framework
      • VAR-2.A.6 - Square brackets ([ ]) are used to access and modify an element in a 1D array using an index.
      • VAR-2.A.7 - The valid index values for an array are 0 through one less than the number of elements in the array, inclusive. Using an index value outside of this range will result in an ArrayIndexOutOfBoundsException being thrown.

Agenda

Objectives

Students will be able to:
  • Access and modify elements in a one-dimensional (1D) array by referring to an index location
  • Explain the cause of an ArrayIndexOutOfBoundsException

Preparation

  • Print copies of the Investigating Arrays handout (one for each pair of students)
  • 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

Vocabulary

  • ArrayIndexOutOfBoundsException - an error that occurs when attempting to access an index that is negative, greater than, or equal to the size of the array

Teaching Guide

Warm Up (5 minutes)

Software Engineers and Data

Remarks

Data is all around us. Companies collect and analyze data to identify trends and make decisions about new products or services to offer or make recommendations based on things we search for or like. Software engineers create programs to store and process data to identify trends and find information.

Do This: Introduce the dataset.

Discuss: Click through the animated slide to display the prompts.

  • What does this make you wonder about how this data is stored and processed by programs?
  • What data would you want to collect and analyze? What would you want to find out?

Discussion Goal: Students suggest that data might be stored in a data structure like a 1D array. Students may wonder how the data is collected and added to a data structure in a program and ways they could analyze the data. Students share ideas of data they would want to analyze related to their personal interests, such as popular songs or movies.

Teaching Tip

Ask students about their personal interests or hobbies and the data that might be collected. You could also ask students to consider how social media services determine a topic is trending or how a company knows that a product they sell is popular.

Activity (30 minutes)

Modifying Values (15 minutes)

Remarks

We learned how to declare and initialize a 1D array using the new keyword and an initializer list in the previous lesson. We can also modify values to a 1D array after it has been initialized.

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 variable name, square brackets, and index refer to a position in the array. Students also notice that the assignment operator is used to assign a value to the specified position. Students may wonder if they can only change values one index at a time.

Teaching Tip

Ask students to identify the similarities and differences between assigning a value to a primitive variable and assigning a value to a position in a 1D array.

Group: Place students in pairs.

Distribute: Give each pair a copy of the Investigating Arrays handout.

Do This: Direct students to Level 2 on Code Studio. Students work with their partners to explore the programs on Levels 2 and 3 and complete the Investigating Arrays handout.

Display: Show the video – Modifying Values in a 1D Array.

Do This: Explain how to modify values in a 1D array.

Working with 1D Arrays (15 minutes)

Remarks

Adding, changing, and printing values in a 1D array is similar to working with variables. With 1D arrays, we need to use the square brackets to specify the index in the 1D array that we want to access. We can work with the contents of a 1D array individually and using a while loop.

Do This: Click through the animated slide to demonstrate using a while loop to print the contents of a 1D array.

Teaching Tip

Prompt students to explain the purpose of the index variable and the condition of the while loop. Ask students why the condition is index < scores.length and what they think would happen if the condition was index <= scores.length.

Do This: Define ArrayIndexOutOfBoundsException.

Do This: Direct students to Level 4 on Code Studio to complete Levels 4 and 5. Students complete a choice level on Level 4 to assign and change individual elements in a 1D array. On Level 5, students print the contents of the 1D array using a while loop.

Wrap Up (10 minutes)

Debugging Wall

Remarks

We saw some new types of errors today! Let's discuss these errors we encountered and update our Debugging Wall with the strategies we used to debug them.

Discuss: Click through the animated slide to display the prompts. Use the Hold That Thought strategy to discuss the prompts.

  • What errors did you encounter while writing your code?
  • How did you find and fix the error?

Discussion Goal: Students consider errors they encountered in previous lessons and this lesson and share scenarios. Students suggest using their new method to find and fix logic errors they have encountered.

Do This: Have students choose a strategy as a class and add it to the Debugging Wall.

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 6.1

Note: Some Learning Objectives and Essential Knowledge statements in the suggested Topic Questions are covered in later units.

Creative Commons License (CC BY-NC-SA 4.0).

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.