A level Computer science Pre Release Material Paper 2 solution 2017

4

What is pre release material?

Pre release material is sent by CIE to studnets who are currently studying O level Computer science or A level Computer science, the pre release material contains programming related Tasks to prepare students for Cambridge international examinations, for O level students the pre release material is sent only for Computer science Paper 2 and for A level students the pre release is sent for Computer science paper 2 and paper 4.

 

Task 1 – Stages of simple Alogrithm

 

(a) Fundamental algorithm stages:input, process and Output.

 

 Task 1.1

Describe a range of activities in terms of these three stages.

Draw examples from different areas such as:

  • Manufacturing
  • College/School
  • Data Processing Systems

 

Solution

 

InputProcessOutput
ManufacturingRaw material,labourAssembly, PackagingFinished goods
College/SchoolstudentTutoringEnhancements in skills and knowledge of students
Data processing SystemsRaw dataStatistical AnalysisIdentification of relationships in data, model of data

 

Task 1.2

Give examples of programming statements that illustrate each of the three stages.

Compare similar statements in Pseudocode and High-level Languages.

 

Solution

 

PseudocodeVB.NET
InputInput AA = Console.Readline
ProcessA <-- A+1A = A+1
OutputOUTPUT AConsole.Writeline(A)

 

(b) Appreciate the use of  logic Statements within an algorithm.

 

Task 1.3

 

Given the following Pseudocode assignments:

FlagA True

FlagB  False

FlagC True

MyNum  27

 

Evaluate the Following expressions:

 

ExpressionEvaluates to
FlagA AND FlagB
FlagA AND FlagC
FlagB AND FlagC
FlagA AND (FlagB OR FlagC)
FlagA AND (NOT FlagB)
(MyNum > 27) AND FlagC
(MyNum => 27) AND (FlagC = FALSE)

 

Solution

 

ExpressionEvaluates to
FlagA AND FlagBFALSE
FlagA AND FlagCFALSE
FlagB AND FlagCTRUE
FlagA AND (FlagB OR FlagC)TRUE
FlagA AND (NOT FlagB)FALSE
(MyNum > 27) AND FlagCFALSE
(MyNum => 27) AND (FlagC = FALSE)FALSE

 

Task 2 – Programming basics: Loops

This is Pseudocode for a simple count-controlled loop:

For Count  1 to 100
     Output count
EndFOR

 

Task 2.1

Re-write this Pseudocode to provide the same functionality using:

  1. A post-condition loop
  2. A pre-condition loop

Solution Task 2.1

1.Post-Condition Loop

 

     DECLARE count : INTEGER
     count ← 1 REPEAT OUTPUT count
     count ← count +1
     UNTIL count > 100
2.Pre-Condition Loop

 

     DECLARE count : INTEGER 
     count ← 0 
     WHILE count < 100 
     count ← count + 1 
     OUTPUT count 
     ENDWHILE

 

Task 2.2

Modify the solutions from Task 2.1 to:

  1. Prompt for the input of start and end value.
  2. Output all the numbers between the start and end values that are exactly divisible by 3.

 

Solution Task 2.2

 

1.Post-Condition Loop

 

DECLARE count : INTEGER 
DECLARE start : INTEGER 
DECLARE end : INTEGER 
OUTPUT “Enter starting value” 
INPUT start
 OUTPUT “Enter final value”
 INPUT end 
count ← start + 1 
REPEAT 
IF MOD(count, 3) = 0 
THEN 
OUTPUT count 
 ENDIF count ← count +1 
UNTIL count > end - 1
2. ​Pre-Condition Loop

 

DECLARE count : INTEGER 
DECLARE start : INTEGER 
DECLARE end : INTEGER 
OUTPUT “Enter starting value” 
INPUT start 
OUTPUT “Enter final value” 
INPUT end 
count ← start 
WHILE  count < end - 1 
count ← count + 1 
IF MOD(count, 3) = 0 
THEN 
OUTPUT count
 ENDIF 
ENDWHILE

Task 3 – program design and coding

A string exists in CamelCase format, for example: ‘ThisIsACamelCaseString’

A procedure is required that will:

  • prompt for the original string
  • separate each word of the string
  • store each word in separate array element
  • fill unused array elements with a rogue string such as “(Empty)”.

