Problem Solving Practice 4
Problem Solving Practice 4
1 (a) Tick (✓) one box in each row to identify whether the Pseudocode given is an example of
selection or iteration.[3]
(b) Write pseudocode to increment the value held in the variable score by one. [1]
(c) State the name of each of the following computational thinking techniques.
decomposition
Hiding or removing irrelevant details from a problem to reduce the complexity. [1]
abstraction
2 A fast food restaurant offers half-price meals if the customer is a student or has a discount
card. The offer is not valid on Saturdays.
A computer system is used to identify whether the customer can have a half-price meal.
Input Value
A Is a student
B Has a discount card
C The current day is Saturday
(i) Complete the following logic diagram for P = (A OR B) AND NOT C by drawing
one logic gate in each box. [3]
(ii) A truth table can be produced for this logic circuit.
Provides a method for mapping out all possible truth values to determine their outcomes
(iii) State how many rows (excluding any headings) would be required in a truth table for the
logic expression [1]
P = (A OR B) AND NOT C
3^3 = 9
(b) The restaurant needs an algorithm designing to help employees work out if a customer can
have a half price meal or not. There are three conditions that entitle a customer to a half
price meal:
It must NOT be Saturday.
The customer is a student OR the customer has a discount card.
The algorithm should:
PYTHON
day = input("is it saturday?")
card = input("do u have a discount card")
student = input("do u have a student card")
if day == "yes":
print("you are not elidgable for discount")
elif day == "no" and card or student == "yes":
print ("you are elidgable for discount")
else:
print("wooo")
PSEUDOCODE
STRING (DAY) = INPUT (“IS IT A SATURDAY?”)
STRING CARD = INPUT(“DO YOU HAVE A DISCOUNT CARD)
STRING (STUDENT) = INPUT(“DO U HAVE A STUDENT CARD?”)
IF day === “yes” THEN
OUTPUT (“you are not elidgable for discount”)
ELSEIF day == “no” AND card OR student == “yes” THEN
OUTPUT (“you are elidgable for discount”)
ELSE
OUTPUT(“invalid input”)
ENDIF
(d) The restaurant adds a service charge to the cost of a meal depending on the number of
people at a table. If there are more than five people 5% is added to the total cost of each
meal.
Customers can also choose to leave a tip, this is optional and the customer can choose
between a percentage of the cost, or a set amount.
Identify all the additional inputs that will be required for this change to the algorithm.[2]
(e) Each member of staff that works in the restaurant is given a Staff ID. This is calculated
using the following algorithm.
(i) Define the term casting and give the line number where casting has been used in the
algorithm. [2]
Definition: a process that converts a variables data type into another data type
Line number: 05
(ii) Complete the following trace table for the given algorithm when the surname “Kofi” and
the year 2021 are entered.
You may not need to use all rows in the table. [4]
Line
surname year staffID Output
number
01 Kofi
02 2021
03 Kofi2021
04
05 Kofi2021x
06
07 IDKofi2021x
3 Jack is writing a program to add up some numbers. His first attempt at the program is shown.
a = input("Enter a number")
b = input("Enter a number")
c = input("Enter a number")
d = input("Enter a number")
e = input("Enter a number")
f = (a + b + c + d + e)
print(f)
(a) Give two ways that the maintainability of this program could be improved.[2]
(b) Jack’s program uses the addition (+) arithmetic operator. This adds together two numbers.
(i) State the purpose of each of the arithmetic operators in the table.[4]
% or MOD
// or DIV
(ii) Complete the description of programming languages and translators by writing the
correct term from the box in each space. [5]
translated into assembly or machine code before it can be executed. This is done using
a translator.
One type of translator is an interpreter. This converts one line of code and then
executes it, before moving to the next line. It crashes when an error is
found, and when corrected continues running from the same position. This translator is
A second type of translator is a compiler. This converts all of the code and produces
an error report. The code will not run until there are multiple errors.
compiler.
(c) Jack decides to improve his program. He wants to be able to input how many numbers to
add together each time the algorithm runs, and also wants it to calculate and display the
average of these numbers. [6]
• ask the user to input the quantity of numbers they want to enter and read this value as
input
• repeatedly take a number as input, until the quantity of numbers the user input has
been entered
• calculate and output the total of these numbers
• calculate and output the average of these numbers.
PYTHON
x=0
y=0
numbers = int(input("how many numbers do u wish to input?"))
for x in range(0,numbers):
numbers2 = int(input("input a number"))
y = y + numbers2
print("your current total is", y)
PSEUDOCODE
STRING x = 0
STRING y = 0
STRING numbers = INPUT("how many numbers do u wish to input?")
FOR x IN RANGE(0, numbers):
STRING numbers2 = INPUT("input a number")
STRING y = y + numbers2
OUTPUT ("your current total is", y)
ENDFOR
PRINT("your final total is", y)
PRINT("your average is", (y / numbers))
4 Customers at a hotel can stay between 1 and 5 (inclusive) nights and can choose between a
basic room or a premium room.
firstName Amaya
surname Taylor-Ling
nights 3
room Premium
stayComplete False
(i) State the most appropriate data type for the following fields:[2]
Nights: integer
Room: string
(ii) Give the name of one field that could be stored as a Boolean data type.[1]
staycomplete
(b) When a new booking is recorded, the details are entered into a program to validate the
values. The following criteria are checked:
PYTHON
firstName = input("Enter a first name")
surname = input("Enter a surname")
room = input("Enter basic or premium")
nights = input("Enter between 1 and 5 nights") stayComplete= False
if (firstName and surname) and (room == "basic" or room == "premium") and nights >= 5:
stayComplete = true
else:
stayComplete = false
PSEUDOCODE
(ii) Complete the following test plan to check whether the number of nights is validated
correctly.[3]
Test data
Type of test Expected output
(number of nights)
2 ALLOWED
Boundary ALLOWED
(c) A Basic room costs RM60 each night. A Premium room costs RM80 each night.
(i) Create a function, newPrice(), that takes the number of nights and the type of room
as parameters, calculates and returns the price to pay.[4]
The number of people currently staying in each room is stored in an array with the identifier
room.
Index 0 1 2 3 4 5 6 7 8
Data 2 1 3 2 1 0 0 4 1
The following program counts how many people are currently staying in the hotel.
for count = 1 to 8
total = 0
total = total + room[count]
next count
print(total)
Describe how the program can be refined to remove these logic errors.
(e) The hotel car park charges RM 4 per hour. If the car is electric, this price is halved to
RM 2 per hour.
Write an algorithm to:
• take as input the number of hours the user has parked and whether their car is electric
or not
• calculate and output the total price
• repeat continually until the user enters 0 hours.