Using One-Dimensional Arrays to Describe Collections of Numbers - dummies

Using One-Dimensional Arrays to Describe Collections of Numbers

By John Pezzullo

It’s often convenient, when dealing with collections of numbers, to use a single variable name to refer to the entire set of numbers. A bunch of values referred to by a single variable name is generally called an array. Arrays can have one or more dimensions, which you can think of as rows, columns, and slices.

A one-dimensional array can be thought of as a simple list of values. For instance, you might record the fasting glucose values (in milligrams per deciliter, mg/dL) of five subjects as 86, 110, 95, 125, and 64, and use the variable name “Gluc” to refer to this collection of five numbers.

Gluc is an array of numbers, and each of the five individual glucose values in the collection is an element of the Gluc array. The variable name Gluc in a formula refers to the whole collection of numbers (five numbers, in this example).

You can refer to one particular element (that is, to the glucose value of one particular subject) of this array several ways. The number that indicates which element of the array you’re referring to is called the index of the array.

  • In a typeset formula, indexing is usually indicated by subscripts. For example, Gluc3 refers to the third number in the collection (in this example, 95).

  • In a plain text formula, indexing is usually indicated by brackets, for example, Gluc[3], although older software might use parentheses for this purpose, like Gluc(3).

The index can be a variable. In that case, Gluc[i] would refer to the ith element of the array. The variable i would, take on some value between 1 and the number of elements in the array (in this example, between 1 and 5).

In some programming languages, the indices start at 0 for the first element, 1 for the second element, and so on, but that can be confusing. In this book, all arrays are indexed starting at 1. But be aware that other books and articles may use an index-0 scheme.