• Foundation
  • About 12 min
  • 5 practice questions

Arrays and strings: storing many values under one name

Use arrays to store a list of values and strings to store text, read items by index and add up or find the largest of a list.

If something looks wrong, please tell us.

So far each variable has held a single value. But a teacher has the marks of many students, and a program often needs a whole list at once. Declaring a separate variable for each mark would be impractical. An array solves the problem by storing many values of the same kind under one name.

Arrays

An array is like a row of numbered boxes. Each box is an element, and its number is its index (or subscript). In most languages the first element has index 0, so an array with 5 elements has indexes 0, 1, 2, 3 and 4. The number of elements is the array's length. If marks = [60, 75, 80, 45, 90], then marks[0] is 60 and marks[3] is 45. The last valid index is always length - 1, and asking for an index outside the range is an error.

Going through an array

A loop can visit every element in turn. The usual pattern is: set total = 0; for each index from 0 to length - 1, add marks[index] to total. To find the average, divide the total by the length.

Strings

A string is a sequence of characters such as letters, digits and spaces, written inside quotation marks. Strings behave like arrays of characters, so each character has an index starting at 0. Useful ideas: the length (number of characters, including spaces); concatenation (joining strings, often with +); and picking out a character by index.

Strings and arrays
IdeaExampleResult
Length of a string"COMPUTER"8
Character at index 0"COMPUTER"C
Character at index 7"COMPUTER"R
Concatenation"Good" + " " + "Day"Good Day
Array elementmarks = [60, 75, 80, 45, 90], marks[2]80

Worked example 1: total and average

For marks = [60, 75, 80, 45, 90], total = 0. Adding in turn: 60, then 135, then 215, then 260, then 350. The total is 350. The array has 5 elements, so the average = 350 / 5 = 70.

Worked example 2: the largest value

Set largest = marks[0] = 60. Compare each later element: 75 is greater, so largest = 75; 80 is greater, so largest = 80; 45 is not; 90 is greater, so largest = 90. The largest mark is 90, found at index 4. Notice that we start from the first element, not from 0, in case all marks are negative or very small.

Worked example 3: indexes in a string

The string is "SCHOOL". Its length is 6, because it has 6 characters. The characters by index are S (0), C (1), H (2), O (3), O (4) and L (5). The last index is 6 - 1 = 5, which holds L. The character at index 3 is O.

Two common mistakes are worth remembering. The first is asking for marks[5] in an array of length 5; the last valid index is 4, so this is out of range. The second is starting a sum without setting total to 0 first, so that a leftover value from earlier corrupts the answer. Checking these two points catches most errors in array questions.

The first index is 0 and the last is length - 1; total first, then divide by length for the average.

Key takeaways

  • An array stores many values of one kind under one name.
  • Index counting starts at 0 in most languages.
  • A string is a sequence of characters with its own length and indexes.

Check your understanding

  1. 1. In an array with 6 elements, what is the index of the last element?

    1. 5
    2. 7
    3. 4
    4. 6
    Show answer

    Answer: A. Indexes run from 0 to 5, so the last index is length - 1 = 5.

  2. 2. If marks = [12, 20, 8, 15], what is marks[1]?

    1. 12
    2. 20
    3. 8
    4. 15
    Show answer

    Answer: B. Index 1 is the second element, which is 20.

  3. 3. What is the length of the string "EXAM DAY" (including the space)?

    1. 7
    2. 9
    3. 8
    4. 6
    Show answer

    Answer: C. The characters are E, X, A, M, a space, D, A, Y: 8 in all.

  4. 4. The average of the array [10, 20, 30, 40] is

    1. 20
    2. 30
    3. 100
    4. 25
    Show answer

    Answer: D. Total = 100, divided by 4 elements gives 25.

  5. 5. Which of these joins two strings together?

    1. Subscripting
    2. Concatenation
    3. Looping
    4. Compiling
    Show answer

    Answer: B. Concatenation joins strings end to end, for example "Good" + "Day".

Privacy · Terms · Refunds · Delivery