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 under a single name and accessed by their numeric index position. Arrays allow you to store, retrieve, and manipulate multiple related values efficiently using loops and index-based access.

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.

Sense of Study hint: When working with arrays, remember that indices start at 0 in most languages, so the last element of an array with n items is at index n-1. Use loops to process every element, and always check that your index is within bounds before accessing an element.

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