4. Computational Thinking, Problem-Solving and Programming

Arrays

Arrays

Welcome, students! 👋 In this lesson, you will learn about arrays, one of the most important tools in programming and computational thinking. Arrays help programmers store and process many related values efficiently, such as test scores, temperatures, or the seats in a cinema. By the end of this lesson, you should be able to explain what arrays are, why they matter, and how they support problem-solving in IB Computer Science HL.

Lesson objectives

  • Understand the main ideas and terminology behind arrays.
  • Use arrays in problem-solving and algorithm design.
  • Connect arrays to abstraction, decomposition, and data processing.
  • Evaluate when arrays are a useful choice in a solution.

Arrays appear in many real-world systems, from school grade records to sensor data in a smart home. 🌍 They are a key example of how computer scientists organize data so that it can be accessed and processed quickly.

What is an array?

An array is a data structure that stores a fixed number of values of the same type in a single variable-like structure. Each value in the array is called an element. The position of an element is called its index.

For example, suppose a teacher stores the marks of five students:

$$\text{marks} = [78, 85, 91, 67, 88]$$

Here, each number is an element of the array. In many programming languages, the first element has index $0$, so:

  • $\text{marks}[0] = 78$
  • $\text{marks}[1] = 85$
  • $\text{marks}[2] = 91$
  • $\text{marks}[3] = 67$
  • $\text{marks}[4] = 88$

This indexing system is important in IB Computer Science because it affects how algorithms access data. students, notice that arrays are useful when you need to treat a group of related items as one unit while still being able to reach each item individually.

An array supports random access, which means any element can be accessed directly if its index is known. This is faster than searching through separate variables one by one.

Why arrays are useful in computational thinking

Arrays support abstraction because they let programmers think about a collection of items as a single structure instead of many separate variables. For example, instead of creating five separate variables such as $\text{score1}$, $\text{score2}$, $\text{score3}$, $\text{score4}$, and $\text{score5}$, a programmer can use one array:

$$\text{scores} = [\,\,]$$

This makes code easier to read, maintain, and expand. If the class later grows from five students to thirty, an array can still manage the data without rewriting the whole program.

Arrays also support decomposition. Many problems can be broken into smaller parts, and arrays help by storing repeated data in a structured way. For example, in a weather app, temperatures for each day can be stored in an array. Then the program can separately calculate the average, find the highest temperature, or display the results.

Arrays are also essential in algorithmic thinking. Algorithms often need to loop through a list of values, compare elements, or calculate totals. Arrays make these tasks efficient because the same procedure can be repeated for every element.

Key terminology you need to know

Here are the main terms connected to arrays:

  • Array: a structured collection of values of the same data type.
  • Element: one item stored in the array.
  • Index: the position of an element in the array.
  • Length: the number of elements in the array.
  • Traversal: visiting each element in the array, usually with a loop.
  • Search: finding a specific value in an array.
  • Update: changing an element in an array.
  • Subscript: another word sometimes used for index notation, such as $\text{numbers}[2]$.

If an array has length $n$, then valid indexes usually range from $0$ to $n-1$. For example, if $\text{names}$ has $4$ elements, the valid indexes are $0$, $1$, $2$, and $3$. Attempting to access $\text{names}[4]$ would go beyond the last element and cause an error in many languages.

Understanding these terms is important because IB Computer Science asks students to explain how data is organized and how algorithms operate on it.

Working with arrays in programming

A programmer often creates an array to store values that belong together. For example, a sports app could store lap times:

$$\text{laps} = [62.4, 60.8, 61.5, 59.9]$$

To process this array, a loop is commonly used. Traversal means visiting each element in order. The idea can be shown in pseudocode:

$$\text{total} \leftarrow 0$$

$$\text{FOR each } x \text{ in } \text{laps}$$

$$\quad \text{total} \leftarrow \text{total} + x$$

$$\text{END FOR}$$

This algorithm adds all the lap times together. Then the average lap time can be found by:

$$\text{average} = \frac{\text{total}}{\text{length of laps}}$$

If the values are $62.4$, $60.8$, $61.5$, and $59.9$, then:

$$\text{total} = 62.4 + 60.8 + 61.5 + 59.9 = 244.6$$

$$\text{average} = \frac{244.6}{4} = 61.15$$

This is a good example of programming and data processing. The array stores the data, and the algorithm processes it.

Arrays are also useful for finding the largest or smallest value. Suppose a student wants to know the highest score in:

$$\text{scores} = [72, 89, 65, 94, 81]$$