After processing the preceding example, the array content will look like this:

This

Is

A

Camel

Case

String

(Empty)

(Empty)

(Empty)

Task 3.1

Use pseudocode to declare the array that can be used to store the separate words of the original string.

You can assume that the original string will contain no more than 10 separate words.

 

Solution Task 3.1

DECLARE Word : ARRAY[1:10] OF STRING

 

Task 3.2

Express the requirements using Structured English.

Solution Task 3.2

Identifier table:
Variable identifierData TypeDescription
countintegerCounter variable for repeat loopcorresponds with the position of the letter being accessed in 'camelstring'
indexintegerindex for array ‘word()’
camelsttringstringString in Camel Case format
character codeintegerAscii value of a letter in
‘camelstring’
word()Array containing 10 string
values
Will store each word in ‘camelstring’
as a separate element

 

SET count to 1
 SET index to 0 
OUTPUT "enter camelcase string" 
INPUT camelstring 
REPEAT 
IF count is less than the length of camelstring THEN 
set charactercode to ascii value of (count)-th letter in camelstring 
IF charactercode is between 65 and 90 THEN 
increment index 
ENDIF
 Add (count)-th letter in camelstring to word(index) 
increment count 
ELSE 
increment index 
set word(index) to “(empty)” 
ENDIF
UNTIL index is equal to 10

Task 3.3

Write pseudocode for this design.

Solution Task 3.3

DECLARE Word : ARRAY[1:10] OF STRING 
DECLARE Charactercode : String 
DECLARE CamelString : STRING 
DECLARE Count : INTEGER 
Count ← 1
 DECLARE Index : INTEGER
 Index ← 0 
OUTPUT "Enter CamelCase String" 
INPUT CamelString 
REPEAT 
IF Count <= LENGTH(CamelString) 
THEN 
Charactercode ← ASC(MID(CamelString, Count, 1))
 IF Charactercode <= 90 AND Charactercode >= 65 
THEN 
Index ← Index + 1 
ENDIF 
Word[Index] ← Word[Index] & CHR(Charactercode) 
Count ← Count + 1 
ELSE 
Index ← Index + 1 
Word[Index] ← "(empty)" 
ENDIF
UNTIL Index = 10 

 

Task 3.4

Write program code for this design.

Solution Task 3.4

[vb]

Sub Main()
Dim charactercode As String
Dim word(9) As String
Dim count As Integer = 1
Dim index As Integer = -1
Dim CamelString As String
Console.WriteLine("Enter CamelCase String")
CamelString = Console.ReadLine()
Do
If count &amp;amp;amp;amp;amp;amp;amp;lt;= Len(CamelString) Then
charactercode = Asc(Mid(CamelString, Count, 1))
If charactercode &amp;amp;amp;amp;amp;amp;amp;lt;= 90 And charactercode &amp;amp;amp;amp;amp;amp;amp;gt;= 65 Then
index = index + 1
End If
word(index) = word(index) &amp;amp;amp;amp;amp;amp;amp;amp; Chr(charactercode)
count = count + 1
Else
index = index + 1
word(index) = "(empty)"
End If
Loop Until index = 9
End Sub

[/vb]

Task 3.5

Amend your program code to print out the contents of the array in a format that is easy to understand.

Solution Task 3.5

[vb]

Sub Main()
Dim charactercode As String
Dim word(9) As String
Dim count As Integer = 1
Dim index As Integer = -1
Dim CamelString As String
Console.WriteLine("Enter CamelCase String")
CamelString = Console.ReadLine()
Do
If count &amp;amp;amp;amp;amp;amp;amp;amp;lt;= Len(CamelString) Then
charactercode = Asc(Mid(CamelString, Count, 1))
If charactercode &amp;amp;amp;amp;amp;amp;amp;amp;lt;= 90 And charactercode &amp;amp;amp;amp;amp;amp;amp;amp;gt;= 65 Then
index = index + 1
End If
word(index) = word(index) &amp;amp;amp;amp;amp;amp;amp;amp;amp; Chr(charactercode)
count = count + 1
Else
index = index + 1
word(index) = "(empty)"
End If
Loop Until index = 9
Console.writline() //’ammendments to the program from here:
For a As integer = 0 to 9
Console.Write(word(a) &amp;amp;amp;amp;amp;amp; " ")
Next Console.ReadLine()
End Sub

