Lesson 3: Column-Major Traversal
45 minutes
Overview
How do I access elements in a 2D array by column?
Students learn to use nested loops to traverse a 2D array in column-major order and differentiate between column-major and row-major traversals. Students identify scenarios that are best for traversing in row-major and column-major order and develop an algorithm to traverse a 2D array in column-major order to find the sum of all values in each column of the array. Students then practice applying standard 1D array algorithms to 2D arrays to find information and perform calculations.
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.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
CON-2.N - For Algorithms in the context of a particular specification that requires the use of 2D array traversals: • Identify standard algorithms. • Modify standard algorithms. • Develop an algorithm.
- CON-2.N.2 - All standard 1D array algorithms can be applied to 2D array objects.
VAR-2 - To manage large amounts of data or complex relationships in data, programmers write code that groups the data together into a single data structure wihtout creating individual variables for each value.
VAR-2.F - Represent collections of related primitive or object reference data using two-dimensional (2D) array objects.
- VAR-2.F.5 - “Row-major order” refers to an ordering of 2D array elements where traversal occurs across each row, while “column-major order” traversal occurs down each column
VAR-2.G - For 2D array objects: • Traverse using nested for loops. • Traverse using nested enhanced for loops.
- VAR-2.G.2 - Nested iteration statements can be written to traverse the 2D array in “row-major order” or “column-major order.”
Agenda
Objectives
Students will be able to:
- Differentiate between row-major and column-major order traversal
- Write nested loops to traverse a two-dimensional (2D) array in column-major order
Preparation
- Print copies of the Tracing Column-Major Traversals 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
- Column-Major Traversal - Video
- Tracing Column-Major Traversals - Handout
- U5L3 Extra Practice - Handout
Vocabulary
- column-major order - traversing a 2D array by accessing each column from left to right
Teaching Guide
Warm Up (5 minutes)
Working with Data
Remarks
In the previous units, we worked with data to find information for a user and to create abstract data art.
Discuss: Use the Retrieve-Pair-Share strategy to discuss the prompt.
- How would 2D arrays have made it easier to store and process that data?
Discussion Goal: Students share examples of datasets they used in the previous units and how 2D arrays would have allowed them to store related values of the same type in a table.
Students may have worked with data of different types in their unit projects, so a 2D array would not have been beneficial in those scenarios. Ask students to consider how this might have changed if their values were of the same data type.
Activity (30 minutes)
Column-Major Traversal (15 minutes)
Remarks
In the previous lesson, we learned how to traverse a 2D array in row-major order. We can also traverse a 2D array in column-major order.
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 the outer loop traverses each column, and the inner loop traverses each row. Students also notice that the syntax for accessing an individual element is still the same. Students may wonder when they would traverse a 2D array in column-major order.
Display: Show the video – Column-Major Traversal.
Do This: Click through the animated slide to demonstrate a column-major traversal.
Group: Place students in pairs.
Distribute: Give each pair a copy of the Tracing Column-Major Traversals handout.
Do This: Have students complete the Tracing Column-Major Traversals handout.
You can choose to model completing the first few rows of the trace table as a class first, then have students work with their partners to complete the remaining rows.
Traversing 2D Arrays (15 minutes)
Remarks
We can traverse a 2D array in row-major order or column-major order depending on how we want to access the information. Both approaches have several similarities and differences.
Discuss: Use the Hold That Thought strategy to discuss the prompt.
- What are the differences between the row-major and column-major order traversal algorithms?
Discussion Goal: Students note that the for
loop headers are swapped. In row-major order, the loop variable in the outer for
loop is used as a row index, and the loop variable from the inner for
loop is used as a column index. Conversely, in column-major order, the loop variable in the outer for
loop is used as a column index, and the loop variable from the inner for
loop is used as a row index.
Do This: Explain use cases for each traversal approach.
Remarks
In the previous lesson, we modified a standard 1D array algorithm to find the sum of all values in a 2D array. Let's modify this algorithm to find the sum of each column in a 2D array.
Do This: Have students write pseudocode to find the sum of each column in a 2D array.
Do This: Direct students to Level 2 on Code Studio to complete Levels 2, 3, and 4. Students debug the program on Level 2, then continue to Level 3 to implement their algorithm. Students complete a choice level on Level 4 to apply a standard 1D array algorithm to a 2D array using the console or The Theater.
Wrap Up (10 minutes)
Closing the Loop
Remarks
We have learned a lot about 2D arrays and applying standard 1D array algorithms to 2D arrays. Let's recap some of the key concepts we have learned.
Discuss: Click through the animated slide to display the prompts.
- What are some similarities between creating and traversing 1D arrays and 2D arrays? What are some differences?
- What are some scenarios you have encountered before where 2D arrays would have been useful?
- What were you confident about in this lesson? What would you like to practice?
Discussion Goal: Students identify similarities and differences in the syntax for creating and traversing arrays. Students suggest examples of scenarios where 2D arrays would have been useful for storing and organizing data. Students share concepts they are confident about and that they need more practice.
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.
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.