0% found this document useful (0 votes)
5 views

Chapter 8 - Array Computer Science

Uploaded by

boomexploder.com
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Chapter 8 - Array Computer Science

Uploaded by

boomexploder.com
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Definition of Array, types with example

• An array is a data structure containing several elements of the same data type; these elements can be accessed using
the same identifier name. The position of each element in an array is identified using the array’s index.

• For better understanding, there is a given array consisting

6 elements. The position starts from 0 index and the element

of position 0 is 2

An array can have

• One dimensional array (1D array)


• Two dimensional array (2D array)

Visualization of Array
Why we should use an array?
One dimensional array (1D array)

An one-dimensional array has just one row of data can be considered as a list.

For example, Numbers = [10, 5, 90, 26, 87]

This array has 5 elements and name of the array is Numbers. The first data item is in position 0 and the data value
is 10.

• Array use third brackets after the identifier to indicate the index we want to access. For example , Array[0] is
accessing the first element in the array/identifier named Numbers,

Numbers[0] = 10
Numbers[1] = 5
Numbers[2] = 90
Numbers[3] = 26
Numbers[4] = 87
One dimensional array declaration in pseudocode

A one-dimensional array can be referred to as a list. Here is an example of a list with 10 elements in it where
the first element has an index of zero.

When a one-dimensional array is declared in pseudocode:

» the name of the array


» the first index value
» the last index value
» and the data type are included.

Formula:

DECLARE NameOfArray : ARRAY[starting index:ending index] OF datatype

For example, to declare a new array called MyList:

DECLARE MyList : ARRAY[0:9] OF INTEGER


One dimensional Array in pseudocode

Each position in the array can be populated in an array by defining the value at each index position.
For instance, we can assign the number 27 to the fourth position in the array MyList as follows:

MyList[4] ← 27

To populate the entire array instead we can use a loop:

Notice that in this code we have used the variable Counter as the array index.

We can display the data that lies in a particular location in an array as follows:

OUTPUT MyList[1] This would display the value 19.


Two dimensional array (2D Array)

A two-dimensional array can be referred to as a table, with rows and columns.

Here is an example of a table with 10 rows and 3 columns, which contains 30 elements.
The first element is located at position 0,0.
When a two-dimensional array is declared in pseudocode:

» the first index value for rows


» the last index value for rows
» the first index value for columns
» the last index value for columns
» and the data type are included.

Formula:

DECLARE NameOfArray : ARRAY[start: end, start: end] OF datatype

For example: DECLARE MyTable : ARRAY[0:9,0:2] OF INTEGER


Two dimensional array (2D Array) in pseudocode
The declared array can then be populated using a loop, just like for one dimensional arrays – however this
time there need to be two nested loops, one for each index:

We can display the data that lies in a particular location in a two-dimensional array as follows:

OUTPUT MyList[2,1]

This would display the value 98.

You might also like