Topic: 2.2.2 Arrays: DIM Names (4) As String
Topic: 2.2.2 Arrays: DIM Names (4) As String
Arrays are declared in a similar way to standard variables, except that the array size and dimensions are
included. For example, the following declares an array that reserves five locations in memory and labels
these as ‘Names’:
The five individual locations are Names(0), Names(1), Names(2), Names(3), Names(4).Each data item is
called an “element” of the array. To reference a particular element a programmermust use the appropriate
U U
index. For example, the following statement assigns data to the 5thelement:
P P
Names(4) = “Johal”
Arrays simplify the processing of similar data. An algorithm for getting five names from the user and
storing them in the array Names is shown below:
One-dimensional arrays
A one-dimensional array is a data structure in which the array is declared using a single index and can be
visually represented as a list.
The following diagram shows the visual representation of the array Names(4):
Page 1 of 7
Computer Science 9608 (Notes)
Chapter: 2.2 Data representation
A two-dimensional array is a data structure in which the array is declared using two indices and can be
visually represented as a table.
Each individual element can be referenced by its row and column indices. For example:
Initializing an array
U
Initializing an array is a procedure in which every value in the array is set with starter values – this starting
value would typically be “” for a string array, or 0 for a numeric array.
Initialization needs to be done to ensure that the array does not contain results from a previous use,
elsewhere in the program.
Page 2 of 7
Computer Science 9608 (Notes)
Chapter: 2.2 Data representation
Note that the variable Flag (line 04 and 09) is used to indicate when the item has been found and stop the
loop repeating unnecessarily (line 12 ends the loop if Flag has been set to True).
To complete the search algorithm, some lines should be added, after the loop, to detect the times when
the item X was not found in the array:
Page 3 of 7
Computer Science 9608 (Notes)
Chapter: 2.2 Data representation
A bubble sort, a sorting algorithm that continuously steps through a list, swapping items until they appear
in the correct order.
Bubble sort is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted,
comparing each pair and swapping them if they are in the wrong order. The pass through the list is
repeated until no swaps are needed, which indicates that the list is sorted. The algorithm gets its name
from the way larger elements "bubble" to the top of the list. It is a very slow way of sorting data and rarely
used in industry. There are much faster sorting algorithms out there such as insertion sort and quick sort
which you will meet in A2.
http://en.wikipedia.org/wiki/Bubble_sort#mediaviewer/File:Bubble-sort-example-300px.gif
34TU U34T
Step-by-step example
Let us take the array of numbers "5 1 4 2 8", and sort the array from lowest number to greatest number
using bubble sort algorithm. In each step, elements written in bold are being compared.
First Pass:
( 5 1 4 2 8 ) ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps them since 5 > 1
( 1 5 4 2 8 ) ( 1 4 5 2 8 ), It then compares the second and third items and swaps them since 5 > 4
( 1 4 2 5 8 ) ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5), algorithm does not swap
them.
The algorithm has reached the end of the list of numbers and the largest number, 8, has bubbled to the
top. It now starts again.
Second Pass:
( 1 4 2 5 8 ) ( 1 4 2 5 8 ), no swap needed
( 1 2 4 5 8 ) ( 1 2 4 5 8 ), no swap needed
( 1 2 4 5 8 ) ( 1 2 4 5 8 ), no swap needed
Now, the array is already sorted, but our algorithm does not know if it is completed. The algorithm needs
one whole pass without any swap to know it is sorted.
Page 4 of 7
Computer Science 9608 (Notes)
Chapter: 2.2 Data representation
(12458) (12458)
(12458) (12458)
(12458) (12458)
(12458) (12458)
Pseudocode implementation:
do
32T
swapped = false
32T
while swapped
32T
end procedure
32T
Page 5 of 7
Computer Science 9608 (Notes)
Chapter: 2.2 Data representation
We will now look at an example in Visual Basic using an array of people's heights. The following data set
is being passed:
height
1 98
2 12
3 99
4 54
Do
50T 36T50
Swapped = False
36T 36T 50T 43T50 36T4 36T 50T 43T50
Temp = height(Count)
10T 10T 10T 10T5 50T 10T 10T 50T 43T50 10T43
swapped = True
36T
EndIf
36T
Next
36T 36T 50T 36T50
Next
36T
EndSub
Page 6 of 7
Computer Science 9608 (Notes)
Chapter: 2.2 Data representation
supposed to be either the location of the list item where the desired value was found; or an invalid
location -1, to indicate that the desired element does not occur in the list.
0T 0T
Return ''-1''
36T 12T36
36T 36T 10T 10T5 10T5 10T48 48T 48T 48T 48T 48T 48T 48T 48T 48T 48T 48T 48T 48T 48T 48T 48T 48T 48T 48T 48T 10T48
searchItem = console.readline()
36T 36T 50T 43T50 36T4 36T4
For x = 0 to 10
36T 36T 10T 10T 10T 10T5 50T 36T
console.writeline("Found item " & searchItem & " at position " & x)
36T
Exit For
36T
EndIf
36T 36T 50T 43T50 36T4
If x = 10 Then
50T 39T50 10T39 10T5 43T50 10T43
console.writeline(-1)
36T
EndIf
36T
Next
Page 7 of 7