Task 2 Complete
Task 2 Complete
Input and validation of the time the passenger would like to go up and
then come down the mountain
Calculating the total price including the group discount (if applicable)
for the journey
Updating the data for the total tickets available, total passengers
travelled and the total money earned separately for each journey
Page XX
Pre-Release May/June 2021
• purchase as many tickets as they want assuming that required number of tickets is available for the
selected train (purchased for group).
• purchase even a single ticket if they are travelling alone (purchased for single passenger).
• When making a purchase, check that the number of tickets for the required train journeys up and
down the mountain is available.
•
i) Code for journey up:
To check if the tickets for a required journey are available, we will first take input of the journey the
passenger would like to go up the mountain and store it in the variable time_up using statements like:
Therefore, we will take input of the time passenger wants to go up the mountain. A WHILE loop will be
used for validation and to ensure that only train timings “0900” or “1100” or “1300” or “1500” are being
entered by the passenger. Input of any other train timings at this specific stage will output an error message
like this:
The index of the journey up selected by the passenger will be stored in a separate variable named index_up
so that it can be later used in the code for checking the availability of tickets and updating the total for
tickets, passengers and money for that specific journey.
There are two possible ways to store the index of the journey up. We will discuss and explain both the
methods. The more efficient and easier one will be implemented.
Page XX
Pre-Release May/June 2021
1) One method is to individually store the index of the journey in the specific variable using
IF…THEN…END IF selection statements:
Index time_up
1 0900
Index time_up
Table given in
2 1100
explanation
of algorithm
Index time_up of TASK 1
3 1300
Index time_up
4 1500
This method may seem easier to understand but it will consume a lot of time and space.
2) Second method is to store the index of the journey in the specific variable using FOR...TO…NEXT
loop:
Unlike the previous method, this code will not be used to check every condition separately and instead we
will give this loop a count of 1 to 4 so that it runs for all the 4 journeys going up the mountain.
Now this may seem confusing at first but after understanding the running example of this code and the
process that is being carried out, it will seem the most efficient and time saving method.
Page XX
Pre-Release May/June 2021
Page XX
Pre-Release May/June 2021
Once the index of journey is stored, the loop will increment again. Understanding and keenly observing the
process above will clear your mind on how the index is stored for only one journey out of the four journeys.
If the passenger had given the time_up as 1500 then the loop would have incremented 3 times. When the
count would have become 4, it will give the value 1500 and as it matches to passengers input, 4 would have
been stored in the variable index_up.
If both timings do not match then the loop will keep incrementing till count becomes 4. Ultimately out of 4
journeys up, timings of any one journey will match and its index will be stored which will be later used at
different places in code.
The second method is more efficient and time saving so we will be using that approach, piece of
code and logic in our Pseudocode for TASK 2.
Therefore, we will take input of the time passenger wants to go down the mountain. A WHILE loop will be
used for validation and to ensure that only:
• train timings “1000” or “1200” or “1400” or “1400” are being entered by the passenger.
• train timing which is greater than the timing selected for journey up is being entered by the
passenger (because it is not humanly possible that time for going up is 1300 and time for coming
down is somehow 1000 which is 3 hours less).
Input of any other train timings at this specific stage will output an error message.
The index of the journey down selected by the passenger will be stored in a separate variable named
index_down so that it can be later used in the code for checking the availability of tickets and updating the
total for tickets, passengers and money for that specific journey.
Similarly, there are two possible ways to store the index of the journey down. The second method which is
explained above with running of example code will be used.
Suppose if the passenger had given the time_down as 1400 then the loop would have incremented 2
times. When the count would have become 3, it will give the value 1400 and as it matches to passengers
input, 3 would have been stored in the variable index_down.
If both timings do not match then the loop will keep incrementing till count becomes 4. Ultimately out of 4
journeys down, timings of any one journey will match and its index will be stored which will be later used at
different places in code.
The whole process remains same as the one explained for journey up. The only changes are to use
different variable names and timings (i.e. 1000, 1200 etc.).
Page XX
Pre-Release May/June 2021
• Check that the number of tickets for the required train journeys up and down the mountain is
available.
•
Now we will display the number of tickets available for the journeys selected by passenger for going up and
down separately.
Now you will realize the use of values stored in index_up and index_down. If the passenger had chosen
train going up at 0900 and train coming down at 1400 then:
• 0900 is stored at index = 1 in array time_up[1:4] so index_up would be 1 therefore tickets available
for that train will be output:
tickets_up[index_up]
tickets_up[1]
number of available tickets stored in array tickets_up[1:4] for that specific journey will be
displayed
tickets_down[index_down]
tickets_down[3]
number of available tickets stored in array tickets_down[1:4] for that specific journey will be
displayed
Then we will simply take input of number of tickets the passenger wants to purchase.
A WHILE loop will be used for validation and to ensure that only available number of tickets are being
entered by the passenger. Input of any other number e.g. less than zero (=<0) OR greater (>) than
available number of tickets for selected journeys will output an error message like this:
Page XX
Pre-Release May/June 2021
• If the tickets are available, calculate the total price including any group discount.
If the required number of tickets were available, then the passenger would have passed the validation check
demonstrated above and the input of number of tickets would have been stored in the variable
num_tickets.
For calculation of the total price and group discount, we’ll make use of additional pieces of information
given in the pre-release as well. This includes the following details:
• The cost is $25 for the journey up and $25 for the journey down.
• Groups of between ten and eighty passengers inclusive get a free ticket for every tenth
passenger, provided they all travel together (every tenth passenger travels free).
The ticket cost of one journey is $25 and it will be stored in a constant named ticket_cost.
The total ticket cost for a passenger or a group of passengers for 1 journey will be stored in a variable
named one_way_cost. This means that the value stored in the variable would be used in calculation of total
money for both journeys as the ticket price is same.
To simplify, if one_way_cost is assumingly $800 then it will be stored in total money for journey up and
total money for journey down separately as both have same ticket price ($25) and therefore cost equally.
Now we will use IF…THEN…END IF selection statement to determine how many passengers are travelling
and accordingly calculate the group discount.
Remember:
If 10 passengers are travelling then they would buy 10 tickets. If 200 passengers are travelling then they
would obviously buy 200 tickets.
Therefore if we are checking number of tickets using IF condition then it ultimately means that the
number of passengers are being checked.
Page XX
Pre-Release May/June 2021
If the tickets purchased are more than 80 then 8 passengers will travel free because every tenth passenger
travels free and 80/10 = 8 tickets.
So 8 x 25 (ticket cost) = $200. This means that $200 will be deducted from the total price of all tickets
purchased.
Example running:
(i) 80 passengers are travelling:
num_tickets = 80 so,
• discount 8 x 25 = $200
• one_way_cost (25 x 80) – 200
• one_way_cost 2000 – 200
• one_way_cost $1800
• discount 8 x 25 = $200
• one_way_cost (25 x 120) – 200
• one_way_cost 3000 – 200
• one_way_cost $2800
Page XX
Pre-Release May/June 2021
If the tickets purchased are more than 10 (which means less than 80 because if they were greater than 80
then they would have been checked in the previous portion of the condition).
So the number of tenth passengers travelling (between 10 and 80 inclusive) will travel free because every
tenth passenger travels free.
So to check how many passengers were tenth, we will use the DIV function. DIV gives us the integer value
which is quotient. (Do not confuse it with MOD function which actually gives us the remainder).
Therefore, the number of tickets purchased will be divided by 10 (because every tenth passenger travels
free) and DIV value will be returned which is integer quotient.
• So suppose 60 passengers were travelling so 60 DIV 10 = 6. This means there were 6 tenth
passengers.
• If passengers were 75 so 75 DIV 10 = 7 (not 7.5 because DIV only returns an integer). This means
there were 7 tenth passengers.
• If the passengers were 39 so 39 DIV 10 = 3 (not 3.9 because DIV only returns an integer). This
means there were 3 tenth passengers.
So the tenth passengers calculated will be deducted from the total number of passengers. This is the
number of passengers that will be charged. The result of deduction will be multiplied by $25 (ticket cost).
Example running:
(i) 10 passengers are travelling:
num_tickets = 10 so,
Page XX
Pre-Release May/June 2021
If the tickets purchased are less than 10 and 80 (since both previous conditions failed) then it will simply
calculate the cost by multiplying 25 (ticket cost) with the number of tickets purchased.
This is done because if the passengers travelling are less than 10 then all of them will be charged as there
will be no tenth passenger left.
Example running:
(i) 9 passengers are travelling:
• one_way_cost 25 x 9
• one_way_cost $225
• one_way_cost 25 x 4
• one_way_cost $100
• Update the screen display and the data for the totals.
To update the data for the totals, we must know that the following things are being totalled:
So the piece of code used for totalling number of tickets available is:
The number of tickets bought by passenger for journey up and journey down is subtracted for both
journeys separately from the total available tickets for each of them.
Page XX
Pre-Release May/June 2021
Then the piece of code used for totalling number of passengers travelling is:
The number of passengers = number of tickets purchased therefore the number of passengers
travelling for journey up and journey down is added for both journeys separately to the total number of
passengers for each of them.
At last the piece of code used for totalling amount of money taken is:
The amount of money taken for journey up and journey down is added for both journeys separately to the
total amount of money taken for each of them.
Example running:
(i) 140 tickets are purchased, time selected for journey up is 1100 and time selected for journey
down is 1600:
3 1400 480
4 1600 640
Page XX
Pre-Release May/June 2021
• num_tickets = 140
• index_up 2 We are assuming that code is
• index_down 4
being run for the first time and
• tickets_up[index_up] 480
the passengers travelled and
• tickets_down[index_down] 640
money earned in the start is 0.
• passengers_up[index_up] 0
• passengers_down[index_down] 0
• money_up[index_down] 0
• money_down[index_down] 0
tickets_up[2] 340
tickets_down[4] 500
---------------------------------------------------------------------------------------------------------
passengers_up[2] 0 + 140
passengers_down[4] 0 + 140
passengers_up[2] 140
passengers_down[4] 140
---------------------------------------------------------------------------------------------------------
money_up[2] 0 + 3300
money_up[4] 0 + 3300
money_up[2] 3300
money_up[4] 3300
Page XX
Pre-Release May/June 2021
(ii) Another 65 tickets are purchased for the same journeys going up and down:
• num_tickets = 65
• index_up 2 We are assuming that code is
• index_down 4
being run for the second time
• tickets_up[index_up] 340
and the tickets available,
• tickets_down[index_down] 500
passengers travelled and money
• passengers_up[index_up] 140
• passengers_down[index_down] 140 earned is taken from the results
• money_up[index_down] 3300 of the previous running of code.
• money_down[index_down] 3300
tickets_up[2] 340 – 65
tickets_down[4] 500 – 65
tickets_up[2] 275
tickets_down[4] 435
---------------------------------------------------------------------------------------------------------
passengers_up[2] passengers_up[2] + num_tickets
passengers_down[4] passengers_down[4] + num_tickets
passengers_up[2] 140 + 65
passengers_down[4] 140 + 65
passengers_up[2] 205
passengers_down[4] 205
---------------------------------------------------------------------------------------------------------
money_up[2] money_up[2] + one_way_cost
25 x (65 – (65 DIV 10))
money_up[4] money_up[4] + one_way_cost
25 x (65 – 6) = 1475
money_up[2] 3300 + 1475
money_up[4] 3300 + 1475
money_up[2] 4775
money_up[4] 4775
Page XX
Pre-Release May/June 2021
For updating the screen display we’ll make use of additional pieces of information given in the pre-release
as well. This includes the following details:
• When a train is full, the word ‘Closed’ is displayed instead of the number of tickets available.
The display consisted of the train timings and tickets available. The train timings do not need to be updated
as they remain same.
The number of tickets available will be updated automatically. The only condition we need to check here is
that if tickets are 0 then ‘Closed’ must be displayed. The whole code of TASK 1 will be used but with a little
change.
IF statement is used to check that if the tickets available for any journey up becomes 0 then we will remove
the OUTPUT of tickets_up[count] and instead simply display the word ‘Closed’.
The exact same condition will be used to check if tickets available for any journey down becomes 0 but only
different variables of journey down will be used.
Page XX
Pre-Release May/June 2021
TASK 2 – Pseudocode:
BEGIN
CONST ticket_cost 25.00 AS FLOAT
DECLARE num_tickets 0 AS INTEGER
DECLARE time_up 0, time_down 0 AS INTEGER
DECLARE count 0, index_up 0, index_down 0 AS INTEGER
DECLARE one_way_cost 0.0, discount 0.0 AS FLOAT
FOR count 1 TO 4
IF time_up = time_up[count] THEN
index_up count
END IF
NEXT count
FOR count 1 TO 4
IF time_down = time_down[count] THEN
index_down count
END IF
NEXT count
PRINT “How many tickets would you like to buy? Every tenth ticket is free for groups of between ten and
eighty passengers inclusive.”
PRINT “The tickets available for the train going up is: “, tickets_up[index_up]
PRINT “The tickets available for the train going back down is: “, tickets_down[index_down]
INPUT num_tickets
WHILE num_tickets <= 0 OR num_tickets > tickets_up[index_up] OR num_tickets >
tickets_down[index_down]
Page XX
Pre-Release May/June 2021
INPUT “There are not enough tickets available. Please choose any available number of tickets as
displayed above”, num_tickets
END WHILE
ELSE
one_way_cost ticket_cost * num_tickets
END IF
PRINT “The train departure times and tickets available for four journeys going up are:”
FOR count 1 TO 4
IF tickets_up[count] = 0 THEN
PRINT “Time: “, time_up[count], “Tickets available: Closed”
ELSE
PRINT “Time: ”, time_up[count], “Tickets available: ”, tickets_up[count]
END IF
NEXT count
PRINT “The train departure times and tickets available for four journeys coming down are:”
FOR count 1 TO 4
IF tickets_down[count] = 0 THEN
PRINT “Time: “, time_down[count], “Tickets available: Closed”
ELSE
PRINT “Time: ”, time_down[count], “Tickets available: ”, tickets_down[count]
END IF
NEXT count
END
Page XX
Pre-Release May/June 2021
TASK 2 – Efficiency:
Page XX
Pre-Release May/June 2021
Page XX
Pre-Release May/June 2021
Page XX
Pre-Release May/June 2021
Page XX