SECTION 17 / 17

Connect Weeks 1–4: choose the right pattern

3 min read
On this page 3 headings
Start, repeat and stop form the core pattern. Add filters, accumulators, procedures, sequential passes or nested comparisons to match the question.
Start, repeat and stop form the core pattern. Add filters, accumulators, procedures, sequential passes or nested comparisons to match the question.
One scan counts or sums; two sequential passes learn an average and classify; nested loops compare student pairs.
Choose the loop pattern by the question. A second loop is not always a nested loop.

Begin with valid data and meaningful types

A record is one card with named fields: Name, DateOfBirth, marks and so on. A list is an ordered collection of values or records. Integer values are whole numbers, including negative numbers and zero; a character is one symbol; a string is a sequence of characters; a Boolean is true or false. A subtype restricts values or operations, such as integer marks from 0 to 100 or valid calendar dates.

Check data sanity before using it: fields must exist, marks must be in range, totals must agree with their component marks, and dates must be valid. The course’s Gender field has the labels M and F; that is the supplied dataset’s coding, not a complete description of people. Respect the field’s permitted operations. Dates and city labels should not be treated as ordinary numbers merely because they have a written representation.

Initialize, update and terminate

An assignment gives a variable a value. Sum = Sum + X.Mathematics means read the old Sum, add this mark, then store the result; it is not an algebraic equality. A count usually starts at 0; a sum starts at 0; a maximum can start with the first valid record or an explicit empty state. Handle empty inputs rather than assuming a first card exists.

A flowchart uses rounded terminals for start/end, rectangles for processes and diamonds for decisions, linked by arrows. A loop has a continuation test and a route back after processing. In pseudocode, WHILE is the repetition and IF selects updates. Move the current card or advance an index so the loop makes progress. Every path must eventually reach a stop condition.

Filter, accumulate and compare

QuestionPatternDataset example
How many? What total?One scan with count/sum accumulators30 score cards; Mathematics sum 2171
Which records meet a fixed rule?Filter during the scanCityTown is Chennai
What is the maximum?Compare with an updating variableMathematics maximum 97
Who is below the final mean?Accumulate first, classify in a second pass15 students below 2171/30
Same operation for different fields?A procedure with a field parameterMaxMarks for Maths, Physics, Chemistry, Total
Which two records relate?Pair search, often nested iterationSame day and month of birth
Can groups rule out work?Bin, then inspect relevant groupsCity bins reduce pair checks to 55
Can remembered state replace rescans?One-pass memory, with a sufficient ruleLastName is useful but has counterexamples

A fixed filter compares with an unchanged value, such as CityTown == Chennai. An updating comparison uses a changing variable, such as X.Mathematics > Max. A threshold learned from the whole dataset, such as the final mean, must be completed before a second pass uses it. A running mean is not the final mean.

Accumulators can hold totals, counts, extrema or collections. To keep every Physics topper, collect all records equal to the final maximum, including ties. A collection of students is a different output from one maximum value. Procedures make repeated patterns reusable through parameters and return values; their side-effect promises tell callers what remains usable.

Later course material will store relationships for reuse. Here it is enough to see the motivation: compute a useful summary or relationship once, keep it in an appropriate representation, and use it in later work. Do not introduce a complicated structure before identifying what the question needs.

REAL COURSE DATA · THINK BEFORE EACH STEP

Find matching birthdays: every pair

Trace the records, compare the methods and watch what changes. Back and Reset let you try again.

Ready

CURRENT RECORD X

Ready to begin

Choose Next step to follow the actual course records.

What we remember

Processed
0

Start with Next step. Predict what changes before reading the explanation.

Try it yourself

Which pattern finds below-average Maths students correctly?

Show explained answer

First compute the final sum and count, so the average is 2171/30. Then scan again and compare each mark with that fixed average. The loops are sequential, not nested.

Try it yourself

Can maximum and sum use the same update rule?

Show explained answer

No. Sum adds every selected value. Maximum replaces the remembered value only when a larger value appears. Initialization and updates depend on the question.

Try it yourself

What should you write down before calling a procedure?

Show explained answer

Its inputs and parameter meanings, its return value or values, its assumptions such as valid data, and which order, markers and values it must preserve.

Try it yourself

Why preserve the tied maximum students rather than only their mark?

Show explained answer

The question “who are the toppers?” asks for identities. One number such as 92 cannot identify every student who achieved it.

YOUR STUDY PROGRESS

Finished this section? Save your progress.

Back to top ↑