• No results found

MAY/JUNE. Computer Science 2210/22 PRE-RELEASE MATERIAL. This document covers every aspect of Pre-Release Material including detailed explanations,

N/A
N/A
Protected

Academic year: 2021

Share "MAY/JUNE. Computer Science 2210/22 PRE-RELEASE MATERIAL. This document covers every aspect of Pre-Release Material including detailed explanations,"

Copied!
52
0
0

Loading.... (view fulltext now)

Full text

(1)

MAY/JUNE

2021

This document covers every aspect of Pre-Release

Material including detailed explanations,

PRE-RELEASE MATERIAL

2210/22

Computer Science

Material including detailed explanations,

Pseudocodes along with their example

expected questions.

running, efficiencies and

(2)

Your preparation for the examination should include attempting the following practical tasks by writing

and testing a program or programs.

An electric mountain railway makes four return trips every day. In each trip the train goes up the mountain and back down. The train leaves from the foot of the mountain at 09:00, 11:00, 13:00 and 15:00. The train returns from the top of the mountain at 10:00, 12:00, 14:00 and 16:00. Each train has six coaches with eighty seats available in each coach. Passengers can only purchase a return ticket; all tickets must be purchased on the day of travel. 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). Passengers must book their return train journey, as well as the departure train journey, when they purchase their ticket. Passengers can return on the next train down the mountain or a later train. The last train from the top of the mountain has two extra coaches on it.

The train times are displayed on a large screen, together with the number of tickets still available for each train. Every time a ticket is booked the display is updated. When a train is full, the word ‘Closed’ is displayed instead of the number of tickets available.

Write and test a program or programs for the electric mountain railway.

Your program or programs must include appropriate prompts for the entry of data; data must be validated on entry.

Error messages and other output need to be set out clearly and understandably.

All arrays, variables, constants and other identifiers must have meaningful names. You will need to complete these three tasks. Each task must be fully tested.

Task 1 – Start of the day.

Write a program to set up the screen display for the start of the day. Initialise suitable data structure(s) to total passengers for each train journey and total the money taken for each train journey. Each train journey must be totalled separately. There are four journeys up and four journeys down every day.

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.

Task 3 – End of the day.

Display the number of passengers that travelled on each train journey and the total money taken for each train journey. Calculate and display the total number of passengers and the total amount of money taken for the day. Find and display the train journey with the most passengers that day.

(3)

Main Idea of Pre-Release Material:

It is based on an electric mountain railway which has 4 journeys up and 4 journeys down a mountain. The train times and tickets available are displayed on a large screen.

The tickets/seats for every journey are same except the last train to return from the top of the mountain as it has 2 extra coaches in it.

The tickets can be purchased for either 1 passenger or even group of passengers at a cost of $25 each for one way journey.

A specified range of passengers will be given a group discount and every tenth passenger will travel free in that range.

The display will be updated with each purchase and if the tickers for a certain journey are sold out then ‘Closed’ will be displayed instead of tickets on display screen.

Furthermore, the number of passengers and money taken for each individual journey will be OUTPUT and then ultimately number of total passengers and total money taken for a whole day will be OUTPUT.

Lastly, the train journey with most number of passengers will be OUTPUT as well.

Explanation of Pre-Release Material:

An electric mountain railway makes four return trips every day. In each trip the train goes up the mountain and back down. The train leaves from the foot of the mountain at 09:00, 11:00, 13:00 and 15:00. The train returns from the top of the mountain at 10:00, 12:00, 14:00 and 16:00.

It can be understood from this piece of text that: • there are total 8 journeys (4 up and 4 down).

• timings for journeys up and journeys down are given.

Each train has six coaches with eighty seats available in each coach. Passengers can only purchase a return ticket; all tickets must be purchased on the day of travel. The cost is $25 for the journey up and $25 for the journey down.

It can be understood from this piece of text that:

• six coaches and eighty seats in each coach = 6 x 80 = 480 seats in each train. • passengers that go up must come back down as well.

• no pre-booking of seats in any train is allowed. • one way cost is $25 and two way cost is $50.

(4)

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). Passengers must book their return train journey, as well as the departure train journey, when they purchase their ticket. Passengers can return on the next train down the mountain or a later train. The last train from the top of the mountain has two extra coaches on it.

It can be understood from this piece of text that:

• passengers >= 10 and <= 80 will get a group discount and so every tenth passenger within this range will travel free.

