VCE IT Lecture Notes by Mark Kelly, McKinnon Secondary College

Arrays

Software Development 2011+

When programming, using independent variables for storage is fine - up to a point. You can use as many as you like, but each one must be addressed by its unique name. You cannot work on a group of them in one operation.

You need code like this:

total_cost = fuel_expense + rent_costs + food_costs + insurance + repair_costs

If you had a dozen variables to sum, the typing alone would rapidly become a pain.

This is where arrays are so valuable... they let you work with any num of variables as a group.

So, if you had a dozen costs you could create an array called COSTS, define it as having 12 storage 'slots' and use any of them by referring to its slot num, or index.

In BASIC, arrays are declared using the DIM (short for DIMENSION) keyword, the name of the array, and the size of its maximum index value like this:

DIM COST[4]

Then you can use it...

total_cost = COST[1] + COST[2] + COST[3] + COST[4]...

This does not seem much of an improvement on the original code, but the real power of arrays is when they're teamed with loops. Because the name of the storage structure (COST) remains the same, only the index needs to be specified, for example

i = 1
total_cost=0

LOOP
   TOTAL_COST = TOTAL_COST + COST[i]

WHILE I <= 12

You could work with a million items with the same num of lines of code!

The COST array above is an example of a one dimensional array.

Arrays, however, can have two, three or more dimensions (but anything over 3 gets hard to visualise).

Consider the need to store 3 values for each of 12 months (e.g. rent, phone costs, transport). You need 36 storage locations. If you created 36 discrete (separate) variables, there would be a lot of typing, and you would have very little power to manipulate the data.

However, create a two dimensional array like this... COSTS[12,3]

Think of it like a table with 12 rows (or columns) and 3 columns (or rows). Cells in the array are addressed Melways-style by referring to the row/column num.

COST[ ] Cost 1 2 3
Month 1
20
30
40
2
21
31
41
3
22
32
42
4
23
33
43
5
24
34
44
6
25
35
45
7
26
36
46
8
27
37
457
9
28
38
48
10
29
39
49
11
30
40
50
Month 12
31
41
51

Now you can add the grand total using 2 nested loops - one inside the other. Here I'll use BASIC's FOR..NEXT loop.

FOR monthnum = 1 to 12
   FOR costnum = 1 to 3
    totalcost = totalcost + COST[monthnum, costnum]
  NEXT costnum
NEXT monthnum

The FOR statements each have a loop counter (monthnum and costnum). Each counter starts at the starting value (1) and goes up top the ending value (12 for the month loop and 3 for the cost loop).

So the pair of loops begins. Monthnum is initialised to 1. The very next statement is another FOR so costnum is initialised to 1. The totalcost calculation is carried out, and it plugs in the current values of monthnum and costnum so you get

    totalcost = totalcost + COST[1, 1]
In other words, totalcost = totalcost + 20.

The "NEXT monthnum state" terminates the inner loop. It checks to see if the counter has reached its ending value. If not, it loops back to the matching FOR statement, increments the value of the counter (so costnum now is 2) and repeats, giving...

    totalcost = totalcost + COST[1, 2]
In other words, totalcost = totalcost + 30.

After the third loop of the costnum loop, its counter hits its max value and it drops down to the NEXT statement of the monthnum loop and that loop now terminates, checks whether it has finished, and loops back to start all over again, this time with monthnum = 2.

So you can see that for each month that ticks by, three costs are processed.

Eventually, after all 3 costs are added for all 12 months, the monthnum loop finishes and the program continues.

 

If you're still with me, good. We're ready for three dimensional arrays.

Imagine, as before, there are 3 costs over 12 months, but now there are a hundred years...

That's 3600 values to process. Not easy with discrete variables.

But with loops and an array, it's a doddle.

Create the array:

DIM COST[100,12,3]

For each of the 100 years, there are 12 months. For each month there are 3 costs.

Then the turbo kicks in...

FOR yearnum = 1 to 100
  FOR monthnum = 1 to 12
    FOR costnum = 1 to 3
      totalcost = totalcost + COST[yearnum, monthnum, costnum]
    NEXT costnum
  NEXT monthnum
NEXT yearnum

Look at the nested loops like a car's odometer counting kilometers.

The innermost dial is spinning the fastest. Every time it hits '9', the next dial ticks over 1. When that dial passes '9', the next dial ticks over 1. That can continue for as many dials (loops) as you care to create (or your brain can handle).

Want an example of a 4 dimensional array?

Think of the example above, but now the same data must be stored for 16 different departments in the organisation.

Five dimensional array?

Same as above, but now there are branches of the organisation in 33 countries, each with 16 departments, each with 100 years, each with 12 months, each with 3 costs.

Now we're up to 2,059,200 values - all controlled with a handful of lines like this...

FOR countrynum = 1 to 33
  FOR deptnum = 1 to 16
    FOR yearnum = 1 to 100
          FOR monthnum = 1 to 12
      FOR costnum = 1 to 3
        totalcost = totalcost + COST[countrynum, deptnum, yearnum, monthnum, costnum]
      NEXT costnum
     NEXT monthnum
   NEXT yearnum
  NEXT deptnum

NEXT countrynum

Relax. The study design only mandates the knowledge of two-dimensional arrays.



Not only can you manage sheer amounts of data, you can customise your processing. We'll revert to an earlier scenario to make life easier.

Let's say we just wanted the total for year 56. Easy.

yearnum = 56
FOR monthnum = 1 to 12
  FOR costnum = 1 to 3
    totalcost = totalcost + COST[yearnum, monthnum, costnum]
  NEXT costnum
NEXT monthnum

Or we could loop through in a different direction to find the totals for cost 2 over years 33 to 77...

FOR yearnum = 33 to 77
  FOR monthnum = 1 to 12
    totalcost = totalcost + COST[yearnum, monthnum, 2]
  NEXT monthnum
NEXT yearnum

(Notice how I used the constant 2 in this case. It's usually not wise, since it's so inflexible: after all, 2 will always be 2. Using a variable lets you change the cost num easily.

When the true power of arrays and loops finally shines upon you, it's a bit like discovering girls... the possiblities are nearly endless.

A challenge: how would you represent the following data constructs?

- a list of 13 people's incomes?

- a chess board?

- a deck of playing cards?

- rainfall figures from 5 locations in each of 6 states over the past 80 years?

Back to the IT Lecture Notes index

Back to the last page you visited

Created 30 March 2010

Last changed: September 15, 2010 5:16 PM

VCE IT Lecture notes copyright © Mark Kelly 2001-