Slides badge

Arrays

Learning Intentions

  • Describe, use and exemplify several data types

  • Describe and use 1D arrays

Success Criteria

  • Be able to explain the different data types and what they do
  • Be able to explain what a 1D array is and use them in code

Arrays

  • An array is a data structure.

  • Arrays let programmers store ‘lists’ of related data in one data structure.

  • For example, we could use an array to store pupil marks.
    Each value in a list is given its own position/index value so it can easily be referenced.

  • Arrays can be assigned to variables.

Arrays

  • Often a program will have to store lots of information at the one time, for example, when a teacher is entering in the names of all their pupils. It is unrealistic to have a separate variable for each of the names (StudentName1, StudentName2….etc).

  • Arrays allow the programmer to store more than one piece of data within a single data structure.

Declaring an array in Python

The element data type can be integers, reals, Booleans, characters or string

Image copyright jamiebalfour.scot

Declaring an array

strings = [""] * 5
integers = [0] * 5
reals = [0.0] * 5
booleans = [False] * 5
characters = [""] * 5

Assume we have the following program

pupil1Name = "Matthew"
pupil2Name = "Charlie"
pupil3Name = "Emilis"
pupil4Name = "Cameron"
pupil5Name = "Logan"
pupil6Name = "Ruaridh"
pupil7Name = "Jasmine"
pupil8Name = "Ignacy"
pupil9Name = "Findlay"
pupil10Name = "Aimee"
pupil11Name = "Lucas"
pupil12Name = "Billy"
pupil13Name = "AJ"

The problem

  • Now what happens if we wanted to add another pupil called Jordan? We would have to now add in another variable this time called pupil14Name.
  • But what if on one occasion we only had 3 pupils, not 13? Our program is hard coded/fixed to accept 13 pupils. This is where we can use arrays.

The fix - using an array

pupils = [""] * 14
pupils[0] = "Matthew"
pupils[1] = "Charlie"
pupils[2] = "Emilis"
pupils[3] = "Cameron"
pupils[4] = "Logan"
pupils[5] = "Ruaridh"
pupils[6] = "Jasmine"
pupils[7] = "Ignacy"
pupils[8] = "Findlay"
pupils[9] = "Aimee"
pupils[10] = "Lucas"
pupils[11] = "Billy"
pupils[12] = "AJ"
pupils[13] = "Jordan"

There still exists a problem with this program

  • This program is still stuck with 14 pupils, what if we wanted to ask the user to tell us how many pupils they have?

This comes up a lot in National 5 assignments!

The fix - ask the user how many pupils they want!

  • This program is still stuck with 14 pupils, what if we wanted to ask the user to tell us how many pupils they have?

This comes up a lot in National 5 assignments!

print("Please insert the number of pupils required.")
numberOfPupilsRequired = int(input())

pupils = [""] * numberOfPupilsRequired

for i in range(0, numberOfPupils):
  print("Please insert the name of pupil", i, ".")
  pupils[i] = input()

The benefits of arrays

  • Arrays save time when declaring variables as all we need to do is specify how many elements (the parts that make up an array) we need and the type of the array.
  • Arrays can be resized as the program runs (as shown before)
  • Arrays tie very nicely to fixed and conditional loops
  • Arrays use less memory than several variables, can you think why?

In SQA Reference language

  • When you are asked a question in an exam paper in the SQA reference language, for example to fill in a line where an array should be, you should use something similar to the following syntax:

 

 

 

 

  • This defines an empty array – that is each element of the array contains an empty string, denoted by the ""

DECLARE playerNames AS ARRAY OF STRING INITIALLY [""]*7

Arrays example: adding all numbers in an array

  • This comes up a lot in exams:
numbers = [1, 2, 3, 4, 5]
#1 mark for total variable
total = 0

for i in range(len(numbers)):
  total = total + numbers[i]

  print(total)

Work through the arrays worksheet

Task

Presentation Overview
Close
JB
Arrays
© 2020 - 2024 J Balfour
23:49 | 06-12-2024
Join Live Session
Go Live
Start Remote
Save Progress
Slideshow Outline
Presenter Mode
Generate Quiz
Generate Lesson Plan
Widget Screen
Canvas Controls
Fullscreen
Random Selector
Timer
Volume Meter
Binary Converter
Python Editor
Show Knox 90
Provide Feedback
Help
!
Keywords
    DragonDocs Management
    Random selector
    Sections
      Binary conversion
      Denary to binary conversion
      Binary to denary conversion
      Feedback 👍
      Accessibility

      Apply a filter:

      ×
      Loading
      All slideshow files