A teacher is designing a program to process pseudocode projects written by her students. Each student project is stored in a text file. The process is split into a number of stages. Each stage performs a different task and creates a new file named as shown: File name Comment MichaelAday_src.txt student project file produced by student Michael Aday MichaelAday_S1.txt file produced by stage 1 MichaelAday_S2.txt file produced by stage 2 The teacher has defined the first program module as follows: Module Description DeleteComment() • called with a parameter of type string representing a line of pseudocode from a student's project file • returns the line after removing any comments Note on comments: A comment starts with two forward slash characters and includes all the remaining characters on the line. The following example shows a string before and after the comment has been removed: Before: IF X2 > 13 THEN //check if limit exceeded After: IF X2 > 13 THEN
- (a)Complete the pseudocode for module DeleteComment(). FUNCTION DeleteComment(Line : STRING) RETURNS STRING ENDFUNCTION[8]
- (b)A second module is defined: Module Description Stage_1() • called with a parameter of type string representing a student name • creates a new stage 1 file • copies each line from the student's project file to the stage 1 file after removing any comment from each line • does not write blank lines to the stage 1 file • returns the number of lines written to the stage 1 file Write pseudocode for module Stage_1(). Module DeleteComment() must be used in your solution.[7]
Mark scheme
Marking points from the official mark scheme (May/June 2024). The mark or mark type is shown in brackets.
- [8] FUNCTION DeleteComment(Line : STRING) RETURNS STRING
- DECLARE NewLine, TwoChars : STRING
- DECLARE Count, TrimTo : INTEGER
- CONSTANT Comment = "//"
- NewLine ← Line
- TrimTo ← 0
- Count ← 1
- WHILE Count < LENGTH(Line) AND TrimTo = 0
- TwoChars ← MID(Line, Count, 2) // extract 2 chars
- IF TwoChars = Comment THEN
- TrimTo ← Count
- ENDIF
- Count ← Count + 1
- ENDWHILE
- IF TrimTo <> 0 THEN
- NewLine ← LEFT(Line, TrimTo - 1)
- ENDIF
- RETURN NewLine
- ENDFUNCTION
- Mark as follows:
- 1 Loop to length of Line (parameter) // length of Line -1
- 2 Terminate loop on first double slash
- 3 Attempt to extract one/two characters in a loop
- 4 Check for "//" after attempt at extraction in a loop
- 5 Record start position of comment // Calculate amount of trim required
- 6 Attempt to trim Line from start of comment
- 7 Completely correct trimmed Line from start of comment
- 8 Return Line after a reasonable overall attempt
- [7] FUNCTION Stage_1(StudentName : STRING) RETURNS INTEGER
- DECLARE OldFile, NewFile, Line : STRING
- DECLARE Count : INTEGER
- OldFile ← StudentName & " src.txt"
- NewFile ← StudentName & " S1.txt"
- OPENFILE OldFile FOR READ
- OPENFILE NewFile FOR WRITE
- Count ← 0
- WHILE NOT EOF(OldFile)
- READFILE OldFile, Line
- Line ← DeleteComment(Line)
- IF LENGTH(Line) <> 0 THEN
- WRITEFILE NewFile, Line
- Count ← Count + 1
- ENDIF
- ENDWHILE
- CLOSEFILE OldFile
- CLOSEFILE NewFile
- RETURN Count
- ENDFUNCTION
- Mark as follows:
- 1 Generate filenames condone missing "_"
- 2 Open both files in correct modes and subsequently close
- 3 Loop to EOF(OldFile)
- 4 Read a line from OldFile and execute DeleteComment() in a loop
- 5 Skip blank lines after DeleteComment() in a loop
- 6 Write Line to stage 1 file and increment Count
- 7 Return the number of lines after a reasonable attempt at counting
Write your answer and get it marked
On Oracle Prep you can write or photograph your own answer and have it marked by AI against this mark scheme, with feedback on what you missed.
Sign up free →