Sunteți pe pagina 1din 11

2

Teachers and candidates should read this material prior to the June 2019 examination for 9608 Paper 2.

Reminders

The syllabus states:

• there will be questions on the examination paper which do not relate to this pre-release material.
• you must choose a high-level programming language from this list:

o Visual Basic (console mode)


o Python
o Pascal / Delphi (console mode)

Note: A mark of zero will be awarded if a programming language other than those listed is used.

Questions on the examination paper may ask the candidate to write:

• structured English
• pseudocode
• program code

A program flowchart should be considered as an alternative to pseudocode for the documenting of an


algorithm design.

Candidates should be confident with:

• the presentation of an algorithm using either a program flowchart or pseudocode


• the production of a program flowchart from given pseudocode and vice versa

Some tasks may need one or more of the built-in functions or operators listed in the Appendix at the
end of this document.

There will be a similar appendix at the end of the question paper.

Declaration of variables

The syllabus document shows the syntax expected for a declaration statement in pseudocode.

DECLARE <identifier> : <data type>

If Python is the chosen language, each variable’s identifier (name) and its intended data type must be
documented using a comment statement.

Structured English – Variables

An algorithm in pseudocode uses variables, which should be declared. An algorithm in structured


English does not always use variables. In this case, the candidate needs to use the information given
in the question to complete an identifier table. The table needs to contain an identifier, data type and
description for each variable.

© UCLES 2019 9608/22/PRE/M/J/19


3

TASK 1 – Arrays

Introduction

Candidates should be able to write programs to process array data both in pseudocode and in their
chosen programming language. It is suggested that each task is planned using pseudocode before
writing it in program code.

TASK 1.1

A 1D array of STRING data type will be used to store the name of each student in a class together with
their email address as follows:

<StudentName>'#'<EmailAddress>

An example string with this format would be:

"Eric Smythe#eric@email.com"

Write program code to:

1. declare the array


2. prompt and input name and email address
3. form the string as shown
4. write the string to the next array element
5. repeat from step 2 for all students in the class
6. output each element of the array in a suitable format, together with explanatory text such as
column headings

TASK 1.2

Consider what happens when a student leaves the class and their data item is deleted from the array.

Decide on a way of identifying unused array elements and only output elements that contain student
details. Modify your program to include this.

TASK 1.3

Extend your program so that after assigning values to the array, the program will prompt the user to
input a name, and then search the array to find that name and output the corresponding email address.

TASK 1.4

Modify your program so that it will:

• prompt the user to input part, or the whole, of a name


• search the whole array to find the search term within the <StudentName> string
• for each array element in which the search term is found within the <StudentName> string, output
the element in a suitable format.

