Data is stored as a comma-separated string: "Alice,85,A" (a) Write pseudocode for a function GetField that takes a CSV string and a field number (1, 2 or 3)...

Assessment: Computer Science (9-1) 0984 | Paper 2 Mock 01 | Algorithms, Programming and Logic Subject: Computer Science (9-1) - 0984

Question 1 Report

Data is stored as a comma-separated string: "Alice,85,A"

(a) Write pseudocode for a function GetField that takes a CSV string and a field number (1, 2 or 3) and returns that field as a string. For example, GetField("Alice,85,A", 2) should return "85". [5]

Answer Details

(a) The GetField function parses a comma-separated string by scanning for comma delimiters and tracking which field it is currently in. When it reaches the requested field number, it extracts and returns the substring from the start of that field to just before the next comma (or the end of the string for the last field). [5]

FUNCTION GetField(CSVString : STRING, FieldNum : INTEGER) RETURNS STRING
    DECLARE CurrentField : INTEGER
    DECLARE Start : INTEGER
    DECLARE i : INTEGER

    CurrentField ← 1
    Start ← 1

    FOR i ← 1 TO LENGTH(CSVString)
        IF CSVString[i] = ',' THEN
            IF CurrentField = FieldNum THEN
                RETURN SUBSTRING(CSVString, Start, i - Start)
            ENDIF
            CurrentField ← CurrentField + 1
            Start ← i + 1
        ENDIF
    NEXT i

    IF CurrentField = FieldNum THEN
        RETURN SUBSTRING(CSVString, Start, LENGTH(CSVString) - Start + 1)
    ENDIF

    RETURN ""
ENDFUNCTION

The algorithm works as follows:

  1. Tracking field boundaries: CurrentField starts at 1, and Start marks the first character of the current field. Each time a comma is found, the algorithm knows a field boundary has been reached.
  2. Detecting commas: The FOR loop scans every character. When a comma is found at position i, the text from Start to i-1 is the current field's content.
  3. Returning the correct field: If CurrentField matches FieldNum when a comma is encountered, the function returns SUBSTRING(CSVString, Start, i - Start). The second parameter of SUBSTRING is the start position, and the third is the length (i - Start characters).
  4. Handling the last field: The last field has no trailing comma, so after the loop finishes, if CurrentField equals FieldNum, the remaining text from Start to the end of the string is returned.
  5. Fallback: If FieldNum exceeds the number of fields, an empty string is returned.

For the example GetField("Alice,85,A", 2): the loop finds the first comma at position 6. CurrentField is 1, not 2, so it increments CurrentField to 2 and sets Start to 7. It finds the second comma at position 9. CurrentField is now 2, which matches FieldNum, so it returns SUBSTRING("Alice,85,A", 7, 2) which is "85".

Download The App On Google Playstore

Everything you need to excel in your exams

Green Bridge CBT Mobile App
Personalized AI Learning Chat Assistant
200,000+ Exam Questions Across IGCSE, JAMB, WAEC & NECO
Over 3,900 Lesson Notes
Offline Support - Learn Anytime, Anywhere
Green Bridge Timetable
Literature Summaries & Potential Questions
Track Your Performance & Progress
In-depth Explanations for Comprehensive Learning