• tickets for the journey up and journey down must be purchased together at same time.

• it is not compulsory for the passenger e.g. travelling at 0900 to return from the next train at 1000 and therefore he/she can return from a train coming at a different time like 1400 as well.

• last train from the top of mountain has two extra coaches and therefore total eight coaches and eighty seats in each coach = 8 x 80 = 640 seats in last train (all other 7 trains have 480 seats each).

The train times are displayed on a large screen, together with the number of tickets still available for each train. Every time a ticket is booked the display is updated. When a train is full, the word ‘Closed’ is displayed instead of the number of tickets available.

It can be understood from this piece of text that: • a screen display needs to be set-up.

• timings for all 8 train journeys and number of tickets available for each of them will be displayed/OUTPUT.

• the timings for all 8 train journeys will remain same but the number of tickets available shown on display will be updated with each purchase.

• once all the tickets for a train journey is sold out, the display will be updated and only word ‘Closed’ will be OUTPUT.

(5)

By now you must be clear about following points:

There are a total of 4 journeys up and 4 journeys down.

A passenger or group of passengers can buy a number of tickets. Each ticket costs $25 for one way journey.

The passenger going up must come back down as well.

There are 480 seats in each train except the last 8th train which has 640 seats. A specified range of group of passengers would be given a discount.

The passengers can return from any train down the mountain after they go up. With every ticket/tickets purchased, the display will be updated.

In case of tickets are no longer available for any journey, a message ‘Closed’ will be OUTPUT. The code must display proper messages for input, error or output.

The code must validate every input data.

The code must use arrays, variables and constants (wherever required) but they should be properly named so they make sense to the examiner.

Write and test a program or programs for the electric mountain railway.

Your program or programs must include appropriate prompts for the entry of data; data must be validated on entry.

Error messages and other output need to be set out clearly and understandably.

All arrays, variables, constants and other identifiers must have meaningful names.

It can be understood from this piece of text that:

• the code must contain formal, suitable and clearly understandable messages/prompts that must be displayed when asking for input of data.

• the data must be validated through various checks and using selection statements (IF..THEN..END

IF) and conditional loops (WHILE..DO..END WHILE)

• if the input is wrong then the error message must be displayed and it should be formal, suitable and clearly understandable as well.

• all output of data must be displayed with proper messages/prompts describing what is the output showing or telling. They should be formal, suitable and clearly understandable as well.

