1. Overview
Arrays are fundamental data structures in computer science that allow you to store and manage collections of data of the same type under a single variable name. Understanding arrays is crucial for efficient data handling, algorithm development, and solving various programming problems, making them a key topic in the IGCSE Computer Science syllabus.
Key Definitions
- Array: A data structure that stores multiple values of the same data type under a single identifier (variable name).
- Element: Each individual value stored within an array.
- Index: A numerical value that identifies the position of an element within an array.
- One-Dimensional (1D) Array: A linear data structure that stores elements in a single row, like a list.
- Two-Dimensional (2D) Array: A data structure that stores elements in a grid format, with rows and columns, resembling a table or spreadsheet.
- Declaration: The process of defining an array, specifying its name, size, and data type.
- Iteration: The process of repeatedly executing a block of code, often used to access and manipulate array elements.
Core Content
a) Declaring and Using 1D and 2D Arrays
- 1D Arrays:
- Imagine a single row of boxes, each holding a value. That's a 1D array!
- Declaration (Pseudocode):
DECLARE myArray : ARRAY[0:9] OF INTEGER // Creates an array named myArray, holding 10 integers (indexed 0 to 9) DECLARE names : ARRAY[1:5] OF STRING //Creates an array called names holding 5 strings (indexed 1 to 5)ARRAY[lower:upper]specifies the range of indices. The array size isupper - lower + 1. Note that the declaration may use indexing from 0 or 1. Be consistent with the convention used.
- Accessing Elements (Pseudocode):
myArray[3] // Accesses the element at index 3 names[1] // Accesses the element at index 1
- 2D Arrays:
- Think of a spreadsheet or a grid. That's a 2D array! It has rows and columns.
- Declaration (Pseudocode):
DECLARE myGrid : ARRAY[0:4, 0:2] OF INTEGER // Creates a 2D array named myGrid, with 5 rows (0-4) and 3 columns (0-2), holding integers - Accessing Elements (Pseudocode):
myGrid[2, 1] // Accesses the element at row 2, column 1
b) Understanding the Use of Arrays Including Variables as Indexes
- The index used to access an element in an array doesn't have to be a fixed number. It can be a variable!
- Examples:
myArray[i]// Accesses the element at indexi, whereiis a variable.myGrid[row, col]// Accesses the element at rowrowand columncol, whererowandcolare variables.myArray[i+1]// The index is the result of an expression.
- Importance of Variable Indexes:
- Essential for iterating through arrays using loops.
- Allows for dynamic access to array elements based on program logic.
- Example using a Loop (Pseudocode):
DECLARE i : INTEGER FOR i <- 0 TO 9 DO OUTPUT myArray[i] // Prints each element of myArray NEXT i - Index Out of Bounds Error: Trying to access an element using an index that is outside the declared range of the array will cause an error. Always make sure that the value of the index variable is within the array bounds.
c) Writing Values Into and Reading Values From an Array Using Iteration
- 1D Array Processing:
- Use a
FORloop to iterate through all elements. - Example: Inputting Values (Pseudocode):
DECLARE numbers : ARRAY[0:4] OF INTEGER DECLARE i : INTEGER FOR i <- 0 TO 4 DO INPUT numbers[i] // Allows the user to enter a value for each element NEXT i - Example: Outputting Values (Pseudocode):
DECLARE i : INTEGER FOR i <- 0 TO 4 DO OUTPUT numbers[i] NEXT i
- Use a
- 2D Array Processing:
- Use nested
FORloops – one for rows and one for columns. - Example: Inputting Values (Pseudocode):
DECLARE grid : ARRAY[0:2, 0:1] OF INTEGER DECLARE row, col : INTEGER FOR row <- 0 TO 2 DO FOR col <- 0 TO 1 DO INPUT grid[row, col] NEXT col NEXT row - Example: Outputting Values (Pseudocode):
DECLARE row, col : INTEGER FOR row <- 0 TO 2 DO FOR col <- 0 TO 1 DO OUTPUT grid[row, col] NEXT col NEXT row
- Use nested
Exam Focus
- Array Declaration: Know how to declare both 1D and 2D arrays, specifying the data type and index range. Pay attention to whether indexing starts at 0 or 1.
- Array Access: Understand how to access elements using their index, including using variables as indices.
- Iteration: Be able to use
FORloops to iterate through arrays, reading, writing, and processing elements. Master nested loops for 2D arrays. - Data Handling: Be prepared to show how arrays can handle data that requires sample data in the questions.
- Explanation: Expect to provide clear explanations of the algorithms used.
- Technical Terminology: Use terms like "array", "index", "element", "1D array", "2D array", "iteration" correctly.
- Depth of Explanation: Provide detailed explanations of your code and algorithms. Don't just show the code; explain why it works.
Common Mistakes to Avoid
- ❌ Wrong: Providing no sample data when required. ✓ Right: Carefully read the question and include all data requested.
- ❌ Wrong: Only showing code to initialize an array to zero without explanation. ✓ Right: Explain step-by-step how the code initializes the array, especially when setting values to zero. For example, "The code uses a nested loop to iterate through each element of the 2D array TeamPoints. Inside the inner loop, the value of TeamPoints[row, col] is set to 0, ensuring all scores are initialized to zero".
- ❌ Wrong: Using incorrect array indexing (e.g., going out of bounds). ✓ Right: Double-check your loop conditions and index calculations to prevent errors.
- ❌ Wrong: Providing code unrelated to the prompt. ✓ Right: Carefully read the problem prompt and include ONLY relevant explanation and code.
- ❌ Wrong: Using vague or inappropriate variable names. ✓ Right: Use descriptive variable names (e.g.,
teamScoresinstead ofa) and avoid spaces or punctuation in names.
Exam Tips
- Read the Question Carefully: Understand exactly what the question is asking you to do with the array.
- Plan Your Code: Before writing code, sketch out the logic you'll use to solve the problem, including how you'll initialize, access, and process the array.
- Test Your Code: Mentally (or on paper) test your code with sample data to ensure it works correctly, especially the loop conditions and array indexing.
- Explain Clearly: When asked to describe your code, provide clear and concise explanations of each step. Use technical terminology where appropriate.