0% found this document useful (0 votes)
126 views20 pages

Task 2 Complete

The document discusses the algorithm for task 2 of a train ticket purchasing program. It involves validating user input for departure and return times, checking ticket availability, calculating prices including discounts, and updating totals. Pseudocode is provided to store the indexes of the selected trips for later use in checking availability and updating data.

Uploaded by

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

Task 2 Complete

The document discusses the algorithm for task 2 of a train ticket purchasing program. It involves validating user input for departure and return times, checking ticket availability, calculating prices including discounts, and updating totals. Pseudocode is provided to store the indexes of the selected trips for later use in checking availability and updating data.

Uploaded by

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

Pre-Release May/June 2021

Concept and understanding of TASK 2:

Task 2 – Purchasing tickets.


• Tickets can be purchased for a single passenger or a group.
• When making a purchase, check that the number of tickets for the required train journeys up and
down the mountain is available.
• If the tickets are available, calculate the total price including any group discount.
• Update the screen display and the data for the totals.

Variables, constants and arrays declarations

Input and validation of the time the passenger would like to go up and
then come down the mountain

Displaying the number of tickets available for selected timings of both


trains and then taking input of the number of tickets the passenger
would like to buy

Validating the number of tickets passenger wants to purchase and then


checking the availability of required number of tickets for both trains

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

Updating the screen display and displaying “Closed” if the tickets


available for any journey becomes 0.

Page XX
Pre-Release May/June 2021

Explanation of Algorithm of TASK 2:


In this task, we have to calculate the total price including the group discounts based on number of tickets
purchased and check their availability. In the end, we will simply update the data for number of train
tickets available, total passengers travelled and total money earned.

• Tickets can be purchased for a single passenger or a group.

It simply means that the passenger can:

• 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

Running of example code:


Suppose the passenger wants to buy ticket for train going up at time 1100. He/she would have given
the input of time_up as 1100.

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.

ii) Code for journey down:


To check if the tickets for a required journey are available, we will then take input of the journey the
passenger would like to go down the mountain and store it in the variable time_down using similar
statements used above but changing the name of variable.

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

Now moving forward to the actual requirement which is to:

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

The following statement will be used:

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

• 1400 is stored at index = 3 in array time_down[1:4] so index_down would be 3  tickets available


for that train will be output:

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.

So we know that number of passengers travelling = number of tickets purchased.

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

So considering the first portion:

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

(ii) 120 passengers are travelling:

num_tickets > 80 so,

• 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

Then considering the second portion:

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,

• one_way_cost  25 x (10 – (10 DIV 10))


• one_way_cost  25 x (10 – 1)
• one_way_cost  25 x 9
• one_way_cost  $225

(ii) 59 passengers are travelling:

num_tickets > 10 so,

• one_way_cost  25 x (59 – (59 DIV 10))


• one_way_cost  25 x (59 – 5)
• one_way_cost  25 x 54
• one_way_cost  $1350

Page XX
Pre-Release May/June 2021

At last considering the third portion:

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

(ii) 4 passengers are travelling:

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

1. number of tickets available for each journey up and journey down

Arrays used: tickets_up[1:4] and tickets_down[1:4]

2. number of passengers travelling for each journey up and journey down

Arrays used: passengers_up[1:4] and passengers_down[1:4]

3. amount of money taken for each journey up and journey down

Arrays used: money_up[1:4] and money_down[1:4]

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:

Index time_up tickets_up


1 0900 480
Tables given
2 1100 480
in explanation
3 1300 480 of algorithm
4 1500 480 of TASK 1

Index time_down tickets_down


1 1000 480
2 1200 480

3 1400 480

4 1600 640

Page XX
Pre-Release May/June 2021

Therefore it can be deduced that:

• 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]  tickets_up[2] – num_tickets


tickets_down[4]  tickets_down[4] – num_tickets

tickets_up[2]  480 – 140


tickets_down[4]  640 – 140

tickets_up[2]  340
tickets_down[4]  500

---------------------------------------------------------------------------------------------------------

passengers_up[2]  passengers_up[2] + num_tickets


passengers_down[4]  passengers_down[4] + num_tickets

passengers_up[2]  0 + 140
passengers_down[4]  0 + 140

passengers_up[2]  140
passengers_down[4]  140

---------------------------------------------------------------------------------------------------------

money_up[2]  money_up[2] + one_way_cost


(25 x 140) – 200 = 3300
money_up[4]  money_up[4] + one_way_cost

money_up[2]  0 + 3300
money_up[4]  0 + 3300

Since passengers travelling are greater


than 80 money will be calculated using
the following piece of code →

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:

Therefore it can be deduced that:

• 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]  tickets_up[2] – num_tickets


tickets_down[4]  tickets_down[4] – num_tickets

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

Since passengers travelling


are greater than 10 money
will be calculated using the
following piece of code →

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.

The following is the piece of code used for journeys up:

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.

The following is the piece of code used for journeys down:

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

