# Encyclopædia

Let's solve Encyclopædia.

## Storing lists of data

In this problem, we are given a list of word counts on the pages of an encyclopædia. Our program must answer multiple questions of the form: "How many pages are there on page x?"

So far, whenever we've encountered problems involving large sets of data, I've tried to describe solutions that only 'remember' one or two variables at a time. With this problem such an approach is no longer possible.

You may be familiar with arrays - these are data structures that let you store large quantities of data. They behave differently in different languages, and you should check how they work in yours. In this walkthrough I'll be using C arrays as an example.

In C, an array is declared much like a normal variable. Say we needed room for 8 int-type variables. Then we would write:

int a;

The eight variables we now have access to are a, a, ..., a, a. (Note that the numbering system is zero-based: counting begins at zero, not one. This may seem unintuitive, but turns out to be quite useful when we try manipulating arrays in advanced ways.)

The elements of an array can be manipulated like normal variables. For example, we could write:

<Read values into a and a>
a = a + a;
<Print out the value of a>

(As before, substitute the comments in angle brackets with code to correctly handle file input and/or output.)

The above would be a correct, albeit strange, solution to Addition (Starter Problems 1).

However, the real power of arrays lies in our ability to access their contents using mathematical expressions. If the variable foo is set to 5, then a[foo] refers to a and we can manipulate a[foo] and store values inside it like any other variable. If the variable i counts from 0 to N-1 over the course of a loop, then in each iteration a[i] will refer to a different element of the array.

This is best seen with an example. The following code reads in a list of nPages integers into an array named wordcounts.

int nWords;
for (int i = 0; i < nPages; i++) {
}
int x;
<Write the value of wordcounts[x-1]>