Lesson goal: An Introduction to Arrays

Previous: Introduction | Home | Next: Arrays and averaging numbers

In a past lesson, you learned how to delcare an array, load it with some values, then display the value held at a given array position.

Since arrays have sequential elements, they are commonly "indexed" using loops. In this lesson, you'll learn how to use a for-loop to print the value of each element in an array.

Now you try. Fix the print statement, so that it will display the value of the $i^{th}$ element of the array.

Type your code here:

See your results here:

This code will not run! In this example, we declare variable a to be an array, and fill it with a few odd numbers. Then we start a for-loop to go from 1 to #a. #a stands for "the number of elements in array a."

The aim of this code is to display the elements in array a, but the print statement needs some fixing. Given that the $i^{th}$ element of an array named a is access via the construct a[i] , what will you put into the print statement to have it display the $i^{th}$ element of our array, called a? Dismiss.

Show a friend, family member, or teacher what you've done!

Here is a share link to your code:

Does your code work? Want to run it on your iPhone?

Here's your code:

  1. Use [Control]-[C] (Windows) or [⌘]-[C] (MacOS) to copy your code.

  2. Paste it using [Control]-[V] (Windows) or [⌘]-[V] (MacOS) into this page

  3. Then click the "Use on iPhone" button that you'll see.