Bits of Bytes.co

IGCSE / GCSE Computer Science

  • Home
  • 1 - Theory of Computer Science
  • 2 - Practical Problem Solving and Programming
  • Pre-Release Material
    • 2018 June >
      • 22 >
        • 22 - Cow Task
        • 22 - Task 1
        • 22 - Task 2
        • 22 - Task 3
  • 1 - Computer Systems (2023)
Picture
MENU
  • Home
  • 1 - Theory of Computer Science
  • 2 - Practical Problem Solving and Programming
  • Pre-Release Material
    • 2018 June >
      • 22 >
        • 22 - Cow Task
        • 22 - Task 1
        • 22 - Task 2
        • 22 - Task 3
  • 1 - Computer Systems (2023)
2 - Practical Problem Solving & Programming / Arrays


Arrays


Variables are used for storing one piece of data. Imagine that your system needs to keep track of the ages of 30 people... this would require 30 separate variables. Not necessarily!  Arrays can solve this problem by providing a single data structure that can store a number of separate pieces of data.

One dimensional arrays

An array is a data structure that allows multiple, separate pieces of data to be stored  name (identifier). Within the array, each item is stored with an address referred to as its index. See below example:
Array name - studentNames
​Index
1
2
3
4
5
Data
"Mary"
"Paul"
"Ivy"
"Will"
"Dan"
 In the above example you can see that the array named "studentNames" contains 5 different names.
Each name is given an index number.

In arrays it is important to note that the index numbers can start at 0 or 1 depending on the system. It is good practice to explicitly state the starting index when declaring an array. CAIE will accept both methods, however they state in their Pseudocode guide that "generally a lower bound of 1 will be used" and therefor we will follow this idea on this page.

Once you have an array that contains data like this you can choose to output certain items by using the name and the desired index number within squared brackets.

arrayname[index]

Example​
Array output example

    
When the example code above is executed the output will be:

Ivy


Declaring Arrays

The declaration process for arrays can differ slightly between languages but in terms of the CAIE pseudocode, we have been given the following rules to follow:

 DECLARE <identifier> : ARRAY [<1>:<n>] of <data type>

If we were to follow this convention to declare our studentNames array, the code would look like this:
CIE Pseudocode array declaration

    
Following these declaration rules set out by CAIE means that it is important to choose the correct data type.  An array can store data of any type e.g. String, Integer, Real.. but you must identify this at the end of the declaration line.


Vertical Divider

Declaring Arrays (Unknown size)


In a lot of cases, you will be required to declare an array, however you (the programmer) will not know how big it needs to be.  In these cases the user of the program will need to state how big the array should be and this value could change on each use.

Example Scenario - A program will allow a teacher to state how many students they have. They can then enter each of their students names into the array.

Solution - The solution for this problem is to use a variable .  The user will state how many students that they have and we store this number in a variable.  This variable can then be used when declaring the array to ensure that it is the required size:
Sequence within other structures

    
In the above example, on line 2 the user will state a number of students.  Line 3 will take this number and create an empty array of that length ready for the names to be entered.

Filling an Array


Once you have correctly set up your array with the correct size you will eventually need to fill it with data.

Data can be assigned to the array at specific index's similarly to variables.  If you know exactly which index that you would like to add some data into then you would achieve this with code like this:
Entering data into a specific index

    
The code above demonstrates how to add hard coded strings and variables into specific array indexes.

Next we will look at filling every space in an array by using a loop.

Filling an array with a loop


It is quite common that your code will be required to allow the user to fill the array with data.

Example Scenario - A program will allow a teacher to state how many students that they have. They will then be given the opportunity to enter and store each students name.

Solution - We have already looked at the first part of the problem (the declaration) in the above section.  We now need to understand the most efficient way to allow data entry at every index.

The best way to achieve this is by using a FOR loop. A FOR loop is used when we know precisely how many times a repetition is required.  As we know the size of the array, and there for how many items of data are to be entered, a FOR loop is the logical choice.

​The code for this solution would look like this:
Entering data into an array using a loop

    
Why does this work?

The key to this working is the fact that we can use the FOR counting variable as the arrays index for input on each iteration of the loop.

In this example the FOR counting variable is called X. This Variable starts with the value of 1 and it will increases by 1 on each repetition until it reaches the value stored in the noStudents variable.

line 7 uses this x variable as the array index.  as x increases by 1 on each repetition it means that data will keep being placed in the next index position until the array is full.

Outputting array items


If you wish to output a specific item in an array then you will do so like this:

OUTPUT <array name>[index]

This will allow you to output a single array item.

If you wish to output all items within an array then you will need to use a FOR loop just like you may have used to insert data.  The code will look almost identical to the input method, however this time you will be using OUTPUT instead of INPUT.

​Take a look at the code for this below

Output data using a loop

    
The output code starts on line 9 and ends on line 11. On the first repetition x is 1 and it will increase by 1 until it reaches the number of students.  On each repetition, line 10 will output the index x of the studentNames array.  These 3 lines of code will output the entire contents of the array.
Est. 2015 - Copyright © 2020