Wednesday, 14 June 2017

Arrays

Introduction to Arrays

Arrays are the most commonly used data structure in Computer Programming and is implemented in some way in almost all programming language.

Definition:
An array is a data structure that stores a collection of items under the same data and accessible through an ordered set of integers called subscripts or index. The array index is used to reference each of the array elements.

Array may also be viewed as a collection of variables of the same data type stored in a contiguous memory location. Arrays are fixed size meaning that the number of elements are fixe at the time of declaration.
An array can have one, two or more subscripts. When an array have 2 subscripts, it is said to be a 2-dimensional array. For 3 subscripts, it is 2-dimensional array.
Individual elements in an array is called elements.

One-Dimensional Arrays
One-dimensional array also known a single-dimensional array or linear array where the elements can be accessed using a single index.

An example of one-dimensional array is shown in Figure 1.
Figure 1: One-Dimensional Array

The array is made up of 6 elements: A[0] to A[6]. Also note that the index of an array most times start from 0;

Two-Dimensional Array
A two dimensional array has its elements reference by using two subscripts and are referred to as non-linear array. Think of a 2 by 2 matrix where the elements are arranged in rows and columns.
Example of a two dimensional array is shown in Figure 2.

Figure 2: A Two Dimensional Array
To reference an element of a two dimensional array, you need to specify the row index and the column index.
So the element

A[i][j]  (it could also by A(i, j) in another language)

referes  to the element at row i and column j. In the example above, A[0][3] means the element at row 0 and column 3 and that is 34.

Array Declaration
Before an array is used in a program, it is first declared. The declaration sets aside memory space for storage of the array. Array declaration  reserves contagious memory locations for the elements of the array. 
In array declaration, the following is done:
  • Specify the name of the array
  • Specify the data type for the array elements
  • Specify the dimension of the array


In the C programming language, an array can be declared with the following syntax:

int A[5][4];
This means that:
  • The array has a name A
  • The array elements would be integer values
  • The array would have 5 rows and 4 columns


Could you work out the maximum number of elements the above array can hold?
Simply multiply the subscripts, that is 5 x 4 = 20 elements

Adding Elements to Arrays
The computer programmer would have to supply the value of the elements of the array. To assign a value to an array, you assign value using the subscript of the array elements. For example;

A[0] = 87;

This assigns the value of 87 to the first element of the one-dimensional array A

A[1][2] = 34;

This line assigns the value of 34 to the element at the second row and third column(remember the first column is 0 an the first row is 0)

You can also assign values to the array elements using loop. The algorithm below would read input fro the user an continue to assign to the array until all the elements are assigned.

Initialize array A[10]
Initialize i = 0
DO
READ VALUE FROM INPUT
ASSIGNE TO VALUE TO A[i]
INCREMENT i
WHILE( i<= 10) CONTINUE

The loop continues to read integer value from the input and assign to the array until all the elements of the array is filled up, that is the subscript reaches maximum.