Ix p2 Solution Practice Worksheet 1
Ix p2 Solution Practice Worksheet 1
Before You Begin: To make the most of your effort, you must try solving each problem on your own
before looking at the solution. Use your previous work, handouts, or textbooks for help, and then
compare your work with this.
1.
Total ← 0 Explanation:
FOR Counter ← 1 TO 10 Process:
REPEAT • Repeat input of a positive number until it is
OUTPUT "Enter positive number" greater than 0 (repeat it 10 times for 10 inputs)
INPUT Number • add each valid number to the total
UNTIL Number > 0 • calculate average by dividing total by 10
• output the average and total
Total ← Total + Number
NEXT Counter What is the use of Repeat Until Loop here?
To ensure a valid input of Number (positive number )
Average ← Total / 10 is entered before continuing.
OUTPUT "Total is ", Total Why used For Loop here?
OUTPUT "Average is ", Average For Loop is used as we are certain of repeating the
steps 10 times.
Note: Explanation is not part of the solution.
2.
Total ← 0
Counter ← 0
REPEAT
REPEAT
OUTPUT "Enter positive number, -1 to finish"
INPUT Number
UNTIL Number > 0 OR Number = -1
3.a)
REPEAT
OUTPUT "How many tickets would you like to buy?"
INPUT NumberOfTickets
UNTIL NumberOfTickets > 0 AND NumberOfTickets < 26
Explanation:
Process:
• repeat input of the number of tickets until a value between 1 and 25 is entered
• check the number of tickets and apply discount:
no discount if less than 10, 10% discount if between 10 and 19, 20% discount if 20 or
more
• calculate total cost using ticket price and discount
• output the final cost of the tickets
4.
Note: The algorithm of this question is provided in pseudocode for reference. It’s not part of the
solution. But do practice it question of writing similar algorithm may asked in exam.
INPUT Bill
INPUT Diners
4.a)
Diners – range check 2 to 12 inclusive, presence check and type check of integer
Bill – range check 10 to 500 inclusive, presence check and type check of real
4.b)
Test data (sample – yours might be different):
• Normal data: 5 diners and a bill of $100.00 Expected result: $20.00
• Normal data: 8 diners and a bill of $74.00 Expected result: $9.25
4.c)
Test data (sample – yours might be different):
• Abnormal data: 15 diners and a bill of $5.00 Expected result: both values rejected
4.d)
• Boundary data for lower bounds 1 diner and 2 diners and bills of $9.99 and $10.00
Expected result:
1 and $9.99 both values rejected
2 and $10.00 will OUTPUT $5.00
• Boundary data for upper bounds 12 diners and 13 diners and bills of $500.00 and
$500.01
Expected result:
13 and $500.01 both values rejected
12 and $500.00 will OUTPUT $41.67
5. a)
Accepted ← 0
Rejected ← 0
FOR i ← 1 TO 10
OUTPUT "Enter dimensions for parcel " + i
INPUT Length
INPUT Breadth
5. c)
Process:
• input length and breadth of 10 parcels
• calculate the size
• check length and breadth less than 30 and size less than 600 for each parcel
• increment the number of parcels accepted or the number of parcels rejected
• output number of parcels accepted and number of parcels rejected
5. d)
Length and breadth must be less than or equal to 30. Size must be less than or equal to
600.
6. a)
• Error 1: Counter ← 1 should be Total ← 0
• Error 2: UNTIL Number < 0 should be UNTIL Number > 0
• Error 3: Total ← Total + Counter should be Total ← Total + Number
• Error 4: Counter ← Counter + 1 is not required
• Error 5: OUTPUT Total should be after the end of the loop
• Error 6: NEXT Number should be NEXT Counter
6. b)
Total ← 0
FOR Counter ← 1 TO 10
REPEAT
OUTPUT "Enter a positive whole number"
INPUT Number
UNTIL Number > 0
Total ← Total + Number
NEXT Counter
OUTPUT Total
6. c)
Test data: 1, 0, 67, 10, 22, −10, 3, 7, 2, 8, 1, 9 (sample – yours might be different).
Counter Number Total OUTPUT
0
1 1 1
2 0
67 68
3 10 78
4 22 100
5 -10
3 103
6 7 110
7 2 112
8 8 120
9 1 121
10 9 130 130
Note: The output “Enter a positive whole number” can be skipped
6. d)
Test data: 1, 0, 67, 10, 22, −10, 3, 7, 2, 8, 1, 9 (sample – yours might be different).
§ 1 extreme § 10 normal § 3 normal § 8 normal
§ 0 erroneous § 22 normal § 7 normal § 1 normal
§ 67 normal § −10 erroneous § 2 normal § 9 normal.
7.
Note: This algorithm is part of the question. Correction and updates are made for better
understanding.
7. a)
Number1 Number2 Sign Answer OUTPUT
5 7 + 12 12
6 2 - 4 4
4 3 * 12 12
7 8 ? 0
0 0 /
7. b)
FOR i ← 0 TO 4
OUTPUT "Enter product price:"
INPUT price
ProductPrice[i] ← price
NEXT i
2. Write an algorithm for a shop, first input no of items user wants to enter, then take each
item’s prices and store it in an array. (User-defined)
FOR i ← 1 TO n
OUTPUT "Enter product price:"
INPUT Price
ProductPrice[i] ← Price
NEXT i
3. Write an algorithm for a shop take product item’s prices as input until the user provide -1
and store it in an array. (Sentinel)
REPEAT
OUTPUT "Enter product price (enter -1 to stop): "
INPUT price
4. You have a product prices array, calculate Total and average price. (total, average)
ProductPrice = [15.00, 22.50, 10.50, 5.25, 30.00, 27.00]
total ← 0
FOR i ← 1 TO 6
total ← total + marks[i]
NEXT i
average ← total / 5
OUTPUT "Total Marks: ", total
OUTPUT "Average Marks: ", average
5. You have a product prices array find Minimum and maximum price. (min-max)
ProductPrice = [15.00, 22.50, 10.50, 5.25, 30.00, 27.00]
MinPrice ← ProductPrice[0]
MaxPrice ← ProductPrice[0]
FOR i ← 1 TO 6
IF ProductPrice[i] < MinPrice THEN
MinPrice ← ProductPrice[i]
ENDIF
6. You have a product prices array calculate how many products are below $20.00 (Count)
ProductPrice = [15.00, 22.50, 10.50, 5.25, 30.00, 27.00]
count ← 0
FOR i ← 1 TO 6
IF ProductPrice[i] < 20.00 THEN
count ← count + 1
ENDIF
NEXT i
searchPrice ← 10.50
found ← false
FOR i ← 1 TO 6
IF ProductPrice[i] = searchPrice THEN
found ← true
ENDIF
NEXT i