# Totalling, Counting & Sentinel Input Loops

- **Track:** Cambridge IGCSE™ Computer Science (0478 / 0984) (CAMBRIDGE IGCSE 0478)
- **Module:** Section 7: Algorithm Design & Problem-Solving
- **Estimated Duration:** 10 minutes
- **Access:** Open Theory (All-Access for Interactive IDE &amp; Autotests)

---

## Theory & Conceptual Background

Two of the most frequently examined standard algorithmic operations in Cambridge IGCSE Computer Science are **Totalling** (accumulating sums) and **Counting** (tracking occurrences).

---

## 1. Totalling vs Counting

  
| Characteristic | Totalling (Accumulator) | Counting (Counter) |
| --- | --- | --- |
| **Purpose** | Computes the running sum of a series of numerical inputs. | Tracks how many times an event occurred or items processed. |
| **Typical Initialization** | `Total <- 0` | `Count <- 0` |
| **Update Assignment** | `Total <- Total + ItemValue` | `Count <- Count + 1` |
| **Value Added** | **Variable**: Changes with each data item entered. | **Constant**: Always increments by a fixed step (usually 1). |

---

## 2. Sentinel Values and Rogue Values

When the exact number of items to be entered is unknown in advance, a **Sentinel Value** (also called a rogue value) is used to terminate input:

- Common examples: `-1` (for positive numbers), `999`, `"XXX"`, or `"STOP"`.
- **Exam Rule**: The sentinel value **must never be included** in the calculation or added to the counter!

### The Priming Read Pattern:

To prevent the sentinel value from polluting the accumulator, developers use the "priming read" pattern:

```pseudocode
Total <- 0
Count <- 0

INPUT Value             // 1. Priming read before loop starts

WHILE Value <> -1 DO    // 2. Test condition BEFORE processing
    Total <- Total + Value
    Count <- Count + 1
    INPUT Value         // 3. Read subsequent value at end of loop body
ENDWHILE
```

If the user enters `-1` immediately on the very first prompt, the `WHILE` condition evaluates to `FALSE`, the loop never executes, and `Total` correctly remains `0`.

---

## Interactive Exercises & Autotest Specifications

### Exercise 1: Exam-Style Check: Distinguish Totalling from Counting (Multiple choice, 25 XP)

**Instructions:**
A cashier system records transactions. An algorithm uses two variables: `Count` and `RunningTotal`. Which pair of statements correctly updates these two variables each time an item with price `ItemPrice` is scanned?

**Options:**
- **A**: Count &lt;- Count + ItemPrice and RunningTotal &lt;- RunningTotal + 1
- **B**: Count &lt;- Count + 1 and RunningTotal &lt;- RunningTotal + ItemPrice
- **C**: Count &lt;- Count + ItemPrice and RunningTotal &lt;- RunningTotal + ItemPrice
- **D**: Count &lt;- Count + 1 and RunningTotal &lt;- RunningTotal + 1


**Autotest Expectations:**
- **Correct update operations for counter and accumulator**: `selection_match` -> Expected: `opt_correct`

### Exercise 2: Exam-Style Question: Trace Initialisation and Accumulator Logic (Fill in blank, 35 XP)

**Instructions:**
Fill in the missing values and variable names to complete the algorithm that totals 20 student exam scores:

```pseudocode
ExamTotal <- [blank_1]
FOR i <- 1 TO [blank_2]
    INPUT Score
    ExamTotal <- ExamTotal + [blank_3]
NEXT i
```


**Starter Code:**
```text
{"template":"ExamTotal <- [blank_1]\nFOR i <- 1 TO [blank_2]\n    INPUT Score\n    ExamTotal <- ExamTotal + [blank_3]\nNEXT i","word_bank":["0","1","20","Score","ExamTotal","i"],"prompt":"Exam-Style Question: Trace Initialisation and Accumulator Logic","blanks":[{"id":"blank_1"},{"id":"blank_2"},{"id":"blank_3"}]}
```

**Autotest Expectations:**
- **Initial total is 0**: `text_exact` -> Expected: `0`
- **Loop limit is 20**: `text_exact` -> Expected: `20`
- **Score is added to total**: `text_exact` -> Expected: `Score`

### Exercise 3: Independent Practice: Positive readings (Pseudocode, 30 XP)

**Instructions:**
Read integers until -1. Ignore other non-positive values. Output the total and then the count of positive values. Do not include the sentinel.

Write a complete program. Read inputs in the stated order. Output only the requested results, one per line, without input prompts. Test with your own values before submitting.


**Starter Code:**
```text
// Plan your solution, then write your program.

```

**Autotest Expectations:**
- **Mixed readings**: `test_case` -> Expected: `{&quot;inputs&quot;:[4,0,-2,6,-1],&quot;output&quot;:&quot;10\n2&quot;}`
- **No readings**: `test_case` -> Expected: `{&quot;inputs&quot;:[-1],&quot;output&quot;:&quot;0\n0&quot;}`
- **Single reading**: `test_case` -> Expected: `{&quot;inputs&quot;:[8,-1],&quot;output&quot;:&quot;8\n1&quot;}`

