Standard Operations on Arrays and Collections

Standard Operations on Arrays and Collections

Most useful programs store many values together and process them as a group. The two main structures in the IB course are arrays, which have a fixed size and are accessed by index, and collections, which are managed with a set of built-in methods. Knowing the standard operations on both is essential.

Arrays and traversal

An array is an indexed sequence of elements, all normally of the same type, stored under one name. An element is accessed by its index, such as SCORES[3], with indices usually starting at 0. Traversal means visiting every element in turn, and it is the foundation of almost every array operation. A count-controlled loop from 0 to the last index performs a traversal:

loop I from 0 to SIZE - 1
    output SCORES[I]
end loop

Summing and averaging

To sum an array, initialize an