0% found this document useful (0 votes)
89 views

Problem Solving Practice 4

Uploaded by

0171966
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
89 views

Problem Solving Practice 4

Uploaded by

0171966
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

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]

Pseudocode Selection Iteration


for i = 1 to 10
print(i)
v
next i
while score != 0
playgame()
v
endwhile
if playerHit() then
score = 0
v
endif

(b) Write pseudocode to increment the value held in the variable score by one. [1]

While score != score+1


Playgame()
endwhile

(c) State the name of each of the following computational thinking techniques.

Breaking a complex problem down into smaller problems. [1]

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.

The table identifies the three inputs to the computer system:

Input Value
A Is a student
B Has a discount card
C The current day is Saturday

(a) The logic system P = (A OR B) AND NOT C is used.

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

Describe the purpose of a truth table. [2]

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:

• input required data


• decide if the customer is entitled to a discount
• output the result of the calculation.

Design the algorithm using a flowchart [6] https://app.diagrams.net/


(c) Create the above Algorithm in either Python or Pseudocode [6]

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]

X1.05 and divide by half + tip amount

(e) Each member of staff that works in the restaurant is given a Staff ID. This is calculated
using the following algorithm.

01 surname = input("Enter surname")


02 year = input("Enter starting year")
03 staffID = surname + str(year)
04 while LENGTH(staffID) < 10
05 staffID = staffID + "x"
06 endwhile
07 print("ID " + staffID)

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

By using a different name for the variable f


By using a string rather than 5 different inputs

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

Arithmetic operator Purpose


Multiply by another number

Divide by another number

Returns remainder of a division

% or MOD

Used for integer division, does not show decimals.

// or DIV
(ii) Complete the description of programming languages and translators by writing the
correct term from the box in each space. [5]

continues crashes debugging error executable

high-level interpreter language low-level many

no one stops with without

Jack writes his program in a lowlevel language. This needs to be

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

helpful when debugging code.

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.

The executable file produced can be run with the

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]

Write an algorithm to:

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

print("your final total is", y)


print("your average is", (y/(numbers)))

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.

(a) A typical booking record is shown in the table:

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:

• firstName and surname are not empty


• room is either “basic” or “premium”
• nights is between 1 and 5 (inclusive).

If any invalid data is found “NOT ALLOWED” is displayed.


If all data is valid “ALLOWED” is displayed.

(i) Complete the following program to validate the inputs.[5]

You must use either:


• Pseudocode, or
• a high-level programming language that you have studied.

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

Erroneous / Invalid NOT 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]

You do not have to validate these parameters.

You must use either:


• Pseudocode, or
• a high-level programming language that you have studied.
(ii) Write program code, that uses newPrice(), to output the price of staying in a
Premium room for 5 nights. [3]

You must use either:


• Pseudocode, or
• a high-level programming language that you have studied.
(d) The hotel has nine rooms that are numbered from room 0 to room 8.

The number of people currently staying in each room is stored in an array with the identifier
room.

The index of room represents the room number.


Array 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)

When tested, the program is found to contain two logic errors.

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.

You must use either:


• Pseudocode, or
• a high-level programming language that you have studied.[6]

You might also like