Professional Documents
Culture Documents
A1.
A practical magazine for amateur astronomers publishes data in a record structure, part
of which is shown below. Dates-visible is a pair of dates such that the named item is
visible from the first date to the second date.
Type = (planet, star, galaxy)
Planet
Name (10 characters)
Sun-distance (large real number)
Dates-visible
Magnitude (small integer)
Telescope-needed (Yes/No)
Star
Name (10 characters)
Constellation-name (10 characters)
Earth-distance (large real number)
Dates-visible
Magnitude (small integer)
Galaxy
Name (10 characters)
Earth-distance (large real number)
Dates-visible
Magnitude (small integer)
Telescope-needed (Yes/No)
Turn over]
A2.
a)
Write a declaration of this record data structure and any substructures that you
feel necessary in a language of your choice.
(10 marks)
b)
Design a program which has access to a file of these records and which requires
two pieces of input from its user, a type choice (1=planet, 2=star, 3=galaxy) and
a name. The output from the program is a printout of the information held for the
chosen item or a "not found" message if the item is not found in the file.
(20 marks)
The table below relates the musical key to numbered musical compositions by a
particular musician:
Key (+Sharps/-Flats)
4
3
2
1
0
-1
-2
-3
-4
Composition numbers
2, 19
11, 22
3, 14, 17
6, 9, 20, 23
1, 12, 15, 18, 26, 29, 32
4, 7, 21
10, 13, 24, 27
5, 16, 30
8, 25
a)
Show how to represent this table using either an array of linked lists or a two
dimensional array.
(6 marks)
b)
Which of these structures would be the best to use? Give your reasons.
(4 marks)
c)
Write a program to create the file compositions containing records each with a
key number and a composition number. The program is to request pairs of
numbers (key number and composition number) from the user with a suitable
method for the user to terminate input. [Note: error checking on the input is not
required].
(15 marks)
d)
Write a program to read from the file compositions and create the data structure
you defined in (a).
(5 marks)
A3.
program A
int mark(char* stuAns, tutAns){
int score;
if(stuAns == tutAns)
score = 0;
else
score = 1;
return(score);
}
int m = mark("Helo","hello");
a)
program B
FUNCTION mark(stuAns, tutAns AS STRING)
VAR score : INTEGER
IF stuAns EQUALS tutAns
score 0
ELSE
score 1
RETURN(score)
ENDFUNC
VAR m = mark("Helo","hello")
Choose either program A or program B and then find and copy out an example of
each of the following:
i) a reserved word
ii) a variable identifier
iii) an integer constant
iv) a type identifier
v) an assignment symbol
vi) a relational operator
Continuing with either program A or program B as in part (a), find and copy out
an example of each of the following:
i) a function call
ii) a function declaration
iii) an assignment statement
Turn over]
A4.
1
19
2
11
3
15
4
14
5
16
6
12
7
17
8
13
9
18
The function f in the code below is going to be executed with parameter p set to 2 and
parameter q set to 6. [You can choose to follow either version of the code]
a)
1
2
3
4
5
6
7
8
9
10
b)
Write down the sequence of values that are assigned to the variable y during the
function call and state the value that is returned from the call f(2,6)?
(8 marks)
Version A
int f(int p, q)
{
int x, y;
y = v[p];
for(x=p+1; x<=q; x++)
{
if( v[x]>y ) y=v[x];
}
return(y);
}
Version B
INTEGER FUNCTION f(p, q AS INTEGER);
BEGIN
INTEGER x, y;
y v[p];
FOR x FROM p+1 STEP 1 TO q DO
BEGIN
IF v[x]>y THEN y v[x]
END;
fy
ENDFUNC
c)
Decide on better names for the identifiers (the function name, its parameters and
the variables) and rewrite the code [either version A or version B] using your new
names and including suitable comments.
(10 marks)
d)
SECTION B
Answer FIVE questions out of EIGHT in Answer Book B. Each question carries 12 marks.
B5.
Write a program to accept from the keyboard a sequence of data for a car in the format
below and write this to a file called carmiles. You are to devise an appropriate
terminator for the data.
date (yyyymmdd format) money spent (decimal currency) miles driven (real)
for example
20080923 40.98 456.7
20081012 21.42 123.4
(12 marks)
B6.
Data for the chemical isotopes of iron (Fe) is shown in the following table:
Atomic number
26
26
26
26
Symb
ol
Fe
Fe
Fe
Fe
Mass
number
54
56
57
58
Abundance Pi %
isomassi
5.90
91.52
2.25
0.33
53.95
55.9527
56.95359
57.952
The relative atomic mass Amass for a chemical element is calculated from its isotope
masses isomass using the formula below where N is the number of isotopes, each
having a percentage Pi
N
Amass = isomassi * Pi / 100
i=1
B7.
a)
Show how to store the information from the table to allow a program to carry out
the relative atomic mass (Amass) calculation described above. You should avoid
storing redundant data items. Your data structure should be capable of storing
data for isotopes of other atoms such as chlorine (Cl).
(3 marks)
b)
Assuming that the data from the table had been input into a program in the style
you described in (a), complete an algorithm or program to input an atomic
number of an element and calculate and print its relative atomic mass Amass.
(9 marks)
Write a program to discover how many numbers between 1 and 40 can be expressed as
the sum of 4 positive squares (e.g. 15 = 12 + 12 + 22 + 32).
(12 marks)
Turn over]
B8.
A linked list has been set up with one data item and one pointer only in each element of
the list.
a)
Draw a diagram of this linked list with a pointer to its head. Show the pointer
movements when a member of the list is deleted.
(3 marks)
b)
c)
B9.
algorithm
iteration
GUI
code generator
(4 x 3 marks = 12 marks)
B10.
B11.
Choose either version of the program below and then find occurrences of the following 6
errors. For each error you should give the line number and an explanation of the error.
a)
b)
c)
d)
e)
f)
Version in C
void main();
int i,j;
float v[10];
{
i=1;
j=i[2];
k=i+j;
if(i>1 i=i-1;
v[0]=i;
if(v[0])j=1;
for(i=0;i<=9;i++)
v[i+1]=i;
2=i;
}
Version in Pascal
program p;
var i,j:integer;
v:array[0..9]of real;
begin
i:=1;
j:=i[2];
k:=i+j;
if i>1 i:=i-1;
v[0]:=i;
if v[0] then j:=1;
for i:=0 step 1 to 9 do
v[i+1]:=i;
2:=i;
end.
(6 x 2 marks = 12 marks)
B12.
Write brief notes to compare and contrast the following pairs of terms.
a)
b)
c)
d)
Turn over]
** END OF PAPER **