Pseudocode: processing a file of records for insertion

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Pseudocode: processing a file of records for insertion

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which loop header correctly reads every line of a file of student records in pseudocode?
A for each file in line
B for each line in file
2. A line reads "7, Ava ,Red". What should happen before the fields are validated?
A Split the line, then trim each field and convert the types
B Build the INSERT statement straight away
3. Which line correctly turns the text field fields[3] into a whole number?
A points <- fields[3] + "number"
B points <- INTEGER(TRIM(fields[3]))
4. When should the program build an INSERT statement for a row?
A Only when the row passes validation
B For every line, even if a field is invalid
5. Which pseudocode correctly builds the SQL text for a valid row?
A sql <- "INSERT INTO students VALUES (" + id + ", '" + name + "')"
B sql <- INSERT INTO students VALUES id, name

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1fields <- split(line, ",") Asql <- "INSERT INTO students VALUES (" + id + ", '" + name + "')"
2accepted <- accepted + 1 Bmark <- toInteger(trim(fields[2]))
3Trim spaces off a field and convert it to a number before checking it CBreaks one line of the file into a list of separate field values at each comma.
4Build the SQL text for a good row by joining the cleaned field values into an INSERT statement DThe field is still text and was not trimmed or converted to a number, so the range check compares text with numbers and may fail.
5for each line in file fields <- split(line, ",") mark <- fields[2] if mark >= 0 AND mark <= 100 then ECounts one more row that passed validation and had an INSERT statement built.
6if valid then accepted <- accepted + 1 else accepted <- accepted + 1 FThe else branch adds to accepted instead of rejected, so rejected rows are counted as accepted and the rejected total never changes.

Definitions

for each line in file

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
for each line in file fields ← _____(line, ",")
2.
name ← ____(fields[0]) age ← integer(fields[1])
3.
age ← _______(trim(fields[1]))
4.
sql ← "INSERT INTO students VALUES ('" sql ← sql _ name + "')"
5.
if valid then accepted ← accepted + 1 else ________ ← rejected + 1

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A accepted ← 0
B rejected ← 1
C for each line in file
D fields ← split(line, ",")
E accepted ← accepted + 1
Question 2
A for each line in file
B fields ← split(line, ",")
C name ← trim(line[0])
D mark ← toInteger(fields[1])
Question 3
A for each line in file
B fields ← split(line, ",")
C age ← toString(fields[1])
D if age < 0 OR age > 120 then
E rejected ← rejected + 1
Question 4
A for each line in fields
B fields ← split(line, ",")
C id ← toInteger(fields[0])
D print id
Question 5
A accepted ← 0
B for each line in file
C fields ← split(line, ",")
D if length(fields) ← 3 then
E accepted ← accepted + 1

Answer Key

Pseudocode: processing a file of records for insertion
Section A: Quick Questions
Q1
B — for each line in file
Q2
A — Split the line, then trim each field and convert the types
Q3
B — points <- INTEGER(TRIM(fields[3]))
Q4
A — Only when the row passes validation
Q5
A — sql <- "INSERT INTO students VALUES (" + id + ", '" + name + "')"
Section B: Match the Pairs
Q1
fields <- split(line, ",") → Breaks one line of the file into a list of separate field values at each comma.
Q2
accepted <- accepted + 1 → Counts one more row that passed validation and had an INSERT statement built.
Q3
Trim spaces off a field and convert it to a number before checking it → mark <- toInteger(trim(fields[2]))
Q4
Build the SQL text for a good row by joining the cleaned field values into an INSERT statement → sql <- "INSERT INTO students VALUES (" + id + ", '" + name + "')"
Q5
for each line in file fields <- split(line, ",") mark <- fields[2] if mark >= 0 AND mark <= 100 then → The field is still text and was not trimmed or converted to a number, so the range check compares text with numbers and may fail.
Q6
if valid then accepted <- accepted + 1 else accepted <- accepted + 1 → The else branch adds to accepted instead of rejected, so rejected rows are counted as accepted and the rejected total never changes.
Section C: Definitions
Q1
for each line in file — A loop that reads each line, splits it into fields, trims and validates them, then builds an INSERT statement for each good row and counts accepted and rejected rows.
Section D: Fill in the Blanks
Q1
for each line in file fields ← split(line, ",")
Q2
name ← trim(fields[0]) age ← integer(fields[1])
Q3
age ← integer(trim(fields[1]))
Q4
sql ← "INSERT INTO students VALUES ('" sql ← sql + name + "')"
Q5
if valid then accepted ← accepted + 1 else rejected ← rejected + 1
Section E: Spot the Error
Q1
B — A counter must start at 0. Starting rejected at 1 means the final count is always one too high. It should be rejected ← 0.
Q2
C — line is the whole text of the row. The separate values are in the fields array after split(), so it should be trim(fields[0]).
Q3
C — The age must be converted to a number so it can be range checked. toString() keeps it as text, so it should be toInteger(fields[1]).
Q4
A — The loop must go through every line in the file. The fields array does not exist until a line has been split inside the loop, so it should be for each line in file.
Q5
D — The ← symbol assigns a value. A condition needs the comparison operator =, so it should be if length(fields) = 3 then.