← QCE Digital Solutions Unit 3

Cleaning data in pseudocode

A loop that checks each record with if conditions, trims and standardises text, rejects or fixes bad values, and writes only the good records to a new clean array, leaving the raw data untouched.

Understanding Cleaning data in pseudocode in QCE Digital Solutions Unit 3

What it does: The if condition detects the missing mark, and continue moves on to the next record without writing it to the clean array. A cleaning algorithm writes only the good records to a new clean array. Deleting from raw destroys the original data you may need to check later. Trimming removes the extra spaces and capitalise fixes the case, so " aVA " becomes "Ava". Copying it unchanged keeps the mess. Converting to one case standardises every spelling variant to "QLD". The wrong option only keeps records that already match and rejects the variants instead of fixing them. No mark can be both below 0 and above 100, so AND never catches anything. OR rejects a mark outside the valid range.

Common mistakes: The test is back to front. It only lets blank names through and drops the good ones. To skip records with missing values, check that name != "". Assignment goes the wrong way. The variable being set must be on the left: town <- lower(town). Otherwise the text is never standardised to lower case.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 194–250) and word search (Modules 194–337).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which loop body correctly builds a clean copy without changing the raw data?
for i from 1 to length(raw) if raw[i].age >= 0 then append(clean, raw[i])for i from 1 to length(raw) if raw[i].age < 0 then delete raw[i]
Matches
for i from 1 to length(raw) raw[i].name <- upper(raw[i].name) append clean, raw[i] next i
↓
The loop changes the raw array directly, so the original data is lost. It should store the cleaned value in a variable or in a copy of the record and write that to clean.
Applying
Meanings
Term
cleaning algorithm
Meaning
A loop that checks each record with if conditions, trims and standardises text, rejects or fixes bad values, and writes only the good records to a new clean array, leaving the raw data untouched.
Practice
clean <- []
Testing
Gaps
for i from 1 to (raw)
Check Answer
Spots
2 name <- trim(raw[i].name)
3 if name = "" then

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Desert Jerboa' badge — High Jumper, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Which loop body correctly builds a clean copy without changing the raw data?
A. for i from 1 to length(raw) if raw[i].age < 0 then delete raw[i]
B. for i from 1 to length(raw) if raw[i].age >= 0 then append(clean, raw[i]) ✓
A cleaning algorithm writes only the good records to a new clean array. Deleting from raw destroys the original data you may need to check later.
2. A student's name field holds " aVA ". Which step standardises it before it is written to clean?
A. name <- capitalise(trim(raw[i].name)) ✓
B. name <- raw[i].name
Trimming removes the extra spaces and capitalise fixes the case, so " aVA " becomes "Ava". Copying it unchanged keeps the mess.
3. How should a cleaning algorithm deal with a record that has a missing email?
A. if raw[i].email = "" then skip the record (do not append it to clean) ✓
B. if raw[i].email = "" then append it to clean anyway
Using an if condition to skip records with missing values keeps incomplete records out of the clean array. Appending them anyway defeats the purpose of cleaning.
4. Records are duplicates when they share the same key (e.g. studentId). Which is the correct check?
A. if studentId is not already a key in clean then append ✓
B. if studentId is not already a key in raw then append
Compare against the clean array, which holds the records accepted so far. Every record is already in raw, so that check would always fail and nothing would be appended.
5. True or false: a cleaning algorithm should overwrite raw[i] with the fixed values so only one copy exists.
A. True - overwriting raw saves memory and is safer
B. False - write the fixed record to a new clean array ✓
The raw data stays untouched so you can check what was changed and start again if the cleaning rules are wrong. Fixed records go into the new clean array.

Match the Pairs

name <- upper(trim(raw[i].name)) → Trims extra spaces and standardises the case of the text before it is stored
append clean, raw[i] → Writes the good record to the new clean array, leaving the raw data untouched
Skip a record with a missing value instead of copying it to the clean array → if raw[i].mark = "" then continue end if
Remove duplicates by comparing the key of the current record with keys already in the clean array → if raw[i].id = clean[j].id then isDuplicate <- true end if
for i from 1 to length(raw) raw[i].name <- upper(raw[i].name) append clean, raw[i] next i → The loop changes the raw array directly, so the original data is lost. It should store the cleaned value in a variable or in a copy of the record and write that to clean.
for i from 1 to length(raw) if raw[i].mark >= 0 then append clean, raw[i] end if next i → The check only rejects negative marks, so invalid values above the maximum (such as 150) still get through. It needs an upper limit, such as raw[i].mark >= 0 and raw[i].mark <= 100.

Key Term

cleaning algorithm
A loop that checks each record with if conditions, trims and standardises text, rejects or fixes bad values, and writes only the good records to a new clean array, leaving the raw data untouched.

Fill in the Blanks

1. for i from 1 to length(raw) name ← trim(raw[i].name) append name to clean
Hint: Type the function that returns how many records are in the raw array.
2. for i from 1 to length(raw) name ← lower(trim(raw[i].name)) append name to clean
Hint: Type the function that converts text to lowercase so spelling is standardised.
3. for i from 1 to length(raw) if raw[i].email ≠ "" then append raw[i] to clean
Hint: Type the comparison operator that skips records whose email is empty.
4. age ← raw[i].age if age >= 0 and age <= 120 then append raw[i] to clean
Hint: Type the Boolean operator that needs both range checks to be true before a record is kept.
5. if not contains(seenIDs, raw[i].id) then append raw[i].id to seenIDs append raw[i] to clean
Hint: Type the Boolean operator that reverses the check so only keys not seen before are written to the clean array.

Spot the Error

Question 1
A for i from 1 to n
B name <- trim(raw[i].name)
C if name = "" then
D k <- k + 1
E clean[k] <- name
F end if
Line C — The test is back to front. It only lets blank names through and drops the good ones. To skip records with missing values, check that name != "".
Question 2
A for i from 1 to n
B town <- trim(raw[i].town)
C if town != "" then
D lower(town) <- town
E add town to clean
F end if
Line D — Assignment goes the wrong way. The variable being set must be on the left: town <- lower(town). Otherwise the text is never standardised to lower case.
Question 3
A for i from 1 to n
B id <- raw[i].id
C if id in keys then
D add id to keys
E add raw[i] to clean
F end if
Line C — This keeps only duplicates and drops the first copy of every key. To remove duplicates, copy a record only if its key is not in keys yet.
Question 4
A for i from 1 to n + 1
B c <- upper(trim(raw[i].cd))
C if c != "" then
D add c to clean
E end if
Line A — The loop runs one step past the last record, so raw[n + 1] does not exist. A cleaning loop must stop at n: for i from 1 to n.
Question 5
A for i from 1 to n
B t <- lower(trim(raw[1].tag))
C if t != "" then
D add t to clean
E end if
Line B — This always reads the first record instead of the current one. It should use the loop counter: raw[i].tag.

Student with a class code? Join your class →