© UCLES 2019 9608/22/PRE/M/J/19 [Turn over


4

TASK 1.5

Convert your design to use a 2D array and add additional pieces of information for each student.

For example:

Array element Information Example data


MyArray[1,1] Student Name "Tim Smith"
MyArray[1,2] Email Address "TimSmith1099@email.com"
MyArray[1,3] Date of Birth "15/05/2001"
MyArray[1,4] Student ID "C3452-B"

TASK 1.6

Modify your program to work with the new structure.

© UCLES 2019 9608/22/PRE/M/J/19


Task 1.1

​'1 - Declaration of array to hold student records


​Dim​clsRecords(30) As String
​Dim​
i As Integer
​For​
i = 0 ​to​ 29
clsRecords(i) = ​"---"
​ ext
N

​'4 and 5 - Writing record to the array


​'nextElement - Acts as a pointer for accessing next blocks in the array
​'cond - Gives choice to the user whether to continue or quit
​Dim​ nextElement As Integer = 0
​Dim​ cond As String

​Do
​'2 - User Prompts
​Dim​ name As String
​Dim​ email As String

Console.WriteLine(​"Enter name: "​)


name = Console.ReadLine()

Console.WriteLine(​"Enter email: "​)


email = Console.ReadLine()

​'3 - Formatting of string as per the requirement


​Dim​ record As String = name & ​"#"​ & email

​ Record is getting written to the array


'
clsRecords(nextElement) = record
nextElement = nextElement + 1

​ User prompt for either continuing to add record or stop


'
Console.WriteLine(​"Any key to continue, S - stop: "​)
cond = Console.ReadLine()

​Loop​ Until cond = ​"S"​ ​or​ nextElement = 29

​'6 - Output
Console.WriteLine(​"Student Name#Email Address"​)
Console.WriteLine(​"--------------------------"​)
​For​ i = 0 ​to​ 29
Console.WriteLine(clsRecords(i))
​Next

1
Task 1.2

Console.WriteLine(​"Student Name#Email Address"​)


Console.WriteLine(​"--------------------------"​)
​For​ i = 0 ​to​ 29
​'Task 1.2 - To display only the current records
​If​ clsRecords(i) <> ​"---"​ ​Then
Console.WriteLine(clsRecords(i))
​End​ ​If
​Next

Task 1.3

​'Task 1.3 Searching a record and display its corresponding email

'Conditional variable that will determine whether user wants to keep


'searching or quit the loop
​Dim​ cond1 As String
​Do
'Prompt to ask name of the student
​Dim​ name1 As String
Console.WriteLine(​"Enter name: "​)
name1 = Console.ReadLine()

Console.WriteLine(​"Student Name#Email Address"​)


Console.WriteLine(​"--------------------------"​)
​For​ i = 0 ​to​ 29
​If​ clsRecords(i) <> ​"---"​ ​Then
​ 'If name in the record equals name entered, email will be
'displayed
​If​ clsRecords(i).Substring(0, name1.length) = name1 ​Then
Console.WriteLine(clsRecords(i).Substring(name1.length +1))
​End​ ​If
​End​ ​If
​Next

'Prompt whether to search for student name again or stop searching


Console.WriteLine(​"Any key to continue searching, S - Stop"​)
cond1 = Console.ReadLine()
​ oop​ Until cond1 = ​"S"
L

2
Task 1.4

​ Task 1.4 Searching a record by inputting full or part of the name and
'
'display the records

'Conditional variable that will determine whether user wants to keep


'searching or quit the loop
​Dim​ cond1 As String
​Do
​Dim​ name1 As String
Console.WriteLine(​"Enter name: "​)
name1 = Console.ReadLine()
​ 'Flag variable to indicate whether full or part of name is found
'In a record
​Dim​ flag As Boolean = ​False

Console.WriteLine(​"Student Name#Email Address"​)


Console.WriteLine(​"--------------------------"​)
​For​ i = 0 ​to​ 29
​If​ clsRecords(i) <> ​"---"​ ​Then
​'IndexOf method allows to search full name or part of the name
'Within the contagious records in the array.
'If record(s) found, flag variable is set to True.
​If​ clsRecords(i).IndexOf(name1) <> -1 ​Then
Console.WriteLine(clsRecords(i))
flag = ​True
​End​ ​If
​End​ ​If
​Next

'If no records found, flag not be set to True and will remain False.
'This will indicate no record is found in the array.
​If​ flag = ​False
Console.WriteLine(​"No Record found!"​)
​End​ ​If

'Prompt whether to search for student name again or stop searching


Console.WriteLine(​"Any key to continue searching, S - Stop"​)
cond1 = Console.ReadLine()
​ oop​ Until cond1 = ​"S"
L

3
Task 1.5 and Task 1.6 - Repeating Tasks 1.1 - 1.4 using 2D array
Task 1.1

​'1 - Declaration of 2D Arrays to hold student records


​Dim​ clsRecords(30,4) As String
​Dim​ row, col As Integer

'For accessing inner arrays or rows


​For​ row = 0 ​to​ 29
'For accessing elements by each column
​For​ col = 0 ​to​ 3
'Each empty block in the array will be represented by “---”
clsRecords(row, col) = ​"---"
​Next
​ ext
N

​'4 and 5 - Writing all of the details to the empty blocks of array
'nexetRecord - Acts as a pointer for accessing next row. Each row is responsible to
'hold details of a student
'cond - Gives choice to the user whether to continue or quit
​Dim​ nextRecord As Integer
​Dim​ cond As String

​Do
'2 - User prompts
​Dim​ name As String
​Dim​ email As String
​Dim​ dob As String
​Dim​ studentID As String

Console.Writeline(​"Enter name: "​)


name = Console.Readline()

Console.Writeline(​"Enter email: "​)


email = Console.Readline()

Console.Writeline(​"Enter date of birth: "​)


dob = Console.Readline()

Console.Writeline(​"Enter student ID: "​)


studentID = Console.Readline()

'3 - All the details of a student are getting added in each of the blocks
'of the inner array
​ Name of the student is stored in the first block of the row
'
clsRecords(nextRecord, 0) = name

4
'Email address of the student is stored in the second block
clsRecords(nextRecord, 1) = email
'Date of birth of the student is stored in the third block
clsRecords(nextRecord, 2) = dob
'StudentID of the student is stored in the fourth block
clsRecords(nextRecord, 3) = studentID
​'Pointer is updated to move to the next row
nextRecord = nextRecord + 1

'User prompt for either continuing to add record or stop


Console.Writeline(​"Any key to continue, S - stop: "​)
cond = Console.Readline()

​Loop​ Until cond = ​"S"​ ​or​ cond = ​"s" ​or ​nextRecord = 29

'6 - Output
Console.Writeline(​"Student Name#Email Address#Date of Birth#Student ID"​)
Console.Writeline(​"---------------------------------------------------"​)

For​ row = 0 ​to​ 29


​For​ col = 0 ​to​ 3
​ To display elements of a row in same line followed by a separator - “#”
'
Console.Write(clsRecords(row, col) & ​"#"​)
​Next
'Once all details of a student is displayed, we move to the next line for
'Displaying the record of another student
Console.Writeline()
​ ext
N

Task 1.2

Console.Writeline(​"Student Name#Email Address#Date of Birth#Student ID"​)


Console.Writeline(​"---------------------------------------------------"​)
​ flag variable - To allow moving to the next line only if a record is found
'
'This will prevent printing unnecessary blank lines.
Dim​ flag As Boolean = ​False

For​ row = 0 t ​ o​ 29
​For​ col = 0 ​to​ 3
​ To prevent printing empty slots in the console. Each elements or cells
'
'are checked if the slot is empty.
​If​ clsRecords(row, col) <> ​"---"​ ​Then
Console.Write(clsRecords(row, col) & " ​ #"​)
​'After record of a student is displayed, flag is set to true
flag = ​True
​End​ ​If

5
​Next

'flag = True represents the record of a student is detected and will only
'allow moving to the next line after record of a student is displayed.
'After moving to next line, flag is then again set to False so that if row of
'are not detected, flag will remain False, and we then do not move to the next
'line.
​If​ flag = ​True​ ​Then
Console.Writeline()
flag = ​False
​End​ ​If
​ ext
N

Task 1.3

​ 'Conditional variable that will determine whether user wants to keep


'searching or quit the loop
​ ​Dim​ ​cond1 As String
​Do
​'Prompt to ask name of the student
​Dim​ ​name1 As String
​ Console.Writeline(​"Enter name: "​)
​name1 = Console.Readline()

'Column headers
​Console.Writeline(​"StudentName#EmailAddress#DateofBirth#StudentID"​)
Console.Writeline(​"----------------------------------------------"​)
​Dim​ ​row1, col1 As Integer
​Dim​ ​flag1 As Boolean =​ ​False

​For​ ​row1 = 0​ ​to​ ​29


​For​ ​col1 = 0​ ​to​ ​3
​If​ ​clsRecords(row1, col1) <>​ ​"---"​ ​Then
​'First cell of the row is checked if it equals the name
'that is entered by the user.
'If True, next line is executed and email is displayed of
'the respective student of whose name was entered.
​If​ ​clsRecords(row1, 0) = name1​ ​Then
​ Console.Writeline(clsRecords(row1,1))
​flag1 = ​True
'Once email address is displayed, it is necessary to
'exit the inner loop as it will repeat itself, and then
'more than once, same email will be displayed as we
'explicitly pass number 1 to access the second column.
​exit​ ​for
​End​ ​if

6
​ nd​ ​If
E
​Next
​If​ ​flag1 =​ ​True​ ​Then
​Console.Writeline()
​flag1 = ​False
​End​ ​if
​ ext
N

​Console.Writeline(​"Any key to continue, S - stop: "​)


​ cond1 = Console.Readline()
​Loop​ ​Until cond1 = ​"S" ​or​ ​cond1​ ​= ​"s"

Task 1.4
​Dim​ cond1 As String
​Do
​ im​ name1 As String
D
Console.Writeline(​"Enter name: "​)
name1 = Console.Readline()

'Column headers
​Console.Writeline(​"StudentName#EmailAddress#DateofBirth#StudentID"​)
Console.Writeline(​"----------------------------------------------"​)
​Dim​ row1, col1 As Integer
​Dim​ flag1 As Boolean = ​False

​For​ row1 = 0 ​to​ 29


​For​ col1 = 0 ​to​ 3
​If​ clsRecords(row1, col1) <> ​"---"​ ​Then
​'If full name includes part of the name, then record(s)
'Of those students will be displayed.
​If​ clsRecords(row1, 0).IndexOf(name1) <> -1 ​Then
Console.Write(clsRecords(row1, col1) & ​"#"​)
flag1 = ​True
​'Here we don’t need to exit loop in advance as col1
'variable gets incremented at the end of the loop which
'will then allow us to access all of the elements of
'inner row(s) and therefore display details of
'student(s)
​End​ ​if
​End​ ​If
​Next
​If​ flag1 = ​True​ ​Then
Console.Writeline()
flag1 = ​False
​End​ ​if

7
​Next

Console.Writeline(​"Any key to continue, S - stop: "​)


cond1 = Console.Readline()

​Loop​ Until cond1 = ​"S"

S-ar putea să vă placă și