[/vb]

Task 3.6

Design Suitable test data to test the code thoroughly. Justify your choice.

Solution Task 3.6

Invalid data:  “computer”

Output:          IndexOutOfRangeException

Valid data:

1. “AStringInCamelCaseFormat”

 2. “COMPUTER”

3. “SEQUENTIAL”

Output:

1. A String In Camel Case Format (empty) (empty) (empty) (empty)

2. C O M P U T E R (empty) (empty)

3. S E Q U E N T I A L

 

Task 4 – File Handling

The Computer club at sports club maintains a log of attendances of each member .Each time a member visits the club, an entry is added to the log file as follows:

  • Membership number ( 6 Characters, for example “123456”)
  • Date of Visit ( 8 Characters , for example “28/07/15”)

The system Concatenates these data items and adds them as a single line to the file.

Solution Task 4

DECLARE membernum : STRING 
DECLARE visitdate : STRING 
OPENFILE "VisitLog.txt" FOR WRITE 
OUTPUT "Enter "endlog" when prompted for Membership Number to terminate program" 
REPEAT
 OUTPUT "Enter Membership Number" 
INPUT membernum 
OUTPUT "Enter date of visit" 
INPUT visitdate 
IF membernum <> "endlog" 
THEN 
FILEWRITE "VisitLog.txt" , membernum & visitdate 
ENDIF 
UNTIL membernum = "endlog" 
CLOSEFILE "VisitLog.txt" 

Task 4.1 

Write Pseudocode to create a text file containig log data for a number of attendances by sports club members. The user will input the two strings described above and these will be concatenated and added to the file.

The process will repeat until a rogu value is input.

Solution Task 4.1

DECLARE membernum : STRING 
DECLARE visitdate : STRING 
OPENFILE "VisitLog.txt" FOR WRITE 
OUTPUT "Enter "endlog" when prompted for Membership Number to terminate program" 
REPEAT
 OUTPUT "Enter Membership Number" 
INPUT membernum 
OUTPUT "Enter date of visit" 
INPUT visitdate 
IF membernum <> "endlog" 
THEN 
FILEWRITE "VisitLog.txt" , membernum & visitdate 
ENDIF 
UNTIL membernum = "endlog" 
CLOSEFILE "VisitLog.txt"

Task 4.2

Write pseudocode to append a new data line to the existing file.

 

Solution Task 4.2

OUTPUT "Enter Membership Number" 
INPUT membernum 
OUTPUT "Enter date of visit" 
INPUT visitdate 
OPENFILE "VisitLog.txt" FOR APPEND 
FILEWRITE "VisitLog.txt" , membernum & visitdate 
CLOSEFILE "VisitLog.txt"

Task 4.3

Write pseudocode to output a list of visits made by a member. Prompt the user to input the membership number and output the date of each visit by that member. If the membership number is not found, output a suitable message.

Solution Task 4.3

DECLARE Entry : STRING 
DECLARE Flag : BOOLEAN Flag2 ← TRUE 
OUTPUT "Enter Membership Number" 
INPUT membernum 
OPENFILE "VisitLog.txt" FOR READ 
WHILE NOT EOF("VisitLog.txt") 
READFILE "VisitLog.txt", Entry 
     IF LEFT(Entry, 6) = membernum 
THEN
 OUTPUT RIGHT(Entry, 8) 
Flag ← FALSE 
ENDIF 
ENDWHILE 
CLOSEFILE 
IF Flag = TRUE 
THEN 
OUTPUT "Membership number not found" ENDIF

Task 4.4

Write code for Task 4.1 to Task 4.3

Solution Task 4.4

[vb]