• the program will use a number of arrays, variables and constants which must have clearly understandable and meaningful names that makes sense. (instead of using names such as $money, the meaningful name must be used such as total_money etc.

(6)

Concept and understanding of TASK 1:

Variables, constants and arrays declarations

Storing the details for train times going up/down and train tickets

available for journey up/down using arrays

Declaring 4 different arrays altogether for totalling and storing money

of all 8 journeys separately (i.e. 4 journeys going up and 4 journeys

coming back down) (i.e. 2 separate arrays for total of journey upwards

and journey downwards and similarly 2 separate arrays for money.

Displaying the timings of trains for journeys going up and down

separately

Task 1 – Start of the day. Write a program to:

Set up the screen display for the start of the day.

Initialise suitable data structure(s) to total passengers for each train journey and total the money taken for each train journey.

Each train journey must be totalled separately.

There are four journeys up and four journeys down every day.

Displaying the number of train tickets available for journeys going up

and down separately

(7)

Explanation of Algorithm of TASK 1:

In this task, we have to set up the screen display along with declaring and initializing suitable data structures for train times, train tickets, totalling passengers and totalling money.

We will make use of 1D arrays to store the information relating to train timings, tickets, passengers and money for all 8 train journeys.

For setting up the complete screen display, we’ll make use of additional pieces of information given in the pre-release as well. This includes the following details:

• The train times are displayed on a large screen, together with the number of tickets still available for each train.

Since we need to display the number of tickets as well, we should also calculate the seats available in each train:

• Each train has six coaches with eighty seats available in each coach. • The last train from the top of the mountain has two extra coaches on it.

The train timings and tickets for trains going up will be stored in the table for all 4 journeys like this:

Write a program to set up the screen display for the start of the day.

time_up

tickets_up

0900

480

1100

480

Index

1

2

3

1300

480

4

1500

480

Timings for trains

going up

available for trains

Number of tickets

going up

1 coach = 80 seats 6 coaches = 80 x 6 6 coaches = 480 seats

Calculation of tickets

Each train has six coaches with eighty seats available in each coach.

(8)

The train timings and tickets for trains going down will be stored in the table for all 4 journeys like this:

time_down

tickets_down

1000

480

1200

480

Index

1

2

3

1400

480

4

1600

640

Timings for trains

going down

available for trains

Number of tickets

going down

The last train from the top of the mountain has two extra coaches

on it.

Each train has six coaches with eighty seats available in each coach. 1 coach = 80 seats 6 coaches = 80 x 6 6 coaches = 480 seats

Calculation of tickets

1 coach = 80 seats 2 coaches = 80 x 2 2 coaches = 160 seats 160 seats + 480 seats Last train has 640 seats

(9)

The screen display involves output of train timings and train tickets available. Therefore it will be displayed/output using a FOR loop.

Firstly we will output the details for trains going up using a FOR loop like this:

Running of example code:

time_up

tickets_up

0900

480

1100

480

Index

1

2

3

1300

480

4

1500

480

time_up

tickets_up

0900

480

1100

480

Index

1

2

3

1300

480

4

1500

480

(10)

Secondly we will output the details for trains going down using a FOR loop like this:

Running of example code:

In this way, the timings and tickets of 4 trains going up and 4 trains coming back down will be taken from their locations in the arrays (after being searched according to index value) and PRINTED/OUTPUT separately.

time_down

tickets_down

1000

480

1200

480

1400

480

1600

640

Index

1

2

3

4

time_down

tickets_down

1000

480

1200

480

1400

480

1600

640

Index

1

2

3

4

(11)

To set up the actual display, we will output both the train timings and tickets available in a single line unlike writing both outputs in separate lines like demonstrated above. Therefore the slight change in the code would be:

Using the pieces of code given above, the display will be setup. It will look something like the following example of display:

Screen display:

Welcome to Train Reservation System

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

The train departure times and tickets available for four journeys going up are:

The train arrival times and tickets available for four journeys coming down are:

Time: 0900 Tickets Available: 480

Time: 1100 Tickets Available: 480

Time: 1300 Tickets Available: 480

Time: 1500 Tickets Available: 480

Time: 1000 Tickets Available: 480

Time: 1200 Tickets Available: 480

Time: 1400 Tickets Available: 480

Time: 1600 Tickets Available: 640

(12)

We will declare and initialise two arrays for passengers so that the number of every passenger can be totalled and stored in 2 different arrays. 1 array for the passengers seated in trains going up and another 1 array for the passengers seated in trains going back down.

Similarly, we will declare and initialise two arrays for money earned so that the amount of money can be totalled and stored in 2 different arrays. 1 array for the money earned from trains going up and another 1 array for the money earned from trains going back down.

We will declare following arrays for money: money_up[1:4] and money_down[1:4].

In this manner, the money earned for each train journey going up will be stored in the array money_up in the respective index value of that train (i.e. 1, 2, 3 or 4).

Similarly the money earned for each train journey going back down will be stored in the array money_down in the respective index value of that train (i.e. 1, 2, 3 or 4).

As a result, all 8 journeys (4 going up and 4 coming back down) will be totalled and stored separately.

initialise suitable data structure(s) to total passengers for each train journey and total the money

taken for each train journey.

each train journey must be totalled separately.

(13)

Concept and understanding of TASK 2:

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

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.

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.

(14)

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.

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

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.

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.

(15)

1) One method is to individually store the index of the journey in the specific variable using IF…THEN…END IF selection statements:

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.

time_up

0900

Index

1

time_up

1100

Index

2

time_up

1300

Index

3

time_up

1500

Index

4

Table given in

explanation

of algorithm

of TASK 1

(16)

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.

(17)

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

(18)

Now moving forward to the actual requirement which is to:

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:

Check that the number of tickets for the required train journeys up and down the mountain is available.

(19)

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 the tickets are available, calculate the total price including any group discount.

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.

(20)

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

(21)

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

(22)

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

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.

(23)

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:

Tables given

in explanation

of algorithm

of TASK 1

time_up

tickets_up

0900

480

1100

480

Index

1

2

3

1300

480

4

1500

480

time_down

tickets_down

1000

480

1200

480

Index

1

2

3

1400

480

4

1600

640

(24)

Therefore it can be deduced that: • num_tickets = 140 • index_up  2 • index_down  4 • tickets_up[index_up]  480 • tickets_down[index_down]  640 • passengers_up[index_up]  0 • passengers_down[index_down]  0 • money_up[index_up]  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

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

money_down[4]  0 + 3300

money_up[2]  3300 money_down[4]  3300

We are assuming that code is

being run for the first time and

the passengers travelled and

money earned in the start is 0.

Since passengers travelling are greater than 80 money will be calculated using the following piece of code →

(25)

(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 • index_down  4 • tickets_up[index_up]  340 • tickets_down[index_down]  500 • passengers_up[index_up]  140 • passengers_down[index_down]  140 • money_up[index_up]  3300 • 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

money_down[4]  money_down[4] + one_way_cost money_up[2]  3300 + 1475

money_down[4]  3300 + 1475

money_up[2]  4775 money_down[4]  4775

We are assuming that code is

being run for the second time

and

the

tickets

available,

passengers travelled and money

earned is taken from the results

of the previous running of code.

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

25 x (65 – (65 DIV 10)) 25 x (65 – 6) = 1475

(26)

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.

(27)

Concept and understanding of TASK 3:

Variables, constants and arrays declarations

Displaying the number of passengers and total money taken for each

train journey separately (already calculated and stored in TASK 2)

Calculating the total number of passengers and total money taken for

the complete day for all 8 train journeys combined together using

concept of totalling

Displaying the calculated total number of passengers and total money

taken for the complete day for all journeys

Task 3 – End of the day.

Display the number of passengers that travelled on each train journey and the total money taken for each train journey.

Calculate and display the total number of passengers and the total amount of money taken for the day.

Find and display the train journey with the most passengers that day.

Finding the journey with most passengers using conditional statements

and then storing it in a separate variable

(28)

Explanation of Algorithm of TASK 3:

This task only revolves around the number of passengers that travelled and the total money taken. We have to output both of these for each train journey separately and then collectively for the whole day as well. In the end, we will simply output the train journey which carried most number of passengers in a day.

There is no need for calculations as the number of passengers for each train journey and money taken for each train journey is already calculated and stored in their respective arrays.

In TASK 1, suitable data structure(s) have been initialized to total passengers for each train journey and total the money taken for each train journey:

• passengers_up[1:4] • passengers_down[1:4] • money_up[1:4]

• money_down[1:4]

In TASK 2, the data for the both totals has been updated and their values have been stored in every journeys individual array.

So now in TASK 3, all we need to do is simply use a FOR loop and output the number of passengers and total money taken for each train journey alongside the timings for every train.

i) Code for journey up:

At first 1 will be placed in [count] and therefore details stored at index 1 of every array will be searched and output from their positions. This will ensure that timings for 1st journey up are output alongside passengers and money.

The count will then increment and so 2 will be placed in [count] and therefore details stored at index 2 of every array will be searched and output from their positions. This will ensure that timings for 2nd journey up are output alongside passengers and money.

This will continue until count becomes 4 and hence all 4 journeys going up will be output with their timings and details.

Display the number of passengers that travelled on each train journey and the total money taken for each train journey.

(29)

Example running:

Suppose the following data was stored:

• In journey 1 → 200 passengers travelled and money earned was $4800. • In journey 2 → 75 passengers travelled and money earned was $1700. • In journey 3 → 400 passengers travelled and money earned was $9800. • In journey 4 → 0 passengers travelled and money earned was $0.

The output from the values stored above will look something like this:

ii) Code for journey down:

