# 1D Arrays & 1-Based Indexing

- **Track:** Cambridge IGCSE™ Computer Science (0478 / 0984) (CAMBRIDGE IGCSE 0478)
- **Module:** Section 8: Programming Concepts & File Handling
- **Estimated Duration:** 10 minutes
- **Access:** Open Theory (All-Access for Interactive IDE &amp; Autotests)

---

## Theory & Conceptual Background

A **One-Dimensional (1D) Array** is a static data structure holding a fixed collection of elements of the **same data type**, referenced under a single identifier and accessed via an integer index.

---

## 1. Array Declaration in Cambridge Pseudocode

```pseudocode
DECLARE <Identifier> : ARRAY[<LowerBound>:<UpperBound>] OF <DataType>
```

### Key Declaration Rules:

- Enclose the bounds inside square brackets `[...]`.
- Separate lower and upper bounds with a colon `:`.
- Follow the declared bounds; `[1:50]` has 50 elements and `[0:49]` also has 50.
- Specify the type with the `OF` keyword (e.g. `OF INTEGER`, `OF STRING`).

---

## 2. Accessing and Manipulating Array Elements

Individual elements are read or written using square bracket index notation:

```pseudocode
DECLARE Grades : ARRAY[1:5] OF CHAR

// Direct assignment
Grades[1] <- 'A'
Grades[2] <- 'B'

// Iterative input
FOR i <- 1 TO 5
    INPUT Grades[i]
NEXT i
```

*Exam Warning*: Attempting to read or write to `Grades[0]` or `Grades[6]` triggers an **Array Index Out of Bounds** runtime error.

## Declared bounds matter

Arrays can be declared with different lower bounds. ARRAY[0:4] contains five elements just as ARRAY[1:5] does. Use the declared bounds in loops; index zero is not universally forbidden. Our examples usually start at 1 for consistency.

---

## Interactive Exercises & Autotest Specifications

### Exercise 1: Exam-Style Check: Cambridge 1D Array Declaration Syntax (Multiple choice, 25 XP)

**Instructions:**
Which statement is the correct Cambridge pseudocode declaration for a one-dimensional array storing 20 real temperature readings?

**Options:**
- **A**: DECLARE Temperatures : ARRAY[1:20] OF REAL
- **B**: DECLARE Temperatures : ARRAY(1:20) OF REAL
- **C**: DECLARE Temperatures : ARRAY[1, 20] OF REAL
- **D**: DECLARE Temperatures[20] AS REAL


**Autotest Expectations:**
- **Correct array declaration syntax chosen**: `selection_match` -> Expected: `opt_correct`

### Exercise 2: Application: Declared bounds (Fill in blank, 30 XP)

**Instructions:**
Work out each answer before submitting. How many elements are in ARRAY[0:4] OF INTEGER? Smallest valid index for that array: Largest valid index for that array:


**Starter Code:**
```text
{"prompt":"Application: Declared bounds","template":"1. How many elements are in ARRAY[0:4] OF INTEGER? [blank_1]\n2. Smallest valid index for that array: [blank_2]\n3. Largest valid index for that array: [blank_3]","blanks":[{"id":"blank_1"},{"id":"blank_2"},{"id":"blank_3"}],"word_bank":["5","0","4","6","1","-1"]}
```


### Exercise 3: Independent Practice: Sum an array (Pseudocode, 30 XP)

**Instructions:**
Read five integers into a one-dimensional array. Output their sum. Include negative values in your own test data.

Write a complete program. Read inputs in the stated order. Output only the requested results, one per line, without input prompts. Test with your own values before submitting.


**Starter Code:**
```text
// Plan your solution, then write your program.

```

**Autotest Expectations:**
- **Mixed values**: `test_case` -> Expected: `{&quot;inputs&quot;:[-2,4,0,8,-1],&quot;output&quot;:&quot;9&quot;}`
- **Last item matters**: `test_case` -> Expected: `{&quot;inputs&quot;:[0,0,0,0,17],&quot;output&quot;:&quot;17&quot;}`
- **All negative**: `test_case` -> Expected: `{&quot;inputs&quot;:[-1,-2,-3,-4,-5],&quot;output&quot;:&quot;-15&quot;}`

