Back to 课程

Computer Science GCES OCR

0% Complete
0/0 Steps
  1. Cpu Architecture Performance And Embedded Systems Ocr
    5 主题
  2. Primary And Secondary Storage Ocr
    6 主题
  3. Data Storage And Compression Ocr
    12 主题
  4. Networks And Topologies Ocr
    6 主题
  5. Wired And Wireless Networks Protocols And Layers Ocr
    6 主题
  6. Identifying And Preventing Threats To Computer Systems And Networks Ocr
    2 主题
  7. Operating Systems And Utility Software Ocr
    2 主题
  8. Ethical Legal Cultural And Environmental Impact Ocr
    2 主题
  9. Computational Thinking Searching And Sorting Algorithms Ocr
    3 主题
  10. Designing Creating And Refining Algorithms Ocr
    5 主题
  11. Programming Fundamentals And Data Types Ocr
    5 主题
  12. Additional Programming Techniques Ocr
    7 主题
  13. Defensive Design And Testing Ocr
    6 主题
  14. Boolean Logic Diagrams Ocr
    2 主题
  15. Programming Languages And Integrated Development Environments Ides Ocr
    3 主题
  16. The Exam Papers Ocr
    2 主题
  17. Structuring Your Responses Ocr
    3 主题
课 Progress
0% Complete

Exam code:J277

1-Dimensional Arrays

What is an array?

  • An array is an ordered, static set of elements in a fixed size memory location

  • An array can only store 1 data type

  • A 1D array is a linear array

  • Indexes start at 0, known as zero indexed

1d-array-example

Concept

OCR exam reference

Python

Description

Create blank 1D array

array scores[5]

scores = [0]*5

Creates an array with 5 elements (0–4), each initialised to 0

Create empty array

n/a

scores = []

Creates an empty list (no elements)

Assignment

colours[4] = "Red"

colours[4] = "Red"

Assigns the colour “Red” to index 4 (5th element)

Example in Python

Creating a one-dimensional array called ‘array’ which contains 5 integers.

  • Create the array with the following syntax:
    array = [1, 2, 3, 4, 5]

  • Access the individual elements of the array by using the following syntax:
    array[index]

  • Modify the individual elements by assigning new values to specific indexes using the following syntax:
    array[index] = newValue

  • Use the len function to determine the length of the array by using the following syntax:
    len(array)

  • In the example the array has been iterated through to output each element within the array. A for loop has been used for this

# Creating a one-dimensional array
array = [1, 2, 3, 4, 5]

# Accessing elements of the array
print(array[0]) # Output: 1
print(array[2]) # Output: 3

# Modifying elements of the array
array[1] = 10
print(array) # Output: [1, 10, 3, 4, 5]

# Iterating over the array
for element in array:
print(element)

# Output:
# 1
# 10
# 3
# 4
# 5

# Length of the array
length = len(array)
print(length) # Output: 5

2-Dimensional Arrays

What is a 2-dimensional array?

  • A 2D array extends the concept on a 1D array by adding another dimension

  • A 2D array can be visualised as a table with rows and columns

  • When navigating through a 2D array you first have to go down the rows and then across the columns to find a position within the array

2d-array-example

Concept

OCR exam reference

Python

Description

Create blank 2D array

ARRAY players[3,3]

players = [[None]*3 for _ in range(3)]

Creates a 3×3 2D array with all values set to None

Create populated 2D array

players = [["Rob", "Paul", "Hayley"], [10, 5, 8]]

players = [["Rob", "Paul", "Hayley"], [10, 5, 8]]

Creates a 2D array with names in row 0 and scores in row 1

Assign a value

players[0,1] = "Holly"

players[0][1] = "Holly"

Replaces "Paul" in row 0, column 1 with "Holly"

Access a value

value = players[2,2]

value = players[2][2]

Retrieves the value at row 2, column 2

Example in Python

# Initialising a 2D array with 3 rows and 3 columns, with the specified values
array_2d = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]

# Accessing elements in the 2D array
print(array_2d[0][0]) # Output: 1
print(array_2d[1][2]) # Output: 6

Examiner Tips and Tricks

In the exam, the question will always give an example to demonstrate which order the array is being read from.

Some questions can be X,Y and others can be Y, X. Always refer to the example before giving your answer!

Worked Example

A parent records the length of time being spent watching TV by 4 children

Data for one week (Monday to Friday) is stored in a 2D array with the identifier minsWatched.

The following table shows the array

 

Quinn

Lyla

Harry

Elias

0

1

2

3

Monday

0

34

67

89

78

Tuesday

1

56

43

45

56

Wednesday

2

122

23

34

45

Thursday

3

13

109

23

90

Friday

4

47

100

167

23

Write a line of code to output the number of minutes that Lyla watched TV on Tuesday [1]  

Write a line of code to output the number of minutes that Harry watched TV on Friday [1]

Write a line of code to output the number of minutes that Quinn watched TV on Wednesday [1]

Answers 

  • print(minsWatched[1,1] or print(minsWatched[1][1]

  • print(minsWatched[2,4] or print(minsWatched[2][4]

  • print(minsWatched[0,2] or print(minsWatched[0][2]

Responses

您的邮箱地址不会被公开。 必填项已用 * 标注