0% found this document useful (0 votes)
9 views9 pages

Ix p2 Solution Practice Worksheet 1

The document contains solutions to a practice worksheet for a Computer Science final term exam, detailing various algorithms and pseudocode examples for input validation, calculations, and data processing. It covers topics such as loops, conditionals, and array manipulations, providing explanations for each algorithm's structure and purpose. Additionally, it includes test data and expected results for validating the algorithms.

Uploaded by

yiyadok303
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)
9 views9 pages

Ix p2 Solution Practice Worksheet 1

The document contains solutions to a practice worksheet for a Computer Science final term exam, detailing various algorithms and pseudocode examples for input validation, calculations, and data processing. It covers topics such as loops, conditionals, and array manipulations, providing explanations for each algorithm's structure and purpose. Additionally, it includes test data and expected results for validating the algorithms.

Uploaded by

yiyadok303
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/ 9

Solution of Practice Worksheet 1 Final Term (2024-2025)

Name: Class: Roll: Section:


Subject: Computer Science Teachers: Md. Shahinur Alam Shadhin Date:

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

IF Number <> -1 THEN


Total ← Total + Number
Counter ← Counter + 1
ENDIF
UNTIL Number = -1

Average ← Total / Counter


OUTPUT "Average is ", Average
OUTPUT "Total is ", Total
Explanation:
Process:
• repeat input of a positive number until a valid number or -1 is entered
• add each valid number (not -1) to the total and count it
• continue until -1 is entered
• calculate average by dividing total by the count
• output the average and total
What is the use of 2nd or nested Repeat Until loop (Blue one) here?
To ensure a valid input of Number (positive number or -1) is entered before continuing.

Why used 1st Repeat Until loop (Green one) here?


A Repeat Until loop is used because the number of repetitions is unknown. It adds numbers to the
total until -1 is entered.

Why used IF condition?


Adding the new number to total and increasing counter only if number is not -1. We need the counter
to get the average.

3.a)

REPEAT
OUTPUT "How many tickets would you like to buy?"
INPUT NumberOfTickets
UNTIL NumberOfTickets > 0 AND NumberOfTickets < 26

IF NumberOfTickets < 10 THEN


Discount ← 0
ELSE
IF NumberOfTickets < 20 THEN
Discount ← 0.1
ELSE
Discount ← 0.2
ENDIF
ENDIF

Cost ← NumberOfTickets * 20 * (1 – Discount)


OUTPUT "Your tickets cost ", Cost

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

Why used Repeat Until loop?


To ensure a valid input of tickets (1 to 25) is entered before continuing.

Why used 1st IF condition?


The first IF checks if the number of tickets is less than 10 (no discount).

Why used 2nd IF condition?


The second IF checks the exact discount based on the number of tickets (10% or 20%).
3.b)
I would use test data with values of:
0, 26, Expected results rejected
1, 25, Expected results 20, 400
9, 10, Expected results 180, 180
19, 20, Expected results 342, 320

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

IF Bill < 10 OR Bill > 500 THEN


OUTPUT "Error: Bill must be between $10 and $500"
ELSE
IF Diners < 2 OR Diners > 12 THEN
OUTPUT "Error: Diners must be between 2 and 12"
ELSE
costPerPerson ← Bill ÷ Diners
OUTPUT "Each person should pay $" + costPerPerson
ENDIF
ENDIF
Explanation (not part of solution):
Process:
• input the total bill amount
• input the number of diners
• check if the bill is between $10 and $500
• if valid, check if the number of diners is between 2 and 12
• if both are valid, divide the bill by the number of diners
• output the cost per person

Why used 1st IF condition?


1st IF checks if the bill is within the valid range ($10–$500)

Why used 2nd IF condition?


2nd IF checks if the number of diners is within the valid range (2–12)

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

Size ← Length * Breadth

IF Length ≤ 30 AND Breadth ≤ 30 AND Size ≤ 600 THEN


Accepted ← Accepted + 1
ELSE
Rejected ← Rejected + 1
ENDIF
NEXT i

OUTPUT Accepted, Rejected


Explanation (not part of solution):
Process:
• repeat input of parcel dimensions 10 times
• calculate the total size of each parcel
• check if Length, Breadth and Size are within limits
• count parcels as accepted or rejected
• output the number of accepted and rejected parcels

Why used For Loop?


FOR loop is used to process 10 parcels one by one

Why used IF condition?


IF is used to check if each parcel meets the size conditions and update the count accordingly
5. b)
Counter Accept Reject Length Breadth Size OUTPUT
0 0 0
1 1 15 10 150
2 2 20 17 340
3 1 32 10
4 2 30 35
5 3 30 15 450
6 3 30 28 840
7 4 25 25 625
8 4 20 15 300 5 5

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.

1. INPUT Number1, Number2, Sign


2. WHILE Number1 <> 0
3. IF Sign = '+' THEN Answer ← Number1 + Number2 ENDIF
4. IF Sign = '-' THEN Answer ← Number1 - Number2 ENDIF
5. IF Sign = '*' THEN Answer ← Number1 * Number2 ENDIF
6. IF Sign = '/' THEN Answer ← Number1 / Number2 ENDIF
7. IF Sign <> '/' AND Sign <> '*' AND Sign <> '-' AND Sign <> '+' THEN
8. Answer ← 0
9. ENDIF
10. IF Answer <> 0 THEN OUTPUT Answer ENDIF
11. INPUT Number1, Number2, Sign
12. ENDWHILE

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)

INPUT Number1, Number2, Sign


CASE OF Sign
ꞌ+ꞌ : Answer ← Number1 + Number2
ꞌ-ꞌ : Answer ← Number1 - Number2
ꞌ*ꞌ : Answer ← Number1 * Number2
ꞌ/ꞌ : Answer ← Number1 / Number2
OTHERWISE Answer ← 0
ENDCASE
Handout 3 Tasks and Solution

1. Write an algorithm to take 5 product prices store it in an array. (Fixed)

DECLARE ProductPrice: Array[0:4] OF REAL

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)

OUTPUT "How many product prices do you want to enter?"


INPUT n

DECLARE ProductPrice: Array[1:n] OF REAL

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)

DECLARE ProductPrice: Array[0:100] OF REAL // assuming max 100 items


i ← 0

REPEAT
OUTPUT "Enter product price (enter -1 to stop): "
INPUT price

IF price <> -1 THEN


ProductPrice[i] ← price
i ← i + 1
ENDIF
UNTIL price = -1

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

IF ProductPrice[i] > MaxPrice THEN


MaxPrice ← ProductPrice[i]
ENDIF
NEXT i

OUTPUT "Minimum Price is: ", MinPrice


OUTPUT "Maximum Price is: ", MaxPrice

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

OUTPUT "Number of products below $20.00: ", count


7. You have a product prices array find if is there any product priced $10.50 (Search)
ProductPrice = [15.00, 22.50, 10.50, 5.25, 30.00, 27.00]

searchPrice ← 10.50
found ← false

FOR i ← 1 TO 6
IF ProductPrice[i] = searchPrice THEN
found ← true
ENDIF
NEXT i

IF found = true THEN


OUTPUT "Product priced at $10.50 is found"
ELSE
OUTPUT "Product priced at $10.50 is not found"
ENDIF

You might also like