Imports System.IO
Module Module1
Dim membernum As String
Dim visitdate As String
Dim path As String
Sub Main()
Dim a As Integer = 0
Console.WriteLine("Enter path of file") ‘ “C:\VisitLog.txt” can be a possible path
path = Console.ReadLine ‘ delete the file once you’re done
MakeVisitLog()
Console.WriteLine()
Appendlog()
Console.WriteLine()
SearchMember()
Console.ReadLine()
End Sub
Sub MakeVisitLog()
Dim Filewriter As New StreamWriter(path, False)
Console.WriteLine("Enter ""end"" when prompted for Membership Number to terminate
program”)
Do
Console.WriteLine()
Console.WriteLine("Enter Membership Number")
membernum = Console.ReadLine()
If membernum &lt;&gt; "end" Then
Console.WriteLine("Enter date of visit")
visitdate = Console.ReadLine()
Filewriter.WriteLine(membernum &amp; visitdate)
End If
Loop Until membernum = "end"
Filewriter.Close()
End Sub
Sub Appendlog()
Console.WriteLine("Enter Membership Number")
membernum = Console.ReadLine()
Console.WriteLine("Enter date of visit")
visitdate = Console.ReadLine()
Dim Filewriter As New StreamWriter(path, True)
Filewriter.WriteLine(membernum &amp; visitdate)
Filewriter.Close()
End Sub
Sub SearchMember()
Dim Entry As String
Dim Flag As Boolean = True
Console.WriteLine("Enter Membership Number")
membernum = Console.ReadLine()
Dim Filereader As New StreamReader(path)
Do
Entry = Filereader.ReadLine()
If Left(Entry, 6) = membernum Then
Console.WriteLine(Right(Entry, 8))
Flag = False
End If
Loop Until Filereader.EndOfStream()
Filereader.Close()
If Flag = True Then
Console.WriteLine("Membership number not found")
End If
End Sub
End Module

[/vb]

 

Task 4.5

Extend the program from Task 4.4. The program will represent a menu to allow the user to repeatedly add a new visit (4.2) or print the visits (4.3). Use a suitable input value to terminate the program.

Solution Task 4.5

[vb]

Imports System.IO
Module Module1
Dim membernum As String
Dim visitdate As String
Dim path As String
Sub Main()
Dim a As Integer = 0
Console.WriteLine("Enter path of file") ‘ “C:\VisitLog.txt” can be a possible
path
path = Console.ReadLine() ‘ delete the file once you’re done
Console.WriteLine()
MakeVisitLog()
Do
Console.WriteLine()
Console.WriteLine("Enter: ")
Console.WriteLine("1 ; for adding a new visit | 2 ; for printing the visits for a specific
member")
Console.WriteLine("3 ; terminate program")
a = Console.ReadLine()
Console.WriteLine()
If a = 1 Then
Appendlog()
ElseIf a = 2 Then
SearchMember()
End If
Loop Until a = 3
End Sub
Sub MakeVisitLog()
Dim Filewriter As New StreamWriter(path, False)
Console.WriteLine("Enter ""end"" when prompted for Membership Number to terminate
program")
Do
Console.WriteLine()
Console.WriteLine("Enter Membership Number")
membernum = Console.ReadLine()
If membernum <> "end" Then
Console.WriteLine("Enter date of visit")
visitdate = Console.ReadLine()
Filewriter.WriteLine(membernum & visitdate)
End If
Loop Until membernum = "end"
Filewriter.Close()
End Sub
Sub Appendlog()
Console.WriteLine("Enter Membership Number")
membernum = Console.ReadLine()
Console.WriteLine("Enter date of visit")
visitdate = Console.ReadLine()
Dim Filewriter As New StreamWriter(path, True)
Filewriter.WriteLine(membernum & visitdate)
Filewriter.Close()
End Sub
Sub SearchMember()
Dim Entry As String
Dim Flag As Boolean = True
Console.WriteLine("Enter Membership Number")
membernum = Console.ReadLine()
Dim Filereader As New StreamReader(path)
Do
Entry = Filereader.ReadLine()
If Left(Entry, 6) = membernum Then
Console.WriteLine(Right(Entry, 8))
Flag = False
End If
Loop Until Filereader.EndOfStream()
Filereader.Close()
If Flag = True Then
Console.WriteLine("Membership number not found")
End If
End Sub
End Module
[/vb]

 

A level Pre release explained by James waring


A Level Computer Science 9608 Pre Release Task 4 explained by james waring

4 COMMENTS

  1. Kindly let me know if ever you have solutions for AS & A level computer science October/November 2017 pre release 9608/22

LEAVE A REPLY

Please enter your comment!
Please enter your name here