PRINT “What time would you like to go up the mountain?”


INPUT time_up
WHILE time_up <> 0900 OR time_up <> 1100 OR time_up <> 1300 OR time_up <> 1500
INPUT “Wrong time entered. It should be either 9, 11, 13 or 15 only”, time_up
END WHILE

FOR count  1 TO 4
IF time_up = time_up[count] THEN
index_up  count
END IF
NEXT count

PRINT “What time would you like to go down the mountain?”


INPUT time_down
WHILE time_down < time_up OR time_down <> 1000 OR time_down <> 1200 OR time_down <> 1400
OR time_down <> 1600
INPUT “Wrong time entered. It should be greater than the time chosen for going up and only be
either 10, 12, 14 or 16”, time_down
END WHILE

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

IF num_tickets >= 80 THEN


discount  8 * ticket_cost
one_way_cost  (ticket_cost * num_tickets) – discount

ELSE IF num_tickets >= 10 THEN


one_way_cost  ticket_cost * (num_tickets – (num_tickets DIV 10))

ELSE
one_way_cost  ticket_cost * num_tickets
END IF

tickets_up[index_up]  tickets_up[index_up] – num_tickets


tickets_down[index_down]  tickets_down[index_down] – num_tickets
passengers_up[index_up]  passengers_up[index_up] + num_tickets
passengers_down[index_down]  passengers_down[index_down] + num_tickets
money_up[index_up]  money_up[index_up] + one_way_cost
money_up[index_down]  money_up[index_down] + one_way_cost

PRINT “Welcome to Train Reservation System”


PRINT “The following is a table of the train times and number of train tickets available:”

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:

• Use of CONSTANT to hold fixed value of ticket cost.


• Use of WHILE loop to validate all user inputs and output appropriate error
messages when validation fails.
• Use of WHILE loop to check that the number of tickets for the required
train journeys up and down the mountain is available.
• Use of IF statement to determine indexes of both journeys.
• Use of IF statement to check the number of passengers travelling and then
accordingly calculate group discount.
• Use of IF statement to check the number of tickets still available and then
accordingly either display them or output ‘Closed’.
• Use of DIV function to calculate the number of tenth passengers.
• Use of FOR loop to output details of journeys for the screen display.

Page XX
Pre-Release May/June 2021

TASK 2 – Explanation of Pseudocode:

Page XX
Pre-Release May/June 2021

Page XX
Pre-Release May/June 2021

TASK 2 – Expected Questions:


1. State two variables you used for Task 2. State the data type and purpose of the variables.
2. Describe the data structures you have used in Task 2. Include the name(s), data type, sample data
and usage for each structure.
3. Write an algorithm for Task 2, using either Pseudocode, programming statements or a flowchart.
You should assume that Task 1 has already been completed.
4. Write an algorithm to complete Task 2 without including any error prompts, using either
Pseudocode, programming statements or a flowchart. You should assume that Task 1 has already
been completed.
5. Explain how your program completes/performs Task 2. Any programming statements used in your
answer must be fully explained.
6. Explain how you calculated the number of every tenth passenger (part of Task 2). You can include
Pseudocode or programming statements as part of your explanation.
7. Explain how you calculated the total money taken for each train journey including the group
discount. You can include Pseudocode or programming statements as part of your explanation.
8. Explain how you totalled the data for the passengers, money and number of tickets available for
each train journey. You can include Pseudocode or programming statements as part of your
explanation.
9. Explain how you calculated the total money taken for each train journey including the group
discount. You can include Pseudocode or programming statements as part of your explanation.
10. Explain how you validated any two inputs used in Task 2. State one valid and one invalid input to
test your validation methods (valid and invalid test data). You can include Pseudocode or
programming statements as part of your explanation.
11. Write an algorithm for Task 2, using either Pseudocode, programming statements or a flowchart.
Change the algorithm to ensure that groups of between thirty and two hundred passengers
inclusive get a free ticket for every thirtieth passenger. You should assume that Task 1 has already
been completed.
12. Write an algorithm for Task 2, using either Pseudocode, programming statements or a flowchart.
Change the algorithm to ensure that none of the passengers get any free tickets. You should
assume that Task 1 has already been completed.
13. Write an algorithm for Task 2, using either Pseudocode, programming statements or a flowchart.
Change the algorithm to ensure that the passengers return on the next train down the mountain
only. You should assume that Task 1 has already been completed.
14. Write an algorithm for Task 2, using either Pseudocode, programming statements or a flowchart.
Change the algorithm to ensure that groups of between ten and eighty passengers inclusive get a
discount of 50% only on tickets for every tenth passenger. You should assume that Task 1 has
already been completed.
15. Write an algorithm for Task 2, using either Pseudocode, programming statements or a flowchart.
Change the algorithm to ensure that the ticket cost is $40 for the journey up and $40 for the journey
down. You should assume that Task 1 has already been completed.
16. Comment on the efficiency of your code for Task 2.

Page XX

You might also like