Array Examples in CS Thinking

Start with the recap, study the fully worked examples, then use the practice problems to check your understanding of Array.

This page combines explanation, solved examples, and follow-up practice so you can move from recognition to confident problem-solving in CS Thinking.

Concept Recap

An ordered collection of values stored together and accessed by their numeric index position.

A numbered list. Item 0, item 1, item 2... Access any by its number.

Read the full concept explanation โ†’

How to Use These Examples

  • Read the first worked example with the solution open so the structure is clear.
  • Try the practice problems before revealing each solution.
  • Use the related concepts and background knowledge badges if you feel stuck.

What to Focus On

Core idea: Arrays store multiple related values under one name, making it easy to iterate over them all.

Common stuck point: Most languages index from 0, not 1. The last element of a 3-item array is at index 2.

Worked Examples

Example 1

easy
An array `scores` contains [85, 92, 78, 95, 88]. What is scores[0]? What is scores[3]? How many elements are there?

Solution

  1. 1
    Step 1: Arrays use zero-based indexing: scores[0] = 85.
  2. 2
    Step 2: scores[3] = 95 (the 4th element).
  3. 3
    Step 3: The array has 5 elements (length = 5).

Answer

scores[0] = 85, scores[3] = 95, length = 5.
Arrays store multiple values in a single variable, accessed by index. Most programming languages use zero-based indexing, so the first element is at index 0.

Example 2

medium
Write pseudocode to find the sum of all elements in an array `nums = [4, 7, 2, 9]`.

Practice Problems

Try these problems on your own first, then open the solution to compare your method.

Example 1

medium
Given array `names = ['Alice', 'Bob', 'Charlie']`, what is names[1]? What happens if you access names[3]?

Example 2

medium
Given `scores = [12, 15, 9, 20]`, what are `scores[0]` and `scores[3]`, and what happens if code tries to read `scores[4]`?

Background Knowledge

These ideas may be useful before you work through the harder examples.

variabledata types