A program can begin by assuming the first value is the largest:

$$\text{largest} \leftarrow \text{scores}[0]$$

Then it compares each remaining element to $\text{largest}$. If a larger value is found, the variable is updated. This method is efficient and widely used in computer science.

Arrays, search, and indexing examples

Arrays make searching easier because the program can inspect elements in a controlled way. There are two common ideas to understand here: direct access and sequential search.

With direct access, if you know the index, you can reach the element immediately. For example, in a timetable array:

$$\text{lessons} = [\text{Math}, \text{English}, \text{Biology}, \text{Art}]$$

Then $\text{lessons}[2] = \text{Biology}$.

With sequential search, the program checks each element one by one until it finds the target. For example, if students wants to know whether $\text{Biology}$ is in the array, the program can compare the target to each element in order.

This is a useful distinction in IB Computer Science HL because it connects arrays to algorithm efficiency. Access by index is fast, while searching may require checking many elements. If an array has $n$ elements, a simple traversal may need up to $n$ comparisons in the worst case.

Arrays also help when processing repeated data from real systems. For example, a traffic sensor may record vehicle counts every hour:

$$\text{counts} = [12, 18, 25, 31, 28, 20]$$

A program could use this array to identify peak traffic times, calculate totals, or plot a graph. This is a clear example of how arrays support data processing in the real world. 🚗

Arrays in solution design and evaluation

When designing a solution, a programmer must decide whether an array is the right data structure. Arrays are a strong choice when the data:

  • contains similar items,
  • needs to be processed in order,
  • may be used in loops,
  • is best handled as a collection.

For example, a school attendance system might store daily attendance for a class in an array. This makes it easy to count present students, identify absences, or calculate attendance percentages.

However, arrays also have limits. In many languages, the size of an array is fixed after creation. That means if the collection needs to grow or shrink often, another structure may be more suitable, such as a dynamic list in some languages. IB Computer Science expects students to evaluate structures based on the problem requirements.

When evaluating an array-based solution, consider:

  • Is the data type the same for all elements?
  • Will the program frequently access specific positions?
  • Will the data be processed with loops?
  • Is the number of elements known in advance?
  • Is the structure simple enough for the problem?

These questions help show strong computational thinking. students, being able to justify the use of arrays is part of good solution design.

Arrays and the IB Computer Science HL perspective

In IB Computer Science HL, arrays are not just a programming feature. They are a way to show understanding of problem-solving, efficiency, and structured data processing. Arrays can appear in examination questions as part of pseudocode, algorithms, or explanations of data representation.

For example, you may be asked to trace a loop that processes an array, predict the output of an algorithm, or explain why an array is suitable for a task. You may also need to describe how arrays help with abstraction by reducing the number of individual variables.

A strong answer should include accurate terminology and clear reasoning. For instance, instead of saying "the computer looks at the list," a better explanation is "the program traverses the array using a loop and processes each element in turn." This shows understanding of the concepts expected at HL.

Arrays also connect to broader themes in the course, such as data handling, algorithm design, and solution evaluation. They are a foundation for more advanced ideas like matrices, records, and complex data structures.

Conclusion

Arrays are a powerful and widely used way to store and process related data. They help programmers organize information, access elements by index, and apply algorithms efficiently. In computational thinking, arrays support abstraction by grouping values, decomposition by managing repeated data, and algorithmic thinking by enabling traversal and search.

For IB Computer Science HL, it is important to know the terminology, understand how arrays work in code, and explain why they are useful in real-world solutions. students, if you can describe an array clearly and use it in an algorithm, you are building a strong foundation for more advanced programming topics. ✅

Study Notes

  • An array stores multiple values of the same type in one structure.
  • Each value is an element, and its position is an index.
  • Many languages use indexing starting at $0$.
  • Arrays support random access, so an element can be reached directly by index.
  • Traversal means visiting each element, usually with a loop.
  • Arrays are useful for totals, averages, maximum and minimum values, and searching.
  • Arrays support abstraction by reducing many separate variables into one collection.
  • Arrays support decomposition by organizing repeated data into manageable parts.
  • A common algorithm pattern is to initialize a variable, traverse the array, and update the variable as needed.
  • Arrays are best when the number of items is known or does not change often.
  • In IB Computer Science HL, you should explain why an array is suitable for a problem and how it helps with efficient processing.
  • Arrays are a key part of computational thinking, problem-solving, and programming.

Practice Quiz

5 questions to test your understanding