Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
29 views

Assignments On Functions

Uploaded by

ig9vsa24
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Assignments On Functions

Uploaded by

ig9vsa24
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 30

Grade: AS Paper 2 Academic year 23-24

Assignment for Functions:


Basic programs:
1. A string is a palindrome if it reads the same forwards as backwards.
The following strings are examples of palindromes:
"Racecar"
"madam"
"12344321"
Upper-case and lower-case characters need to be treated the same. For example, 'A'
is equivalent to 'a'.
(a) A function IsPalindrome() will take a string parameter. The function will return
TRUE if the
string is a palindrome and will return FALSE if the string is not a palindrome.
Write pseudocode for IsPalindrome().

2. (a) A text string contains three data items concatenated as shown:


<StockID><Description><Cost>
Item lengths are:

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]

5. A simple arithmetic expression is stored as a string in the format:


<Value1><Operator><Value2>
An operator character is one of the following: '+' '' '*' '/'
Example arithmetic expression strings:
"803+1904"
"34/7"
(a) A procedure Calculate() will:
• take an arithmetic expression string as a parameter
• evaluate the expression
• output the result.
Assume:
• the string contains only numeric digits and a single operator character
• Value1 and Value2 represent integer values
• Value1 and Value2 are unsigned (they will not be preceded by '+' or '').
(i) Write pseudocode for the procedure Calculate()
Exam questions:
1. A program is being created to convert the data capacity of a storage device into a different

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.

Complete the function calculate

function calculate(.................................................., number)

if measurement = = "gigabytes" then

value = number * 1024 * 1024 * 1024 * 8

elseif measurement = = ".................................................." then

value = number * 1024 * 1024 * 8

elseif measurement = = ".................................................." then

value = number * 1024 * 8

elseif measure = = "bytes" then

value = number * ..................................................

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.

State the values output by the program on each of the lines.

3.

Develop an algorithm, to define a function called check(data)

• initialises a variable called regValid to False

• 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:

• if the value of reg is R0 or R9 then regValid should be True


• if the value of reg is r6 or Rh then regValid should be False

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.

The algorithm must:

• ask the player for the position of their block on the board

• use the checkblock() function to check if this position is free

• 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.

6. Four separate subroutines have been written to control a robot.

• Forward(n) moves the robot n squares forward.

• TurnLeft() turns the robot 90 degrees left.

• TurnRight() turns the robot 90 degrees right.

• ObjectAhead() returns true if the robot is facing an object in the next

square or returns false if this square is empty.


1. Draw the path of the robot through the grid below if the following program is executed (the
robot starts in the square marked by the ↑ facing in the direction of the arrow).

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]

3 Figure 3 shows the subroutine convert described in Figure 2.


Some parts of the subroutine have been replaced with the labels L1 to L5.
Figure 3
State the pseudo-code that should be written in place of the labels in the subroutine
written in Figure 3. [5 marks]
L1 __________________________________
L2___________________________________
L3___________________________________
L4___________________________________
L5___________________________________

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.

Stub - Flow Diagram


The above diagram clearly states that Modules 1, 2 and 3 are available for
integration, whereas, below modules are still under development that cannot be
integrated at this point of time. Hence, Stubs are used to test the modules. The order
of Integration will be:
1,2
1,3
2,Stub 1
2,Stub 2

(d) A validation function is written to check that the passwords generated are valid.

To be valid, each password must:

• be 14 characters long

• be organised as three groups of four case-sensitive alphanumeric characters. The

groups are separated by hyphen characters

• 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]

You might also like