The code for journey down is exactly the same but every “up” word is replaced with “down” and so the number of passengers and total money taken for 4 journeys down will be output with their timings.

To calculate the total number of passengers and total amount of money taken for the whole day, we will simply use concept of Totalling and FOR loop.

The number of passengers stored for each train journey at their respective index values/position in the array would be looked up one by one and then totalled and stored in a separate total_passengers_up and total_passengers_down variables. Since there are 4 indexes, a FOR loop will increment and run 4 times so every journey up and down is searched in a sequence and then stored number of passengers of each journey is added to the separate variables of total passengers for journey up and journey down.

Example Output:

The number of passengers that travelled on 4 journeys up and the total money taken for

each journey up is:

Time: 0900 Passengers travelled: 200 Money taken: $4800

Time: 1100 Passengers travelled: 75 Money taken: $1700

Time: 1300 Passengers travelled: 400 Money taken: $9800

Time: 1500 Passengers travelled: 0 Money taken: $0

Calculate and display the total number of passengers and the total amount of money taken for the day.

(30)

The amount of money stored for each train journey at their respective index values/position in the array would be looked up one by one and then totalled and stored in a separate total_money_up and total_money_down variables. Since there are 4 indexes, a FOR loop will increment and run 4 times so every journey up and down is searched in a sequence and then stored amount of money of each journey is added to the separate variables of total money for journey up and journey down.

