Lesson 13: Introduction to Arrays

Overview

This lesson introduces arrays as a means of storing lists of information within a program. The class begins by highlighting the difficulties that arise when trying to store lists of information in a variable. Students then watch a short video introducing arrays and a subset of the operations that can be performed with them. Students will work in Code Studio for the remainder of the class as they practice using arrays in their programs. At the conclusion of the sequence, students build a simple app which can be used to store and cycle through a list of their favorite things. In the next lesson, students will continue working with a version of this app that can display images and not just text strings.

Purpose

Some sort of list data structure is a component of almost all programming languages. A list allows large amounts of information to be easily referenced and passed around a program, and the use of a numeric index allows individual items in a list to be accessed. Historically a list would have literally been a single contiguous chunk of memory and the index or address was used to know how far into that chunk a relevant piece of information was stored. In many modern languages, however, it is more likely that the items in an array are stored at many locations on your computer’s hard drive, and the index is only useful to help the programmer identify different components. In this way, a JavaScript array is actually another example of abstraction. We know that it is holding a list of related information, but we don’t need to think about the actual implementation details.

Agenda

View on Code Studio

Objectives

Students will be able to:

  • Identify an array as a data structure used to store lists of information in programs.
  • Create arrays and access information stored within them using an index.
  • Manipulate an array using the append, insert, and remove operations.
  • Account for the fact that JavaScript arrays are zero-indexed when using them in a program.

Vocabulary

  • Array - A data structure in JavaScript used to represent a list.
  • List - A generic term for a programming data structure that holds multiple items.

Introduced Code

Teaching Guide

Getting Started (10 Minutes)

Prompt: What makes lists useful in everyday life?

Goal

Demonstrate that lists are a desirable feature of a programming language and that using variables to store lists is cumbersome or impossible. Motivate the need for arrays.

Thinking Prompt:

  • "Today we’re going to start looking at how we can use lists in programs, but before we dive into that, let’s think about why we would want to in the first place. What are the benefits of creating lists? Why is it helpful to keep information in lists?"

Discuss:

Students may discuss in small groups or you can just ask for ideas from the class. Potential answers include:

  • Lists help us organize information.
  • Lists help us collect all the relevant information in one place.
  • Lists show that a lot of ideas are related.
  • Lists help us order or prioritize ideas.
  • Lists help us think about the big picture.

Transition: Variables are a bad way to store lists.

Transitional Remarks

There are a lot of benefits to keeping lists of information in real life. Since we use programming to solve a lot of similar problems, we would like to keep lists of information in our programs, too.

Right now, the only way we know how to store information in our programs is with a variable, but each variable can only store a single piece of information.

Today we’ll be learning about a new programming construct that will allow us to hold as many pieces of information as we want within a single list.

Activity (80 Minutes)

App Lab: Introduction to Arrays

This set of levels looks long, but all the problems are relatively short and small.

You might consider watching the first video as a whole class before diving in.

Code Studio levels

  • Introduction to Lists - Part 1
  • (click tabs to see student view)
  • List Basics
  • (click tabs to see student view)
  • Introduction to Lists - Part 2
  • (click tabs to see student view)
  • List Basics
  • (click tabs to see student view)
  • Introduction to Lists - Part 3
  • (click tabs to see student view)
  • Introduction to Lists - Part 4
  • (click tabs to see student view)
  • Length and Indexes
  • (click tabs to see student view)
  • "My Favorite Things" Demo App
  • (click tabs to see student view)
  • Array Reflection Questions
  • (click tabs to see student view)

Wrap-up (10 Minutes)

Reflection: When to use a variable and when to use an array

Goal: Students now know how to store information in both variables and arrays. Help students synthesize their new knowledge by trying to develop a rule for when to use a variable vs. an array. This is also just a useful way to assess students’ understanding of arrays at the conclusion of the lesson.

Thinking Prompt (Also in Code Studio)

  1. Your app needs to store the following information. Decide whether you would use an array or a variable to store it?

    1. All the messages a user has sent
    2. The highest score a user has ever reached on the app
    3. A username and password to unlock the app
  2. In general, when do you think you should you store information in an array, and when should you use a variable?

Discuss: You can use these questions as an exit ticket, but it is probably even more useful to discuss as a class. Use this discussion to possibly identify and address any misconceptions about what an array is and how it stores information. Here are some key points to pull out:

  • Variables store single pieces of information, while arrays store many.
  • An array can grow in size to accommodate more information.
  • Arrays are slightly more complex to use than variables. If you are only going to be storing a small and fixed amount of information, it is probably appropriate to use multiple variables.

Conclusion:

Remarks

We are going to keep exploring arrays in the coming lessons. Keep your eye out for some of the distinctions we just discussed, and keep thinking about how you might want to use arrays in applications of your own.

Extended Learning

One of the last levels in App Lab challenges students to keep developing the app. If you wish, you may also have students submit these projects.

Standards Alignment

View full course alignment

Computer Science Principles

1.1 - Creative development can be an essential process for creating computational artifacts.
1.1.1 - Apply a creative development process when creating computational artifacts. [P2]
  • 1.1.1B - Creating computational artifacts employs an iterative and often exploratory process to translate ideas into tangible form.
5.1 - Programs can be developed for creative expression, to satisfy personal curiosity, to create new knowledge, or to solve problems (to help people, organizations, or society).
5.1.2 - Develop a correct program to solve problems. [P2]
  • 5.1.2A - An iterative process of program development helps in developing a correct program to solve problems.
5.3 - Programming is facilitated by appropriate abstractions.
5.3.1 - Use abstraction to manage complexity in programs. [P3]
  • 5.3.1A - Procedures are reusable programming abstractions.
  • 5.3.1B - A function is a named grouping of programming instructions.
  • 5.3.1C - Procedures reduce the complexity of writing and maintaining programs.
  • 5.3.1K - Lists and list operations, such as add, remove, and search, are common in many programs.
  • 5.3.1L - Using lists and procedures as abstractions in programming can result in programs that are easier to develop and maintain.
5.5 - Programming uses mathematical and logical concepts.
5.5.1 - Employ appropriate mathematical and logical concepts in programming. [P1]
  • 5.5.1H - Computational methods may use lists and collections to solve problems.
  • 5.5.1I - Lists and other collections can be treated as abstract data types (ADTs) in developing programs.
  • 5.5.1J - Basic operations on collections include adding elements, removing elements, iterating over all elements, and determining whether an element is in a collection.

CSTA K-12 Computer Science Standards (2017)

AP - Algorithms & Programming
  • 3A-AP-14 - Use lists to simplify solutions, generalizing computational problems instead of repeated use of simple variables.
  • 3A-AP-16 - Design and iteratively develop computational artifacts for practical intent, personal expression, or to address a societal issue by using events to initiate instructions.
  • 3B-AP-12 - Compare and contrast fundamental data structures and their uses.