10 pages
5.1 Array concept (general)
A typical variable stores one data item, like the number 59 or the character ‘a’. Instead, sometimes a list of data items should be stored. Ex: A program recording points scored in each quarter of a basketball game needs a list of 4 numbers. Requiring a programmer to declare 4 variables is annoying; 200 variables would be ridiculous. An array is a special variable having one name, but storing a list of data items, with each item being directly accessible. Some languages use a construct similar to an array called a vector. Each item in an array is known as an element.
- A variable usually stores just one data item.
- Some variables should store a list of data items, like variable pointsPerQuarter that stores 4 items.
- Each element is accessible, like the element numbered 3.
You might think of a normal variable as a truck, and an array variable as a train. A truck has just one car for carrying “data”, but a train has many cars, each of which can carry data.
Figure 5.1.1: A normal variable is like a truck, whereas an array variable is like a train.

In an array, each element’s location number is called the index; myArray[2] has index 2. An array’s key feature is that the index enables direct access to any element, as in myArray[2]; different languages may use different syntax, like myArray(3) or myVector.at(3). In many languages, indices start with 0 rather than 1, so an array with 4 elements has indices 0, 1, 2, and 3.
5.2 Arrays
Array declarations and accessing elements
A programmer commonly needs to maintain a list of items, just as people maintain a list of items like a grocery list or a course roster. An array variable is an ordered list of items of a given data type and size. Each item in an array is called an element. For array x, each element is accessed as x[0], x[1], … In an array access, the number in brackets is called the index of that element. In Coral, the first array element is at index 0.
Some relevant terminology
- [ ] are brackets
- { } are braces
- ( ) are parentheses
To contrast with array variables, a single-item (non-array) variable is called a scalar variable.
The program below declares an integer array named itemCounts, having 3 elements. The program assigns the first element itemCounts[0] with an input value, the next element with 99, and the third element with itemCounts[0] * 2.
Note below that in Coral, an array stores a size attribute, indicating the number of array elements.
Arrays and loops
A key advantage of arrays becomes evident when used with loops. The program below uses a loop to allow a user to enter 5 integer values, storing those values in array userVals, and then outputting those 5 values.
The loop makes use of the array’s size attribute, accessed via userVals.size, which in this case has value 5.
5.3 Array iteration drill
The following activities can help one become comfortable with iterating through arrays or vectors, before learning to code such iteration.
5.4 Iterating through arrays
Iterating through an array using loops
Iterating through arrays using loops is common and is an important programming skill to master.
An array’s indices are numbered 0 to size-1, not 1 to size. Thus, when iterating through an array, programmers use the standard loop form shown below, starting with i = 0, and iterating while i < size.
Such a loop will iterate with i = 0, 1, …, size – 1, thus matching the array’s indices. If size is 5, the loop iterates with i = 0, 1, 2, 3, 4, as desired.
In Coral, given an array named userVals, the array’s size is accessible as userVals.size.
Iterating through arrays example: Max
Programs commonly iterate through arrays to determine some quantity about the array’s items. The program below determines the maximum value in a user-entered list. If the user enters numbers 7 -9 55 44, then the program will output “Max: 55”. The program uses the variable maxVal to store the largest value seen “so far” as the program iterates through the array. During each iteration, if the array’s current element value is larger than the max seen so far, the program assigns maxVal with the array element.
Before entering the loop, maxVal must be initialized to some value because max will be compared with each array element’s value. A logical error would be to initialize maxVal to 0, because 0 is not in fact the largest value seen so far, and would result in incorrect output (of 0) if the user entered all negative numbers. Instead, the program peeks at an array element (using the first element, though any element could have been used) and initializes maxVal to that element’s value.
5.5 Swapping two variables (general)
Sometimes a program must swap values among two variables. Swapping two variables x and y means to assign y’s value with x, and x’s value with y. If x is 33 and y is 55, then after swapping x is 55 and y is 33.
A common method for swapping uses a temporary third variable. To understand the intuition of such temporary storage, consider a person holding a book in one hand and a phone in the other, wishing to swap the items. The person can temporarily place the phone on a table, move the book to the other hand, then pick up the phone.
5.6 Code: Arrays
Array declarations
In code, a variable that is an array has a special keyword “array” added, with the size indicated too. The code below declares userVals to be an array with 4 elements.
integer array(4) userVals
The elements are then accessed as userVals[0], userVals[1], userVals[2], and userVals[3]. The array’s size can also be accessed as userVals.size.
integer array(5) oldestPeople
integer nthPerson
integer personAge
// Died 1997 in France
oldestPeople[0] = 122
// Died 1999 in U.S.
oldestPeople[1] = 119
// Died 1993 in U.S.
oldestPeople[2] = 117
// Died 1998 in Canada
oldestPeople[3] = 117
// Died 2006 in Ecuador
oldestPeople[4] = 116
nthPerson = Get next input
if ((nthPerson >= 1) and (nthPerson <= 5))
personAge = oldestPeople[nthPerson – 1]
Put nthPerson to output
Put “th oldest person died at age ” to output
Put personAge to output
Iterating through arrays example: Max
Programs commonly iterate through arrays to determine some quantity about the array’s items. The program below determines the maximum value in a user-entered list. If the user enters numbers 7 -9 55 44, then the program will output “Max: 55”. The program uses the variable maxVal to store the largest value seen “so far” as the program iterates through the array. During each iteration, if the array’s current element value is larger than the max seen so far, the program assigns maxVal with the array element.
Before entering the loop, maxVal must be initialized to some value because max will be compared with each array element’s value. A logical error would be to initialize maxVal to 0, because 0 is not in fact the largest value seen so far, and would result in incorrect output (of 0) if the user entered all negative numbers. Instead, the program peeks at an array element (using the first element, though any element could have been used) and initializes maxVal to that element’s value.
integer array(4) userVals
integer i
integer maxVal
for i = 0; i < userVals.size; i = i + 1
userVals[i] = Get next input
// Largest so far
maxVal = userVals[0]
for i = 0; i < userVals.size; i = i + 1 if userVals[i] > maxVal
maxVal = userVals[i]
Put “Max: ” to output
Put maxVal to output
Because the cunts didn’t explain any of this shit:
integer userInts
integer i
integer mid
integer array(9) n
userInts = Get next input
while (userInts >=0) and (i < n.size)
n[i] = userInts
userInts = Get next input
i = i + 1
if userInts >=0
Put “Too many inputs” to output
else
mid = i / 2
Put n[mid] to output