Once the total number of passengers and total money taken for all journeys up and all journeys down are stored in 4 separate variables, another piece of code will be used:

The total number of passengers for all journeys up and all journeys down will be added together and stored in a single total_passengers variable.

Similarly, the total amount of money taken for all journeys up and all journeys down will be added together and stored in a single total_money variable.

This is the final calculation which will provide us with the: • total passengers for the whole day and

• total money taken for the whole day. In the end we will simply output both variables:

• total_passengers • total_money

In this manner, the total number of passengers and the total amount of money taken for the day will be calculated and then displayed.

(31)

Example running:

Suppose the following data was stored:

• In journey 1 up → 200 passengers travelled and money earned was $4800. • In journey 2 up → 75 passengers travelled and money earned was $1700. • In journey 3 up → 400 passengers travelled and money earned was $9800. • In journey 4 up → 0 passengers travelled and money earned was $0.

• In journey 1 down → 300 passengers travelled and money earned was $7300. • In journey 2 down → 50 passengers travelled and money earned was $1125. • In journey 3 down → 0 passengers travelled and money earned was $0. • In journey 4 down → 325 passengers travelled and money earned was $7925.

The process carried out from the values stored above will look something like this:

When count will be 1: (the details stored at index [1] will be searched and their stored values will replace their respective arrays)

total_passengers_up  200 + 0 total_passengers_down  300 + 0 total_money_up  4800 + 0 total_money_down  7300 + 0

When count will be 2: (the details stored at index [2] will be searched and their stored values will replace their respective arrays)

total_passengers_up  75 + 200 total_passengers_down  50 + 300 total_money_up  1700 + 4800 total_money_down  1125 + 7300

When count will be 3: (the details stored at index [3] will be searched and their stored values will replace their respective arrays)

total_passengers_up  400 + 275 total_passengers_down  0 + 350 total_money_up  9800 + 6500 total_money_down  0 + 8425

When count will be 4: (the details stored at index [4] will be searched and their stored values will replace their respective arrays)

total_passengers_up  0 + 675 total_passengers_down  325 + 350 total_money_up  0 + 16300

(32)

Final values:

• total_passengers_up  675 • total_passengers_down  675 • total_money_up  16300 • total_money_down  16350

Total passengers and total money for the day:

total_passengers  total_passengers_up + total_passengers_down total_money  total_money_up + total_money_down

total_passengers  675 + 675 total_money  16300 + 16350 total_passengers  1350 total_money  32650

To find the journey with most passengers, we will simply declare a variable named most_passengers and initialise it to 0.

A FOR loop will be used and it will increment and run 4 times to ensure that all 4 journeys up and 4 journeys down are checked for the most passengers condition.

An IF…THEN…END IF selection statement would be used to determine the train which carried most passengers for the day.

When that train is found, its timings will be stored in most_passengers_train_time variable so that it can be later output/displayed. In addition to that, the direction of the train carrying most passengers would be stored in train_direction variable (i.e. whether this train was going “up” or “down”).

At the end, we will simply OUTPUT:

• train timings (stored in most_passengers_train_time) AND • train direction (stored in train_direction)

(33)

The following piece of code will be used:

To understand this simply, just consider that:

• the first journey had passengers greater than 0.

• the second journey had passengers less than the first journey. • the third journey had most passengers.

• the fourth journey had passengers lesser than the third journey.

Initially, if the passengers of the first journey going up [count=1] are greater than 0, then the value of number of passengers would be stored in most_passengers variable and therefore it will replace the pre-set value “0”. The train timing and direction would be stored separately too. The loop will increment to 2.

Now if the passengers of the second journey are lesser than the passengers of already stored first journey, the condition will fail as most_passengers > passengers_up[2] and therefore no changes will be made to values stored in variables. The loop will increment to 3.

