Unit#6  Arrays - Elementary Statistics
General Considerations:

Up to this point we dealt with problems that required only a small number of variables. We stressed the need to declare each one of these variables as either an integer or float or double etc. and we also stressed the importance of defining the physical significance of each variable and whenever possible giving the units of measurement of the quantity represented by the variable.

We also saw how to deal with a large number of data that resides in a file only if we can manage our problem by a single sweep of the data.  For example we can set up a loop which reads a number from the file holds it in the memory location score , adds it to the sum and then reads the next number and holds the new one in the same location score ( overwriting the old number), adds it to the sum etc. ….

In this unit we want to deal with a different situation where we will need to refer to a large number of variables in our program.  For example suppose that we want to read 450 scores from a file and then sort them out in descending order and then print to the screen the 20 best scores and the 10 lowest scores. For a problem like this we need to have a device, which will allow us to declare simply, and conveniently a large number of variables and which will allow us to refer to these variables sequentially using a loop.  Arrays provide exactly these features.

Arrays are clusters of indexed memory locations.  When we declare in our program

int score [ 35 ];

thirty five memory locations are reserved for storage of integer data.  The first location is referred by score [ 0 ], the second one by  score[ 1 ], …….. and the last one by score[ 34 ].

Consider the following segment of code:

int score[ 35 ];

for ( k = 0; k <= 34; k++)

{

score[ k ] = pow( 0.57, k );

}

score[ 5 ] = 0.0;

Here the 35 in the first line of the code indicates the size of the array .  It says that there should be 35 memory locations.  But in the third and fourth line the number in the brackets refers to the particular location in the memory cluster declered earlier. In other words the 5 in the fourth line signifies that sixth location must be filled with zero.  Why sixth? Because the locations are numbered starting at 0. In other words we have the locations 0, 1, 2, 3, 4, and 5.  This is the only confusing point with arrays. We hope that you will understand it quickly and start using this wonderful device called array.  Note that the above short segment assigns values to all entries of the array.

Two dimensional arrays

Once we understand the notion of one dimensional arrays ( think of them as a vertical stack of  empty shoe boxes which are ready to receive our data) , we can easily generalize to the notion of two dimensional arrays ( think of them as the two dimensional lattice of teacher mail boxes you see in the school office).

The declaration of a two dimensional array looks like this:

float  temperature [ 10 ] [ 8 ] ;

Here we are talking about an array by the name "temperature", which has 10 rows and 8 columns.  We can represent it with the figure below.  The green element  is the temperature[3][4] .

Rows \ Columns

0

1

2

3

4

5

6

7

 0

               

 1

               

 2

               

 3

               

 4

               

 5

               

 6

               

 7

               

 8

               

 9

               

 

 

Jacob Y. Kazakia © 2001 All rights reserved