Back to 课程

Computer-science_A-level_Cie

0% Complete
0/0 Steps
  1. computers-and-components
    6 主题
  2. logic-gates-and-logic-circuits
    2 主题
  3. central-processing-unit-cpu-architecture
    6 主题
  4. assembly-language-
    4 主题
  5. bit-manipulation
    1 主题
  6. operating-systems
    3 主题
  7. language-translators
    2 主题
  8. data-security
    3 主题
  9. data-integrity
    1 主题
  10. ethics-and-ownership
    3 主题
  11. database-concepts
    3 主题
  12. database-management-systems-dbms-
    1 主题
  13. data-definition-language-ddl-and-data-manipulation-language-dml
    1 主题
  14. computational-thinking-skills
    1 主题
  15. algorithms
    14 主题
  16. data-types-and-records
    2 主题
  17. arrays
    2 主题
  18. files
    1 主题
  19. introduction-to-abstract-data-types-adt
    1 主题
  20. programming-basics
    1 主题
  21. constructs
    2 主题
  22. structured-programming
    1 主题
  23. program-development-life-cycle
    2 主题
  24. program-design-
    2 主题
  25. program-testing-and-maintenance
    3 主题
  26. user-defined-data-types
    1 主题
  27. file-organisation-and-access-
    3 主题
  28. floating-point-numbers-representation-and-manipulation
    3 主题
  29. protocols
    2 主题
  30. circuit-switching-packet-switching
    1 主题
  31. processors-parallel-processing-and-virtual-machines
    5 主题
  32. boolean-algebra-and-logic-circuits
    4 主题
  33. purposes-of-an-operating-system-os
    3 主题
  34. translation-software
    3 主题
  35. encryption-encryption-protocols-and-digital-certificates
    3 主题
  36. artificial-intelligence-ai
    4 主题
  37. recursion
    1 主题
  38. programming-paradigms
    4 主题
  39. object-oriented-programming
    7 主题
  40. file-processing-and-exception-handling
    2 主题
  41. data-representation
    5 主题
  42. multimedia
    3 主题
  43. compression
    2 主题
  44. networks-and-the-internet
    11 主题
课 Progress
0% Complete

Arrays

What is an array?

  • An array is an ordered, static set of elements

  • Can only store 1 data type

  • The position of each element in an array is identified using the array’s index

  • The array’s first element is the lower bound (LB)

  • The array’s last element is the upper bound (UB)

  • The lower bound of an array is typically 0 or 1 depending on the language being used

  • An array can be one-dimensional or multi-dimensional

One-dimensional (1D) arrays

  • A 1D array is a linear array

Structure of a 1D array
  • To declare a 1D array in pseudocode you must include the lower bound, upper bound and data types:

DECLARE <identifier> : ARRAY[LB:UB] OF <data type>
  • In this example a 1D array of five elements each containing single character can be declared as:

DECLARE Letters : ARRAY[0:4] OF CHAR
  • An example complete program could be:

// Declare the array
DECLARE Letters : ARRAY[0:4] OF CHAR // Assign values to each index
Letters[0] ← 'B'
Letters[1] ← 'E'
Letters[2] ← 'A'
Letters[3] ← 'D'
Letters[4] ← 'S' // Output the full array
FOR Index ← 0 TO 4 OUTPUT Letters[Index]
NEXT Index
  • The array is declared with indices from 0 to 4

  • Each element stores a single character using the CHAR data type

  • The loop outputs each letter in order

Two-dimensional (2D) arrays

  • A 2D array can be visualised as a table

  • 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

Structure of a 2D array
  • In 2D arrays the following must be declared:

    • Lower bound for rows (LBR) & upper bound for rows (UBR)

    • Lower bound for columns (LBC) & upper bound for columns (UBC)

DECLARE <identifier> : ARRAY[LBR:UBR, LBC:UBC] OF <data type>
  • In this example a 2D array can be declared as:

DECLARE Letters : ARRAY[0:2, 0:4] OF CHAR
  • An example complete program could be:

DECLARE Letters : ARRAY[0:2, 0:4] OF CHAR // Row 0
Grid[0,0] ← 'B'
Grid[0,1] ← 'E'
Grid[0,2] ← 'A'
Grid[0,3] ← 'D'
Grid[0,4] ← 'S' // Row 1
Grid[1,0] ← 'S'
Grid[1,1] ← 'E'
Grid[1,2] ← 'V'
Grid[1,3] ← 'E'
Grid[1,4] ← 'N' // Row 2
Grid[2,0] ← 'W'
Grid[2,1] ← 'H'
Grid[2,2] ← 'I'
Grid[2,3] ← 'T'
Grid[2,4] ← 'E'
  • ARRAY[0:2, 0:4] creates 3 rows and 5 columns

  • First index is the row, second is the column: Letters[row, column]

  • All elements are of type CHAR

Worked Example

A program reads data from a file and searches for specific data.

The main program needs to read 25 integer data items from the text file Data.txt into a local 1D array, DataArray

Write program code to declare the local array DataArray [1]

Answer

  • 1D array with name DataArray (with 25 elements of type Integer) [1 mark]

Java

public static Integer[] DataArray = new Integer[25];

VB.NET

Dim DataArray(24) As Integer

Python

DataArray = [] #25 elements Integer

Responses

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