Skip to content
Gallery
csH2019paper
Share
Explore
section 2

icon picker
q-18

about the question

This question features some code for a search algorithm.
read the code an identify formal and actual parameters
explain variable scope and identify the scope of a variable
predict the values of a variable at a breakpoint, (by doing a trace)
There are additional notes at the end which are not part of the question but will help to develop your understanding.
local variables and scope of a variable, including code efficiency
choosing to use a function or a procedure

You can run and edit code in the replit project included on this page to explore the question and better understand the concepts

question introduction

CoasterRocks theme park is running an online competition to win a free family ticket. People visit the web page and enter their email.
After the closing date a program will sort the emails into order so that multiple entries from the same email can be identified. It will then create a list of unique emails from which to choose the winner.
The algorithm for this program is shown below.
Import the emails from the text file into the array called emails
Sort the emails array into ascending order
Store a list of unique emails in an array called uniques
Choose a random winner from the uniques array

The table below shows the contents of the emails and uniques arrays after steps 2 and 3.

emails array after step 2 | uniques array after step 3
----------------------------+----------------------------
| . . . . . . . .
| . . . . . . . .
| . . . . . . . .
. . . . . . . . |
Part of the program code is shown below.
1 DECLARE emails AS ARRAY OF STRING INITIALLY []
2 DECLARE uniques AS ARRAY OF STRING INITIALLY []

11 importEmails(emails)
12 sortEmails(emails)
13 removeDuplicates(emails, uniques)
14 chooseWinner(uniques)

70 PROCEDURE removeDuplicates(ARRAY OF STRING list, ARRAY of STRING newList)
71 DECLARE position INITIALLY 0
72 SET newList[position] TO list[0]
73 FOR index FROM 1 to length(list)-1 DO
74 IF list[index] ≠ newList[position] THEN SET position TO position + 1
75 SET newList[position] TO list[index]
76 END IF
77 END FOR
78 END PROCEDURE

part a

Identify from the code shown above
a formal parameter
an actual parameter

// state the answer here

answer parts a

formal parameter: line 70 list and newlist
line 70-78 is the body of the procedure called removeDuplicates
line 70 is the procedure header and includes the name and the formal parameter list
a formal parameter includes the name and the data type
actual parameters
line 11: emails
line 12: emails
line 13: emails, uniques
line 14: uniques
lines 11-14 are procedure calls, the actual parameter lists state what variables are to be passed to the procedure to create the Data Flow required.
actual parameters may or may not use the same names as the formal parameters but they must be the same type
the actual parameter variables are mapped or linked to the formal parameters by the position in the list: 1, 2, 3 etc.

part b i

Explain what is meant by the scope of a variable.
// place the description here

answer part b i

scope means the block of code that can access the variable
a local variable has scope for the procedure where it is declared
a global variable has scope for the whole program

part b ii

State the scope of the variable emails.
// state the scope here:

answer part b ii

emails is declared on line 1 and so is not within any procedure or function
that means its scope the the entire program, it is a global variable

part c

The data below is used to test the program.
emails array
-------------------
allan@giggle.com
bert@yeeha.com
bert@yeeha.com
bert@yeeha.com
colin@iclood.com
colin@iclood.com
A breakpoint has been set at Line 77. The variables in the table below are inspected.
Complete the table to show the values stored.
trace table
line
position
newList[position]
list[index]
index
1
70
2
71
3
4
There are no rows in this table
2
Count
trace table answer

answer part c


breakpoint at line 77
position : 1
newList[position] :
list[index] :

additional notes

procedure or function
a procedure is used here (not a function)
reading the code and the parameters we can see the data flow is
IN: list, OUT: newList
both parameters are arrays, the list array elements are not changed but they are used to change the newList elements
when an array parameter is changed by a module the dataflow is OUT because the change is fed back to the module call, no function return is needed
a function could have been used but OUT dataflow for arrays is usually automatic
procedure efficiency
there are local variables in the procedure
these variables only require memory when the procedure is called, before and after the call they use no memory
this helps make the code efficient

additional activity

The choose winner method does not work properly. Sometimes it comes up with a null winner, try it.
Can you debug and improve the code so that it chooses a winner correctly.
Notice that the number of unique emails may vary depending on the original list.

Loading…


Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.