Lesson goal: An Introduction to Arrays

Previous: $\sqrt{n}-\sqrt{n-1}<0.01$ | Home | Next: Loops and arrays

In a past lesson, you learned that a variable is a place that can store or hold a single quantity of information. An array is also a variable, but is different in that it can hold more than one value, all under the same variable name.

With normal variables, you can hold just one value as in score=10, in which case the variable score hold the single value of 10. With an array, you can hold many values, as you'll learn in this lesson.

First, we have to be able to tell the computer when we want a variable to be an array, and what values to hold in it. This is done like this:

variable={val1,val2,val3,val4,val5,...}
Move the mouse over a dotted box for more information.

Now you try. Fix the print statement, so that it will display the value of the 1st element in 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 numbers. We'd like to be able to display any element of the array that we wish, 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 1st element of our array, called a? If you figure that out, how about the 2nd element? 3rd? 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.