Arrays, Strings And Data Handling: One-Dimensional Arrays: Declaration And Use (Copy)
One-Dimensional Arrays: Declaration And Use
What A One-Dimensional Array Is
- A one-dimensional array is a data structure used to store:
- Multiple values
- Of the same data type
- Under a single variable name
- Each value is accessed using an index
- Used when:
- Handling lists of related data
- Processing repeated inputs
- Performing batch operations (sum, count, search)
Core Properties Of One-Dimensional Arrays
| Property | Explanation |
|---|---|
| Data type | All elements must be same type |
| Index | Position used to access each element |
| Size | Fixed number of elements |
| Order | Elements stored sequentially |
| Access | Random access using index |
Array Indexing Rules (Exam-Critical)
Indexing Convention
- Common pseudocode convention:
- Index starts from 1
- Example:
marks[1]→ first elementmarks[n]→ last element
Indexing Pitfalls
- Accessing:
- Index
0when array starts at1 - Index
n + 1
- Index
- These cause:
- Out-of-range access
- Loss of marks
Declaring A One-Dimensional Array
General Declaration Format
DECLARE arrayName : ARRAY[startIndex..endIndex] OF dataType
Example: Integer Array
DECLARE marks : ARRAY[1..10] OF INTEGER
- Meaning
markscan store 10 integers- Indexed from 1 to 10
Example: Real Number Array
DECLARE temperatures : ARRAY[1..7] OF REAL
Declaring Arrays Of Different Data Types
| Data Type | Example Declaration |
|---|---|
| INTEGER | DECLARE scores : ARRAY[1..5] OF INTEGER |
| REAL | DECLARE prices : ARRAY[1..8] OF REAL |
| STRING | DECLARE names : ARRAY[1..20] OF STRING |
| BOOLEAN | DECLARE flags : ARRAY[1..10] OF BOOLEAN |
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions and 11 World Records For Educate A Change AS Level Computer Science Full Scale Course
Assigning Values To Array Elements
Manual Assignment
marks[1] ← 75
marks[2] ← 68
marks[3] ← 90
- Use case
- Small arrays
- Fixed test data
Assignment Using A Loop (Most Common)
FOR i ← 1 TO 10
INPUT marks[i]
NEXT i
- Examiner expectation
- Loop bounds must match array size
- Pitfall
- Looping beyond declared range
Accessing Array Elements
Output Single Element
OUTPUT marks[5]
Output All Elements
FOR i ← 1 TO 10
OUTPUT marks[i]
NEXT i
Access For Processing
total ← 0
FOR i ← 1 TO 10
total ← total + marks[i]
NEXT i
Common Operations On One-Dimensional Arrays
Operation 1: Calculating Total
total ← 0
FOR i ← 1 TO n
total ← total + arr[i]
NEXT i
Operation 2: Calculating Average
average ← total / n
- Pitfall
- Forgetting division
- Dividing by wrong value
Operation 3: Counting Based On Condition
count ← 0
FOR i ← 1 TO n
IF arr[i] >= 50 THEN
count ← count + 1
ENDIF
NEXT i
Operation 4: Finding Maximum Value
max ← arr[1]
FOR i ← 2 TO n
IF arr[i] > max THEN
max ← arr[i]
ENDIF
NEXT i
- Why safe
- Works with negative numbers
- Pitfall
- Initialising
max ← 0
- Initialising
Operation 5: Finding Minimum Value
min ← arr[1]
FOR i ← 2 TO n
IF arr[i] < min THEN
min ← arr[i]
ENDIF
NEXT i
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions and 11 World Records For Educate A Change AS Level Computer Science Full Scale Course
Searching In A One-Dimensional Array
Linear Search (Basic Search)
found ← FALSE
index ← 1
WHILE index <= n AND found = FALSE DO
IF arr[index] = target THEN
found ← TRUE
ELSE
index ← index + 1
ENDIF
ENDWHILE
- Characteristics
- Searches sequentially
- Stops early if found
- Examiner focus
- Safe bounds
- Correct flag usage
Updating Array Elements
Modify Values Based On Condition
FOR i ← 1 TO n
IF arr[i] < 0 THEN
arr[i] ← 0
ENDIF
NEXT i
Replace All Values
FOR i ← 1 TO n
arr[i] ← arr[i] * 2
NEXT i
Using Arrays With User-Defined Size
Input Size First
INPUT n
DECLARE data : ARRAY[1..n] OF INTEGER
Then Populate
FOR i ← 1 TO n
INPUT data[i]
NEXT i
- Pitfall
- Declaring array before knowing size
Passing Arrays Through Logic (Conceptual Use)
Example: Multiple Processing Passes
FOR i ← 1 TO n
INPUT arr[i]
NEXT i
FOR i ← 1 TO n
OUTPUT arr[i]
NEXT i
- Common exam pattern
- Input loop
- Processing loop
- Output loop
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions and 11 World Records For Educate A Change AS Level Computer Science Full Scale Course
Common Logic Errors With One-Dimensional Arrays
Error 1: Index Out Of Range
FOR i ← 1 TO n + 1
OUTPUT arr[i]
NEXT i
- Fix
FOR i ← 1 TO n
Error 2: Wrong Index Variable
arr[i] ← value
- But loop uses
j - Leads to undefined behaviour
Error 3: Using Uninitialised Array Elements
- Accessing elements before assigning values
- Examiner treats as undefined
Error 4: Mixing Data Types
DECLARE ages : ARRAY[1..10] OF INTEGER
ages[1] ← "Ten"
- Invalid
- Data type mismatch
Dry-Run Technique For Array-Based Code
Dry-Run Checklist
- Write array indices horizontally
- Fill values as assigned
- Track changes after each iteration
- Verify bounds at every access
High-Frequency Exam Patterns Using Arrays
| Task | Technique |
|---|---|
| Store multiple inputs | FOR + array |
| Calculate total | Accumulator |
| Count matches | Counter |
| Search | WHILE + flag |
| Modify values | FOR + IF |
| Output all | FOR loop |
Safe One-Dimensional Array Template (Copy-Paste)
INPUT n
DECLARE arr : ARRAY[1..n] OF INTEGER
FOR i ← 1 TO n
INPUT arr[i]
NEXT i
FOR i ← 1 TO n
OUTPUT arr[i]
NEXT i
Examiner Checklist For Full Marks
- Correct declaration syntax
- Correct index bounds
- Loop limits match array size
- All elements initialised before use
- No out-of-range access
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions and 11 World Records For Educate A Change AS Level Computer Science Full Scale Course
One-Line Rules To Memorise
- Arrays store same-type values
- Indices must stay in range
- Loop bounds must match array
- Initialise before use
- Start from index 1 unless stated otherwise
