Assignments On Functions
Assignments On Functions
A procedure Unpack() takes a parameter of type string and returns 3 values after
unpacking. These values will be used by the calling program after the procedure
ends.
(i) Write pseudocode for the procedure Unpack().
3. Components are weighed during manufacture. Weights are measured to the nearest
whole gram.
Components that weigh at least 3 grams more than the maximum weight, or at least 3
grams less than the minimum weight, are rejected.
A component is rechecked if it weighs within 2 grams of either the maximum or
minimum weight.
A function Status() will be called a parameter value, representing the weight of an
individual component
The value returned from the function will be as follows:
4.
The following pseudocode algorithm attempts to check whether a string is a valid
email address
(a) Part of the validation is implemented by the line:
IF NOT (Dots >= 1 AND Ats = 1 AND Others > 8) THEN
State the values that would result in the condition evaluating to TRUE.
...................................................................................................................................................
...................................................................................................................................................
............................................................................................................................................. [1]
(b) (i) Complete the trace table by dry running the function when it is called as follows: Result IsValid("Liz.
(ii) State the value returned when IsValid() is called using the expression shown in
part (b)
(i). ..............................................................................................................................
[1]
measure.
The function, calculate (), takes the measurement (e.g. gigabytes) and the number (e.g. 2) as
two parameters. It then returns the value in bits. The function returns –1 if an invalid measurement
was entered.
else
..................................................
endif
return ..................................................
endfunction [6]
2. Dru writes the following program using a high-level language.
01 function newscore(a,b)
02 temp = a*b
03 temp = temp + 1
04 return temp
05 endfunction
06 score = 18
07 name = "Dru"
08 print (score)
09 print ("name")
10 print (newscore(score,2))
11 print (score)
(a) The following table contains the program code for each line where this program outputs
values.
3.
• sets a variable called regValid to True if the string contained in the variable reg is an uppercase R
followed by the character representation of a single numeric digit.
Examples:
You may wish to use the subroutine isDigit(ch) in your answer. The subroutine isDigit returns True if
the character parameter ch is a string representation of a digit and False otherwise. [3 marks]
4. OCRBlocks is a game played on a 5 × 5 grid. Players take it in turns to place blocks on the board.
The board is stored as a two-dimensional (2D) array with the identifier game grid
Fig. shows that players A and B have placed three blocks each so far
(ii) Describe how validation could be added in to the checkblock() function to stop this error from
occurring. ........................................................................................................................................... .....
...................................................................................................................................... ...........................
................................................................................................................ .................................................
.......................................................................................... .......................................................................
.................................................................... .............................................................................................
......................................... [3]
(d) Write an algorithm to allow player A to select a position for their next block on the game
board.
• ask the player for the position of their block on the board
• if the position is free, add the letter "A" to the position chosen in the gamegrid array
• if the position is not free, repeat the above steps until a free position is chosen.
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
.............................................................................................................................................. [6]
5.
Forward(2)
TurnLeft()
Forward(1)
TurnRight()
Forward(1)
8. Bubble sort sorting:
Fill in the blank arrays to show the steps involved in applying the bubble sort algorithm to the array
[3, 5, 1, 4, 2]. You only need to show the missing steps where a change is applied to the array.
[5 marks]
9. Four subroutines are shown below.
SUBROUTINE main(k)
OUTPUT k
WHILE k > 1
IF isEven(k) = True THEN
k decrease(k)
ELSE
k increase(k)
ENDIF
OUTPUT k
ENDWHILE
ENDSUBROUTINE
SUBROUTINE decrease(n)
result n DIV 2
RETURN result
ENDSUBROUTINE
SUBROUTINE increase(n)
result (3 * n) + 1
RETURN result
ENDSUBROUTINE
SUBROUTINE isEven(n)
IF (n MOD 2) = 0 THEN
RETURN True
ELSE
RETURN False
ENDIF
ENDSUBROUTINE
Complete the table showing all of the outputs from the subroutine call main(3)
The first output has already been written in the trace table. You may not need to use all rows of the
table. [4 marks]
Describe how the developer has used the structured approach to programming in Figure given above
[2 marks]
____________________________________________________________________________
10. A developer has written a set of subroutines to control an array of lights. The lights are
indexed from zero. They are controlled using the subroutines in Table 2.
11. The following subroutines control the way that labelled blocks are placed in different
columns
12. A developer is writing a program to convert a sequence of integers that represent
playing cards to Unicode text.
The developer has identified that they need to create the subroutines shown in
Figure to complete the program.
2 State two benefits to the developer of using the three separate subroutines described in
Figure instead of writing the program without using subroutines. [2 marks]
13. The Algebraic Patent Sewing Machine is a programmable sewing machine that creates
patterns on rows of cloth. It is controlled by writing programs that use the following
subroutines:
14.
15. A chocolate factory produces bars of chocolate. A computer program controls the
process.
The weight of each bar is stored in an array, BarWeight. The array contains 100
elements,representing the weights of 100 bars that make up one shipping box.
A procedure, CheckWeight(), is required to:
1. examine each array element and count how many times the weight has exceeded
MaxWeight
2. compare the count obtained with a limit value, Threshold. Call procedure
ServiceCheck() if the count exceeds the Threshold
3. output a message if the count does not exceed the Threshold. For example:
"ShippingBox OK – maximum weight exceeded 3 times
16. A global 1D array, Contact, of type STRING is used to store a list of names and email
addresses.
There are 1000 elements in the array. Each element stores one data item. The format of
each data item is as follows:
<Name>':'<EmailAddress>
Name and EmailAddress are both variable-length strings.
For example:
"Wan Zhu:zwan99@mymail.com"
A function, Extract(), is part of the program that processes the array. A string data item is
passed to the function as a parameter. The function will return the Name part. Validation
is not necessary.
17. A program allows a user to save passwords used to login to websites. A stored
password is inserted automatically when the user logs into the corresponding website.
A student is developing a program to generate a password. The password will be of a fixed
format,
consisting of three groups of four alphanumeric characters. The groups are separated by
the
hyphen character '-'.
An example of a password is: "FxAf-3haV-Tq49"
A global 2D array Secret of type STRING stores the passwords together with the website
domain
name where they are used. Secret contains 1000 elements organised as 500 rows by 2
columns.
Unused elements contain the empty string (""). These may occur anywhere in the array.
An example of a part of the array is:
(a) Write pseudocode for module RandomChar(). You may wish to refer to the insert for
a description of the CHR() function. Other functions may also be required.
Info:
What is a Stub?
Stubs are used during Top-down integration testing, in order to simulate the
behaviour of the lower-level modules that are not yet integrated. Stubs are the
modules that act as temporary replacement for a called module and give the same
output as that of the actual product.
Stubs are also used when the software needs to interact with an external system.
(d) A validation function is written to check that the passwords generated are valid.
• be 14 characters long
• not include any duplicated characters, except for the hyphen characters.
Note: lower-case and upper-case characters are not the same. For example, 'a' is not the
same as 'A'.
Give two password strings that could be used to test different areas of the validation
rules.
Password
1 ...............................................................................................................................
Password
2 ............................................................................................................................... [2]
(e) The RandomChar() module is to be modified so that alphabetic characters are generated
twice as often as numeric characters. Describe how this might be
achieved. ......................................................................................................................................
............. ........................................................................................................................................
........... ..........................................................................................................................................
......... ............................................................................................................................................
....... ..............................................................................................................................................
..... .............................................................................................................................................
[3]