0% found this document useful (0 votes)
48 views3 pages

Assesed HW

Uploaded by

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

Assesed HW

Uploaded by

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

Assessed homework -1 Total Marks: 20

1. Create a trace table for input 51.

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

3. Write a pseudocode algorithm which does the following:


Asks the user to input an integer between 0 and 23, representing the hours in a
24-hour clock, and accepts the user’s input, which you can assume will be valid.
Outputs the equivalent time in 12-hour clock format. For example:
 If the user inputs 0, the output will be 12am
 If the user inputs 2, the output will be 2am
 If the user inputs 12, the output will be 12pm
 if the user inputs 23, the output will be 11pm
Input time
If time == 0
Output “time = 12 AM”
Else if time < 12
Output “time =”, (time) “AM”
Else if time == 12
Output “time = 12 PM”
Else if time > 12
Output “time-”, (time – 12) “PM”

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]

You might also like