Now since the passengers of the third journey are greater than the passengers of already stored first journey, the condition will pass as passengers_up[3] > most_passengers and therefore the process will be repeated. The greatest number of passengers would be stored in the most_passengers variable replacing the number of passenger previously stored from journey 1 because at that value of count [1], the passengers were considered to be greatest but now at this value of count [3], even greater number of passengers has been found. The train timing and direction of journey 3 will be stored in their respective variables replacing the already stored values. The loop will increment to 4.

Now as the passengers of the fourth journey are lesser than the passengers of already stored third journey, the condition will fail as most_passengers > passengers_up[4] and therefore no changes will be made to values stored in variables. The loop will end.

(34)

Similarly, the same condition would be tested for journeys going down as well.

Both the journeys up and journeys down would be checked simultaneously and at the end, a single journey would be found which carries most passengers. Its time and direction will be OUTPUT.

Example running:

Suppose the following data was stored:

• Pre-defined value of most_passengers = 0

• In journey 1 up → 200 passengers travelled. • In journey 2 up → 75 passengers travelled. • In journey 3 up → 400 passengers travelled. • In journey 4 up → 0 passengers travelled.

• In journey 1 down → 300 passengers travelled. • In journey 2 down → 50 passengers travelled. • In journey 3 down → 0 passengers travelled. • In journey 4 down → 325 passengers travelled.

The process carried out from the values stored above will look something like this:

When count will be 1: (the details stored at index [1] will be searched and their stored values will replace their respective arrays)

IF 200 > 0 THEN most_passengers  200 most_passengers_train_time  0900 train_direction  “up” END IF IF 300 > 200 THEN most_passengers  300 most_passengers_train_time  1000 train_direction  “down” END IF

• 1st Journey UP had most passengers.

• Therefore, condition was processed and its timings and direction were stored.

• 1st Journey DOWN had even more passengers than 1st Journey UP.

• Therefore, condition was processed and its timings and direction were stored hence replacing the 1st Journey UP.

(35)

When count will be 2: (the details stored at index [2] will be searched and their stored values will replace their respective arrays)

IF 75 > 300 THEN

CONDITION FAILED THEREFORE NOT PROCESSED

END IF

IF 50 > 300 THEN

CONDITION FAILED THEREFORE NOT PROCESSED

END IF

When count will be 3: (the details stored at index [3] will be searched and their stored values will replace their respective arrays)

IF 400 > 300 THEN most_passengers  400 most_passengers_train_time  1300 train_direction  “up” END IF IF 0 > 400 THEN

CONDITION FAILED THEREFORE NOT PROCESSED

END IF

When count will be 4: (the details stored at index [4] will be searched and their stored values will replace their respective arrays)

IF 0 > 400 THEN

CONDITION FAILED THEREFORE NOT PROCESSED

END IF

IF 325 > 400 THEN

CONDITION FAILED THEREFORE NOT PROCESSED

END IF

• 2nd Journey UP had lesser passengers than 1st Journey DOWN.

• Therefore, condition failed and no changes were made to the previously stored values in variables.

• 2nd Journey DOWN had lesser passengers than 1st Journey DOWN.

• Therefore, condition failed and no changes were made to the previously stored values in variables.

• 3rd Journey DOWN had lesser passengers than 3rd Journey UP.

• Therefore, condition failed and no changes were made to the previously stored values in variables.

• 3rd Journey UP had even more passengers than 1st Journey DOWN.

• Therefore, condition was processed and its timings and direction were stored hence replacing the 1st Journey DOWN.

• 4th Journey UP had lesser passengers than 3rd Journey UP.

• Therefore, condition failed and no changes were made to the previously stored values in variables.

• 4th Journey DOWN had lesser passengers than 3rd Journey UP.

• Therefore, condition failed and no changes were made to the previously stored values in variables.

(36)

Final values:

• most_passengers  400

• most_passengers_train_time  1300 • train_direction  up

Hence it was determined that 3rd Journey UP had most number of passengers out of all 8 journeys. Therefore the following piece of code will be used to output this information:

Final OUTPUT:

The train with most passengers travelled up at time: 1300

(Note: the number of most passengers (in this case = 400) won’t be displayed because it is

not the requirement of the TASK 3)

Color Codes:

The Pseudo codes for TASK 1, TASK 2 and TASK 3 uses different colors for representing some keywords for a better understanding. These keywords and their color codes are listed below in two tables below:

Begin & End

Declarations & Data Types

Input & Output

Pre-defined Values of Arrays

