1D Arrays
1D Arrays
1 DIMENSIONAL ARRAYS
Store a list of variables of the same data type.
Conceptual Explanations
1. Stores multiple values
2. Data stored is of the same type
3. Data is stored in array positions known as Index
Array Name
Upper Bound Index
1 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618
Array Operations
1. Array Declaration/Creation
DECLARE <Array-Name> : ARRAY[Lower Bound Index : Upper Bound Index] OF <DataType>
Example;
DECLARE StudentNames : ARRAY[1:10] OF STRING
Array Size
2 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618
Array Operations
2. Storing and Display/Access values from an array
Example;
Create an array of size 3 called StudentNames and use it to store the names in brackets respectively (“Bill”,
”Greta”, ”Daniella”)
Soln:
1.Creating an array
DECLARE StudentNames : ARRAY[1:3] OF STRING
NOTE: When storing and retrieving array elements we shall always use a
for loop
3. Accessing array elements
Qtn: Retrieve array value in position or array index 2
OUTPUT StudentNames[2]
3 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618
Array Pseudocodes
Qtn 1: Write a pseudocode program which inputs a number and store it in array position 2.
(Array name is Numbers and size is 5)
Qtn 3: Use a for loop to retrieve all stored array elements in question2
FOR Index 1 TO 100
OUTPUT ResultArray[Index]
NEXT Index
4 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618
Searching In An Array
NOTE: For searching we shall use a condition.
Qtn 1: There is an array which contains names of 500 students, search at which position is “Lucy” stored.(Array Name =
“StudentNames”)
DECLARE Index : INTEGER
FOR Index 1 TO 500
IF StudentNames[Index] = “Lucy”
THEN
OUTPUT Index
ENDIF
NEXT Index
Qtn 2: Already there is an array named “SearchBox” with 500 elements of data type String. Search through the array and
find out how many times “Empty” was repeated
DECLARE Index : INTEGER
DECLARE EmptyCount : INTEGER
EmptyCount 0
FOR Index 1 TO 500
IF SearchBox[Index] = ”Empty”
THEN
EmptyCount EmptyCount+1
OUTPUT EmptyCount
ENDIF
NEXT Index
5 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618
6 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618
7 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618
8 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618
9 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618
10 Davis_Kazibwe@2023KIS