Standard Algorithm & Logic Patterns: Maximum, Minimum And Average Finding Algorithms (Copy)
Maximum, Minimum And Average Finding Algorithms
What These Algorithms Are Used For (AS Level Focus)
- These are core algorithm patterns used to:
- Find the largest value
- Find the smallest value
- Calculate the average
- They appear in:
- Array questions
- Repeated input problems
- Real-world scenarios (marks, temperatures, sales, scores)
Core Examiner Rule (Extremely Important)
- Initialisation determines correctness
- A correct comparison with wrong initial value = wrong answer
- Examiners penalise:
- Missing first input handling
- Using incorrect starting values
- Dividing average by wrong quantity
Part 1: Maximum Finding Algorithm
What “Finding Maximum” Means
- Identifying the largest value from a set of values
- Requires:
- Comparison
- Updating a tracking variable
Correct Maximum-Finding Strategy (Exam-Safe)
Rule You Must Follow
- Initialise max using the first input
- Never guess a starting value like
0or-1unless explicitly justified
Standard Maximum Algorithm (No Array)
INPUT num
max ← num
FOR i ← 2 TO n
INPUT num
IF num > max THEN
max ← num
ENDIF
NEXT i
OUTPUT max
Why This Works
- First value gives a valid baseline
- All other values are compared fairly
- Works for:
- Positive numbers
- Negative numbers
- Mixed values
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions and 11 World Records For Educate A Change AS Level Computer Science Full Scale Course
Maximum Algorithm Using Arrays
When Arrays Are Needed
- Values reused later
- Multiple passes required
Array-Based Maximum Algorithm
DECLARE nums : ARRAY[1..n] OF INTEGER
INPUT nums[1]
max ← nums[1]
FOR i ← 2 TO n
INPUT nums[i]
IF nums[i] > max THEN
max ← nums[i]
ENDIF
NEXT i
OUTPUT max
Common Maximum-Finding Mistakes
Mistake 1: Incorrect Initialisation
max ← 0
- Fails if all numbers are negative
Mistake 2: Updating Outside Loop
IF num > max THEN
max ← num
ENDIF
- Must be inside the loop
Part 2: Minimum Finding Algorithm
What “Finding Minimum” Means
- Identifying the smallest value from a set of values
- Logic is the mirror image of maximum finding
Correct Minimum-Finding Strategy
Rule You Must Follow
- Initialise
minusing the first input - Never assume a starting minimum
Standard Minimum Algorithm (No Array)
INPUT num
min ← num
FOR i ← 2 TO n
INPUT num
IF num < min THEN
min ← num
ENDIF
NEXT i
OUTPUT min
Array-Based Minimum Algorithm
DECLARE nums : ARRAY[1..n] OF INTEGER
INPUT nums[1]
min ← nums[1]
FOR i ← 2 TO n
INPUT nums[i]
IF nums[i] < min THEN
min ← nums[i]
ENDIF
NEXT i
OUTPUT min
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions and 11 World Records For Educate A Change AS Level Computer Science Full Scale Course
Common Minimum-Finding Mistakes
Mistake 1: Initialising With Arbitrary Large Number
min ← 9999
- Unsafe
- Examiner dislikes assumptions
Mistake 2: Reversing Comparison Operator
IF num > min THEN
- Wrong direction
Part 3: Average Finding Algorithm
What “Finding Average” Means
- Calculating:
average = total ÷ count
- Requires:
- Accumulator
- Counter (sometimes implicit)
Standard Average Algorithm (All Values Included)
When Count Is Known
total ← 0
FOR i ← 1 TO n
INPUT num
total ← total + num
NEXT i
average ← total / n
OUTPUT average
Key Examiner Note
- Divide by
nonly if all inputs are included
Average Of Selected Values (Very Common Exam Pattern)
Example: Average Of Values Above 50
total ← 0
count ← 0
FOR i ← 1 TO n
INPUT num
IF num > 50 THEN
total ← total + num
count ← count + 1
ENDIF
NEXT i
IF count > 0 THEN
average ← total / count
OUTPUT average
ENDIF
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions and 11 World Records For Educate A Change AS Level Computer Science Full Scale Course
Why count > 0 Is Essential
- Prevents division by zero
- Shows examiner-aware logic
- Missing this check can lose marks
Combined Maximum, Minimum And Average Algorithm
Very High-Frequency Exam Pattern
INPUT num
max ← num
min ← num
total ← num
FOR i ← 2 TO n
INPUT num
IF num > max THEN
max ← num
ENDIF
IF num < min THEN
min ← num
ENDIF
total ← total + num
NEXT i
average ← total / n
OUTPUT max
OUTPUT min
OUTPUT average
Why This Is Efficient
- One loop
- No arrays required
- Minimal variables
- Examiner-friendly
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions and 11 World Records For Educate A Change AS Level Computer Science Full Scale Course
Using These Algorithms With Strings
Example: Longest Word Length
INPUT text
maxLength ← LENGTH(text)
FOR i ← 2 TO n
INPUT text
IF LENGTH(text) > maxLength THEN
maxLength ← LENGTH(text)
ENDIF
NEXT i
OUTPUT maxLength
Common Exam Errors Across All Three Algorithms
Error 1: Not Reading First Value Separately
- Leads to invalid comparison base
Error 2: Wrong Division In Average
average ← total / n
- Wrong if only some values included
Error 3: Forgetting Output Statements
- Logic correct
- Marks lost
Quick Comparison Table
| Algorithm | Key Variable | Update Condition |
|---|---|---|
| Maximum | max | num > max |
| Minimum | min | num < min |
| Average | total / count | accumulation + division |
Examiner Language You Should Use
- “The first value is used for initialisation”
- “Each subsequent value is compared”
- “The accumulator maintains a running total”
- “The average is calculated after the loop”
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions and 11 World Records For Educate A Change AS Level Computer Science Full Scale Course
Exam-Ready Master Template (Safe For Most Questions)
INPUT value
max ← value
min ← value
total ← value
FOR i ← 2 TO n
INPUT value
IF value > max THEN
max ← value
ENDIF
IF value < min THEN
min ← value
ENDIF
total ← total + value
NEXT i
average ← total / n
OUTPUT max
OUTPUT min
OUTPUT average
One-Line Rules To Memorise
- Initialise max and min using first input
- Compare everything else against it
- Accumulate totals inside the loop
- Divide by what you counted
- One clean loop beats multiple messy ones