Logical, Arithmetic & Relational Operators

Selection Statements (IF...THEN…END IF)

Loop (FOR…TO…NEXT)

Loop (WHILE…DO…END WHILE)

Prompts, Messages & Variables

BLACK

LIGHT BLUE

DARK BLUE

ORANGE

GREEN

RED

PURPLE

PINK

BLACK

(37)

TASK 1 – Pseudocode:

BEGIN

DECLARE count  0 AS INTEGER

DECLARE time_up [1:4], time_down [1:4] AS INTEGER DECLARE tickets_up [1:4], tickets_down [1:4] AS INTEGER

DECLARE passengers_up [1:4], passengers_down [1:4] AS INTEGER

DECLARE money_up [1:4], money_down [1:4] AS FLOAT

time_up[1]  0900 time_up[2]  1100 time_up[3]  1300 time_up[4]  1500 time_down[1]  1000 time_down[2]  1200 time_down[3]  1400 time_down[4]  1600 tickets_up[1]  480 tickets_up[2]  480 tickets_up[3]  480 tickets_up[4]  480 tickets_down[1]  480 tickets_down[2]  480 tickets_down[3]  480 tickets_down[4]  640 money_up[1]  0.0 money_up[2]  0.0 money_up[3]  0.0 money_up[4]  0.0 money_down[1]  0.0 money_down[2]  0.0 money_down[3]  0.0 money_down[4]  0.0 passengers_up[1]  0 passengers_up[2]  0 passengers_up[3]  0 passengers_up[4]  0 passengers_down[1]  0 passengers_down[2]  0 passengers_down[3]  0 passengers_down[4]  0

(38)

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

PRINT “Time: ”, time_up[count], “Tickets available: ”, tickets_up[count]

NEXT count

PRINT “The train departure times and tickets available for four journeys coming down are:”

FOR count  1 TO 4

PRINT “Time: ”, time_down[count], “Tickets available: ”, tickets_down[count]

NEXT count

END

TASK 1 – Efficiency:

Use of

ARRAYS

to store train timings and number of train tickets available.

Use of different

ARRAYS

to store and total the number of passengers and

money earned separately for each train journey.

Initialization of all

ARRAYS

with pre-defined values.

(39)
(40)
(41)

TASK 1 – Expected Questions:

1. State three arrays you used for Task 1. State the data type and purpose of the arrays.

2. Describe the data structures you have used in Task 1 to store the data for the train. Include the name(s), data type, sample data and usage for each structure.

3. Write an algorithm for Task 1, using either Pseudocode, programming statements or a flowchart. 4. Write an algorithm for Task 1, using either Pseudocode, programming statements or a flowchart.

Assume that the data structures for storing data about train journeys going up and down have already been initialized with predefined values.

5. Write an algorithm to complete Task 1 without including any appropriate prompts, using either Pseudocode, programming statements or a flowchart.

6. Explain how your program completes/performs Task 1. Any programming statements used in your answer must be fully explained.

7. Explain how you displayed the complete details for all the journeys (part of Task 1)? You can include Pseudocode or programming statements as part of your explanation.

8. Write an algorithm for Task 1, using either Pseudocode, programming statements or a flowchart. Change the algorithm to ensure that the money earned and passengers for each journey are displayed too.

(42)

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 >

(43)

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_down[index_down]  money_down[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

(44)

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.

(45)
(46)
(47)

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.

References

Related documents

The center will focus more time and energy on supporting substance abuse prevention activities in individual congregations and faith- based organizations, and on helping faith-based

species, 19 strains related to bacterial fish disease, and 33 strains of unidentified yellow bacteria listed in Table 1 were used as negative controls.. Sensitivity of

19% serve a county. Fourteen per cent of the centers provide service for adjoining states in addition to the states in which they are located; usually these adjoining states have

In this the machine has ability to recognize and classify After constructing the features vector, one algorithm that classified the data that have been obtained

Field experiments were conducted at Ebonyi State University Research Farm during 2009 and 2010 farming seasons to evaluate the effect of intercropping maize with

In this present study, antidepressant activity and antinociceptive effects of escitalopram (ESC, 40 mg/kg) have been studied in forced swim test, tail suspension test, hot plate

ephemera joins the discussion on critical organization studies in a field of engagement already drawn out by AOM, APROS, BAM, CMS, EGOS, SCOS, Organization, Organization Studies,