Programming
169 questions· page 1 of 17
A student has been asked to create a simple guessing game program. This program will generate a random integer value between 1 and 100. It will then repeatedly prompt the user to input an integer value until they input the randomly generated value.
The student has written a structured English description:
step 1 – randomly generate an integer value between 1 and 100 inclusive
step 2 – prompt the user to input an integer value
step 3 – output an appropriate message if the value input was too high; then repeat from step 2
step 4 – output an appropriate message if the value input was too low; then repeat from step 2
step 5 – output an appropriate message if the value input was the same value that was randomly generated; then end the program.
Write a pseudocode algorithm from this structured English description.
Assume no input validation is needed.
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
....................................................................................................................................................
Write pseudocode for module CustomerOrder()
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
.............................................................................................................................................
Write pseudocode for module AddNewCustomers()
Assume that there is at least one customer already in the loyalty scheme.
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
.....................................................................................................................................
The requirements for AddNewCustomers() are changed. There may be no existing customers in the loyalty scheme.
Explain the changes that will need to be made to the module AddNewCustomers()
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
.....................................................................................................................................
An algorithm is designed to generate and output two unique random integers. Each integer value is between –10 and 10 inclusive.
If both integers output are negative, a third random integer between 30 and 35 inclusive will be generated and output.
Write pseudocode for this algorithm.
A programmer has been asked to create a module RollDice() to simulate multiple rolls of a dice. This module will be used as part of a program for a game.
The module will:
step 1 – take a positive integer parameter representing the number of times the dice will be rolled
step 2 – simulate one roll of the dice by generating a random integer between 1 and 6 inclusive
step 3 – output the integer generated
step 4 – repeat, as required from step 2
step 5 – calculate the average value of the random integers generated
step 6 – return the average value.
Write pseudocode for the module RollDice()
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
....................................................................................................................................................
A module Parity() takes a string as a parameter. The parameter has the identifier BitString and it represents a binary value.
The module will concatenate a single character to the end of BitString by applying one of the two rules:
• '0' if BitString contains an even number of 1s
• '1' if BitString contains an odd number of 1s.
The modified value of BitString is then returned.
For example:
| Parameter | String returned | Explanation |
|---|---|---|
| "0010010110" | "00100101100" | there are an even number of 1s in the string passed to Parity() so a '0' is concatenated to the end of BitString |
| "101010" | "1010101" | there are an odd number of 1s in the string passed to Parity() so a '1' is concatenated to the end of BitString |
Write pseudocode for the module Parity()
Assume the parameter BitString can only contain the characters '0' and '1'
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
....................................................................................................................................................
Write pseudocode for module UpdateVisit()
Assume the customer has made at least one previous visit.
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
The programmer decides to amend the UpdateVisit() module so that loyalty points are further increased if the customer visits the coffee shop the same day of the week as their last visit.
Write the pseudocode for this condition.
...........................................................................................................................................
.....................................................................................................................................
Write pseudocode for module MondayCheck()
The module FindCustomer() must be used and assume it returns a valid string for the current customer.
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
A date is stored in LastVisitDateString in the format:
DDMMYYYY
DD is a 2-digit string
MM is a 2-digit string
YYYY is a 4-digit string.
For example, the date 03/09/2024 is stored as "03092024"
An algorithm is needed to convert the string stored in LastVisitDateString to data type DATE which is then stored in the variable LastVisitDate.
Complete the pseudocode for this algorithm:
Assume that LastVisitDateString has been declared and contains valid data.
DECLARE DayInt, MonthInt, YearInt : INTEGER
DECLARE LastVisitDate : DATE
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
A string function Compare() will compare two strings.
The function will:
- take four parameters:
- two strings,
String1andString2 - a character,
Position, to indicate whetherString1will be compared with the start ('s'), or the end ('e') ofString2 - a Boolean,
CaseMatters, to indicate whether an upper case character and lower case character (for example,'A'and'a') are regarded as different (TRUE), or as the same (FALSE)
- two strings,
- return
FALSEifString2has fewer characters thanString1 - return
TRUEif the comparison is true, otherwise returnFALSE
For example:
| Parameter | Return value | |||
|---|---|---|---|---|
| String1 | String2 | CaseMatters | Position | |
"Cat" | "Catalogue" | TRUE | 's' | TRUE |
"CAT" | "Catalogue" | TRUE | 's' | FALSE |
"CAT" | "Catalogue" | FALSE | 's' | TRUE |
"Cat" | "Catalogue" | TRUE | 'e' | FALSE |
"GUE" | "Catalogue" | FALSE | 'e' | TRUE |
"Catalogue" | "Cat" | TRUE | 's' | FALSE |
Write pseudocode for the function Compare()
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
....................................................................................................................................................
The programmer decides to use a library routine to provide part of the solution. One reason for this is that library routines may perform functions that the programmer is unable to program themselves.
State three other benefits of using library routines in the development of the program.
1 ................................................................................................................................................
2 ................................................................................................................................................
3 ................................................................................................................................................
State two reasons why the programmer may decide to use a subroutine.
1 ........................................................................................................................................
2 ........................................................................................................................................
A function header in pseudocode is defined as:
FUNCTION Pass2(Count : INTEGER) RETURNS BOOLEAN
Complete the table by describing the terms used in the function header.
The first row has been completed.
| Term | Description |
|---|---|
Pass2 | the name of the function |
Count | |
BOOLEAN |
Variables in the program have example values:
| Variable | Example value |
|---|---|
DoB | 23/6/2011 |
Multiplier | 2.5 |
AddressLine[1] | "35 Lincoln Avenue" |
Complete the table by evaluating each expression using the example values:
| Expression | Evaluates to |
|---|---|
LENGTH(NUM_TO_STR(Multiplier)) | |
MONTH(DoB) > 4 | |
15 + STR_TO_NUM(MID(AddressLine[1], 2, 1)) |
A program contains a global 1D array Data containing 20 elements of type INTEGER
A global string NumString represents a sequence of three-digit numbers, separated by commas. For example:
"101,456,219,754,328"
The string contains at least four three-digit numbers. The total number of three-digit numbers in the string is unknown.
A procedure Store() will:
- extract one three-digit number at a time from
NumString - convert each of the three-digit numbers extracted to an integer and assign this to the next array element, starting from index 1
- end when all three-digit numbers have been stored, or when the array is full.
Complete the pseudocode for Store()
All local variables used must be declared.
PROCEDURE Store()
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
ENDPROCEDURE
The following table shows four valid pseudocode assignment statements.
Complete the table by giving an appropriate data type to declare each of the variables A, B, C and D.
| Assignment statement | Data type |
|---|---|
A ← LEFT(MyName, 1) | |
B ← Total * 2 | |
C ← INT(ItemCost) / 3 | |
D ← "Odd OR Even" |
Other variables in the program have example values as shown:
| Variable | Value |
|---|---|
Sorted | False |
Tries | 9 |
ID | "ZGAC001" |
Complete the table by evaluating each expression, using the example values.
| Expression | Evaluates to |
|---|---|
Tries < 10 AND NOT Sorted | |
Tries MOD 4 | |
TO_LOWER(MID(ID, 3, 1)) | |
LENGTH(ID & "xx") >= Tries |
The choice of suitable variable names is one example of good programming practice.
Give one other example.