# Finding Maximum, Minimum & Calculating Average

- **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

A current maximum or minimum must start at a value that is valid for the actual dataset. Guessing 9999 as a minimum fails if every input exceeds 9999; starting a maximum at zero fails for all-negative data.

## Worked example

Read the first value, 12000. Set both Minimum and Maximum to 12000. The next value, 15000, changes Maximum only. A later 11000 changes Minimum only. The result is based on observations rather than a guessed limit.

```pseudocode
INPUT Value
Minimum <- Value
Maximum <- Value
FOR i <- 2 TO 5
    INPUT Value
    IF Value < Minimum THEN
        Minimum <- Value
    ENDIF
    IF Value > Maximum THEN
        Maximum <- Value
    ENDIF
NEXT i
```

## Trace before coding

For -6,-2,-9, the successive (minimum,maximum) pairs are (-6,-6), (-6,-2), (-9,-2). Equal values do not require changing either extreme. Test all-equal, all-negative and large values, not only convenient positive values.

The separate mean lesson combines total and count and handles an empty dataset.

---

## Interactive Exercises & Autotest Specifications

### Exercise 1: Exam-Style Check: Extreme Value Initialisation Misconceptions (Multiple choice, 25 XP)

**Instructions:**
A weather monitoring station records daily temperatures in the Arctic, where values range from -40°C to -5°C. If the programmer initializes `MaxTemp <- 0` before the loop, what will the algorithm erroneously report as the maximum temperature?

**Options:**
- **A**: -40
- **B**: -5
- **C**: 0
- **D**: The program will crash with a runtime division-by-zero error.


**Autotest Expectations:**
- **Initialisation bug consequence identified**: `selection_match` -> Expected: `opt_zero`

### Exercise 2: Exam-Style Question: Complete Average and Min/Max Calculation (Fill in blank, 35 XP)

**Instructions:**
Assume each score is between 0 and 100 inclusive. Complete the algorithm to compute the average of 10 student scores and find the highest score:


**Starter Code:**
```text
{"prompt":"Assume each score is between 0 and 100 inclusive. Complete the algorithm to compute the average of 10 student scores and find the highest score:","template":"```pseudocode\nHighest <- 0\nTotal <- 0\nFOR i <- 1 TO 10\n    INPUT Score\n    Total <- Total + Score\n    IF Score [blank_1] Highest THEN\n        Highest <- Score\n    ENDIF\nNEXT i\nAverage <- Total [blank_2] [blank_3]\n```","word_bank":[">","<","/","*","10","100"]}
```

**Autotest Expectations:**
- **Comparison operator is greater than**: `text_exact` -> Expected: `&gt;`
- **Arithmetic division operator**: `text_exact` -> Expected: `/`
- **Count divisor is 10**: `text_exact` -> Expected: `10`

### Exercise 3: Independent Practice: Safe maximum and minimum (Pseudocode, 30 XP)

**Instructions:**
Read five integers, which may be negative or greater than 9999. Output the maximum and then the minimum. Do not assume arbitrary upper or lower limits.

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:**
- **Above old artificial bound**: `test_case` -> Expected: `{&quot;inputs&quot;:[10000,11000,12000,13000,14000],&quot;output&quot;:&quot;14000\n10000&quot;}`
- **All negative**: `test_case` -> Expected: `{&quot;inputs&quot;:[-4,-8,-1,-9,-2],&quot;output&quot;:&quot;-1\n-9&quot;}`
- **Equal values**: `test_case` -> Expected: `{&quot;inputs&quot;:[7,7,7,7,7],&quot;output&quot;:&quot;7\n7&quot;}`
- **Mixed values**: `test_case` -> Expected: `{&quot;inputs&quot;:[0,-5,8,2,-1],&quot;output&quot;:&quot;8\n-5&quot;}`

