Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Arrays

Array is defined as a finite, ordered set of variables of the same type referenced under a
common name. In programming, we usually use array when the problem requires storage of
same items.

Points to remember
 the variables in an array are called its components or elements
 a specific element in an array is accessed by an index or subscript
 all arrays consist of contiguous memory locations
 the lowest address corresponds to the first element, the highest address corresponds to
the last element
 arrays may have from one to several dimensions
 In C, arrays must be explicitly declared so that the compiler can allocate space for them
in memory
 Note that in C, there are no bounds checking on arrays. This means that you could
overwrite either end of the array and write into some other variable’s data, or even into
a piece of the program’s code.

Let’s look for example an implementation for a population for each of the 16 regions of
the Philippines. In C, we declare this as:

int population [16];

population

 values
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
 indices

The array when declared, starts its first element at a base address (B). Also note that in
C Language, every array starts its index at 0.

population

 Base Address (B)

Base address is expressed in a language the computer can understand (binary or


hexadecimal) but for the purpose of discussion, let’s express B in decimal.

Assume the base address of the array is 100.

population
100
Each element in the array consumes byte(s) depending on the data type used.
Commonly used data types with their equivalent size

Data type Size


char 1 byte
int 2 bytes
float 4 bytes
double 8 bytes

So, if the base address of the array population is 100, the address of its second element is
______. The address of the 5th element? ______ Address of population [9]? ______
population [15]? _______

What if you’re given the array below:

float x[50];
base address = 1500

What is the address of x [23]? ______ x [17]? ______ x [44]? _______

Now, instead of drawing boxes, we use the formula:

Address of array [M] = base + ( M – L ) * element size

where: M is the index of the array, and


L is the lower bound of the array.

In C, lower bound is always zero (0). Therefore, the new formula would be

Address of array [M] = base + M * element size

Example:

float x [50];
base address = 1500

address of x [23] = 1500 + 23 * 4


= 1500 + 92
address of x [23] = 1592

Exercise:

1. Use the same array (x) above.


o What is the address of x [36]? _______
o What is the address of x [49]? _______
2. Given the array below, solve for the base if address of Y_array [5] is 1200.

int Y_array [20];


What is the address of Y_arrayI [12]? _______
Two-dimensional Array

Let’s look at another example. Let’s say we would like to track down the total quarterly
sales of each of the 5 salesmen. We declare this in C as

float sales [4] [5];

row
column

Salesmen
#1 #2 #3 #4 #5
0 1 2 3 4
1st 0 0,0 0,1 0,2 0,3 0,4
Quarter

2nd 1 1,0 1,1 1,2 1,3 1,4


3rd 2
4th 3

Fig. 3.1 Pictorial representation of a 4 rows by 5 columns array

The base of the array is sales [0] [0]

If the base address is 1000, the address 1004 belongs to


a) sales [1][0]
b) sales [0][1]
c) sales [1][1]

What is the address of sales [2][3]? _______ sales [3][1]? _______

Salesmen
#1 #2 #3 #4 #5
0 1 2 3 4
0 1000
Quarter

1
2
3

Though it is fairly easy to compute for the location of a particular element in a two-
dimensional array, it is not always the case. In some instances, the size of the array is so big that
tracking down the address of a particular element would be very hard if done manually. Just like
a single dimensional array, we could formulate a shorter way of doing it.

Derive the formula.

Address of array [M][N] = base + _____________________________________

Hint: you’ll need the range (size) of the column, the row and column index in your formula

Here’s the formula for solving the address of a two-dim array’s element:

Address of array[M][N] = Base + [ (M * R2) + N ] * element size

where: M is the index of the row


N is the index of the column
R2 is the range of the column

Example:

float sales[4][5];
Base address = 2000

address of sales[2][3] = 2000 + [ (2 * 5) + 3 ] * 4


= 2000 + 13 * 4
= 2000 + 52
address of sales[2][3] = 2052

You might also like