Skip to content
Activities · Activity 4.4

Q.Let us answer the following questions using the pesudocode given in example 4.9:

​1) What will the sum when the input are 6, 7, 4, 8, 2, 5, 0, 3, 1.
​2) What will be the value of count?
​3) Why did we use the input statement to enter num twice?
​4) Why did we divide sum by count?
​5) Can there be any other approach?
Punjab PsebTextbookSubjective· 4mImportance★★★★★est
22% · 8/36 Questions
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

Tracing Example 4.9's sentinel loop on 6, 7, 4, 8, 2, 5, 0, …: the loop ends at 0, giving sum = 32 and count = 6; the two input statements form the priming-read pattern that keeps the sentinel out of the sum; sum/count is the average; and yes, count-first or list-based designs are alternative approaches.

The idea. Example 4.9 computes the average of numbers entered until the user types 0 (the sentinel). Its shape: initialise sum and count to 0; INPUT num once; WHILE num ≠ 0 → add num to sum, add 1 to count, INPUT num again; finally average = sum / count. Answering the five questions is a matter of dry-running that pseudocode on the given inputs.

Dry-run for inputs 6, 7, 4, 8, 2, 5, 0, 3, 1:

num readnum = 0?sumcount
6No61
7No132
4No173
8No254
2No275
5No326
0Yes — loop ends326

The 3 and 1 typed after 0 are never read — the algorithm has already left the loop.

1) What will the sum be? 32 (= 6 + 7 + 4 + 8 + 2 + 5). The sentinel 0 adds nothing and stops the loop before 3 and 1.

2) Value of count? 6 — six numbers were processed before the sentinel.

3) Why input num twice? It is the priming read pattern. The first INPUT (before the loop) fetches a value so the WHILE condition has something to test on the very first pass; the second INPUT (at the bottom of the body) fetches the next value so it is tested before being processed. Together they guarantee the sentinel 0 is never added to sum or counted.

4) Why divide sum by count? sum holds the total of exactly count genuine numbers, and average is defined as total ÷ how-many. Dividing by a fixed number (like 5) would be wrong because the user decides how many numbers to enter.

5) Any other approach? Yes, several:

  • Count-controlled: first ask "how many numbers?" (n), then loop exactly n times — no sentinel needed. …

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.