Assesed HW
Assesed HW
A Y OUTPUT
51 5
51 x 3 = 153 51 x 4 = 204 153, 204
2. Tickets are sold for a concert at $20 each. If 10 tickets are bought, then the
discount is 10%; if 20 tickets are bought the discount is 20%. No more than 25
tickets can be bought in a single transaction. (a) Use pseudocode to write an
algorithm to calculate the cost of buying a given number of tickets. (b) Explain
how you would test your algorithm.
(a) Input tickets
cost = 20
totalcost = cost * tickets
if tickets > 25
output “You cannot buy so many tickets”
Else if tickets >= 20
Discount = 20/100 * (totalcost)
Else if tickets >= 10
Discount = 10/100 * (totalcost)
Else
discount = 0
FinalCost = (totalcost) - discount
Output “final cost of tickets =”, FinalCost
(b) Testing of the algorithm can be done by using integer values to check whether the
program would function the way it’s required to, through a trace table.
Tickets totalcost discount Finalcost
(OUTPUT)
26 - - You cannot buy so
many tickets
22 22 x 20 = 440 20/100 x 440 = 88 440 – 88 = 352
18 18 x 20 = 360 10/100 x 360 = 36 360 – 36 = 324
8 8 x 20 = 160 0/100 x 160 = 0 160 – 0 = 160
4. Complete the trace table to determine the output of the following algorithm.
total = 0
n=1
while n < 6
if n mod 2 == 0
total = total + n
endif
n=n+1
endwhile
print(total)
n n<6 n mod 2 total output
1 True false
2 True True 2
3 true false 2
4 True True 6
5 true False 6
6 false 6 6
5. Write the pseudo code to define and enter 5 elements in a 1-dimensional array?
Array numbers[5]
// Iterate to take input and append array
For j = 0 to 4
Input “enter an integer”, n
numbers[j] = n
Output numbers
// Iterate the array to output
For j = 0 to 4
output “Value: ”, numbers[j]