Website by Joshua Bleier

Module 4.1 - Array Basics

This module refers to files and folders found in the tar file module4.tar.

So far, when we've dealt with variables, they have contained a single value. In the following variable declaration:

int temp;

the variable temp is declared to contain a single integer value.

An array is a data structure that holds more than one of the same kind of value. In the declaration of an array, we indicate the number of unique values we wish to store in the array.

So if we wish to hold 10 integers in an array named temps, the declaration would be:

int temps[10];

Notice that the number of values to be stored is placed between open and close square braces.

And where the declaration of temp would assign memory in the computer as follows:

temp's declaration in code...
... and how memory is assigned for temp when the program is compiled and run.

 

...the declaration of the array of integers temps assigns memory that look like this:

Code declaration for temps...
...and the memory allocation for temps.

 

Note that the initial contents in each of the integer variables of the array is unpredictable; the programmer is responsible for initializing each variable's value.

Also notice that the location of the first integer in the array temps is at temps[0]; for an array of n values, you reference each value from 0 to n - 1. This is critically important, and bears repeating:

When accessing values of an array of n items, you reference the values from 0 to n - 1.

referencing the last array location via temps[n] rather than temps[n-1] (Commonly refered to as an "Off by 1" error) is one of the most common programming mistakes, and one of the most difficult to detect, as this type of error will compile correctly, but cause the program to crash during execution. Keeping an eye out for it early can save you alot of time and headaches later on.

Assigning a value to a particular array location looks like this:

temps[2] = 5;

In the above example, the value of 5 is put in the 3rd array position of temps.

You can use an integer variable to denote the array position to access:

int temps[5];
int i;
i = 2;
temps[i] = 5;

Using a variable for determining array positions becomes very effective when used in combination with loop structures to assign and access array values:

Using Arrays and Loops to find the Average
When you download and decompress module4.tar via the command

tar cvf module.tar

You'll see 4 programs that find the average of an array of integers; average0.c, average1.c, average2.c, and average3.c. Lets take a look at a step-by-step run of average0.c below.

Average0.c uses the rand() function to generate random numbers, and stores these randomly generated (At least, pseudo-randomly generated, but that's a topic for another module) in an array of 5 integers. The flash movie below includes

  • a button to Step Through each Instruction ,
  • a Stop Button to stop midway during an instruction, and
  • a Rewind  button to go back to the very beginning of the program.

You'll also see that to the right of the actual C code program is vertical column of cells containing numbers. This is a representation of a portion of the computer's memory; you'll see how it changes during the run of the program.

There are a bunch of numbers in each slot of memory, even at the beginning, before we've assigned memory or set any variables; as mentioned above, memory is not wiped clean of data and automatically intialized to null values for you; this will be the case anytime you write a program that stores data in memory, so it will be up to you to be careful to initialize values when necessary.

Continue to the Apply Section ->