Chapter 13 SB Answers
Chapter 13 SB Answers
Chapter 13 SB Answers
Activity 13A
TYPE Tday = (Monday, Tuesday, Wednesday, Thursday, Friday,
Saturday, Sunday)
DECLARE today : Tday
DECLARE yesterday : Tday
today Wednesday
yesterday today - 1
Activity 13B
TYPE TdayPointer = ^Tday
DECLARE dayPointer : TdayPointer
dayPointer ^today
Activity 13C
1 A composite data type refers to other data types in its definition. A non-composite data type
does not refer to other data types.
2 Use defined data types allow programs to be more readable for other programmers. For
example, using the days of the week as an enumerated data type.
3 a) An enumerated data type, as a list of colours can be provided with meaningful names used
for each colour in the list.
b) A record structure that contains different types of data would be used, so the data for each
house can be used together in one structure.
c) pointer data type as this will reference the address/location of the integer stored in main
memory.
Cambridge International AS & A Level Computer Science Answers
Cambridge International AS & A Level Computer Science Answers
Python
# writing to a file, reading a line of text from a file,
appending a line of text
myFile = open ("myText.txt","w")
textLn = "start"
while textLn != "":
textLn = input("Please enter a line of text ")
if textLn != "":
myFile.write(textLn + "\n")
else: myFile.close()
Cambridge International AS & A Level Computer Science Answers
VB
'writing to, reading from and appending to a text file
Imports System.IO
Module Module1
Sub Main()
Dim textLn As String
Dim objMyFileWrite As StreamWriter
Dim objMyFileAppend As StreamWriter
Dim objMyFileRead As StreamReader
Dim objMyFileReadAgain As StreamReader
Do
objMyFileWrite.WriteLine(textLn)
Console.Write("Please enter a line of text ")
textLn = Console.ReadLine()
Loop Until textLn = ""
objMyFileWrite.Close()
objMyFileReadAgain.Close()
Console.ReadLine()
End Sub
End Module
Cambridge International AS & A Level Computer Science Answers
Java
//writing to and reading from a text file
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
class WhatYouNeedToKnow13_2 {
try {
FileReader myFileReader = new FileReader("textFile.txt");
BufferedReader myBufferReader = new BufferedReader(myFileReader);
textLn = myBufferReader.readLine();
do {
System.out.println(textLn);
textLn = myBufferReader.readLine();
}
while(textLn != null);
myFileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
FileWriter myFileWriter = new FileWriter("textFile.txt", true);
PrintWriter myPrintWriter = new PrintWriter(myFileWriter);
System.out.println("Please enter a line of text ");
textLn = myObj.next();
do {
myPrintWriter.printf("%s" + "%n", textLn);
System.out.println("Please enter a line of text ");
textLn = myObj.next();
}
while (!textLn.equals("end"));
myPrintWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
FileReader myFileReader = new FileReader("textFile.txt");
Cambridge International AS & A Level Computer Science Answers
} catch (IOException e) {
e.printStackTrace();
}
}
Activity 13D
As 9354 1000 is 9 remainder is 354, with 5 locations for each record, this record would be stored at
address 2175 = 500 + 353 5 and the next four locations, assuming that the first record is stored at
address 500.
Activity 13E
1 Serial file each new record is added to the end of a file, for example a log of temperature
readings taken at a weather station.
Sequential file records are stored in a given order, usually based on the key field, for example
ascending number of employee number for a personnel file.
2 See section 13.2.1
3 a) Random access as only one record is required at a time, low hit rate.
b) Sequential access as all the records need to be accessed, high hit rate.
c) Serial access, as each record is added to the end of the file in chronological order.
Cambridge International AS & A Level Computer Science Answers
Activity 13F
a) 39/64 × 25 = 19.5
b) 41/128 × 27 = 41
c) 7/8 × 2−5 = 7/256 (0.02734375)
d) 15/64 × 2−4 = 15/1024 (0.0146484375)
e) 7/8 × 23 = 7
f) −13/16 × 22 = −3.25
g) −3/32 × 24 = −1.5
h) −5/8 × 25 = −20
i) −5/8 × 2−3 = −5/64 (−0.078125)
j) −1/4 × 2−6 = −1/256 (−0.00390625)
Activity 13G
1a) 0 0 1 0 1 1 0 0 0 0 0 0 0 1 1 1
b)
0 0 1 0 0 1 1 0 0 0 0 0 0 0 1 1
c)
0 0 0 1 0 1 1 0 1 1 1 1 1 0 1 1
d)
0 1 1 1 1 0 0 0 1 1 1 1 1 1 0 1
e)
0 1 0 1 0 1 0 0 0 0 0 0 0 1 0 1
f)
1 1 1 0 1 0 1 0 0 0 0 0 0 1 0 0
g)
1 0 1 1 1 0 0 0 1 1 1 1 1 1 1 1
h)
1 1 0 1 1 0 0 0 0 0 0 0 0 1 0 1
i)
1 1 1 0 0 0 0 0 1 1 1 1 1 0 1 0
Cambridge International AS & A Level Computer Science Answers
..j)
1 0 1 1 0 0 0 0 1 1 1 1 1 1 1 0
Activity 13H
a) 0.1101000 × 00000011 f) 1.0000000 × 00000100
b) 0.1100000 × 00000111 g) 1.0010000 × 00001010
c) 0.1110000 × 00000010 h) 1.0110000 × 00000000
d) 0.1000100 × 00000001 i) 0.1111000 × 11110101
e) 0.1110000 × 00000110 j) 1.0000000 × 11110000
Activity 13I
1 Largest:
0 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1
Smallest magnitude:
0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0
2 Accuracy (precision) is increased by increasing size of mantissa.
Range is increased by increasing size of component.
3 a) This will cause an overflow error since the number > maximum number which can be stored.
b) underflow error will occur since division by zero generates a very small number < smallest value
which can be stored.
Cambridge International AS & A Level Computer Science Answers
Cambridge International AS & A Level Computer Science Answers
Cambridge International AS & A Level Computer Science Answers
5 a)
File organisation
File access method
method
random sequential
serial direct
sequential
b) A – serial, as meter readings are added to the end of file and stored chronologically.
B – sequential as each customer has a unique account number and the file is sorted on the
account number and there is a high hit rate.
C – random organisation allows fastest direct access to the required record, so this is
suitable for access to individual records.