SECTION 04 / 13

Sums and averages: two variables, one scan

3 min read
On this page 4 headings

Counting adds 1 for each record. Summing adds a value from that record. You can do both in one scan, then divide to get an average.

The first four actual Mathematics marks sum to 229. Their average is 229 ÷ 4 = 57.25. The full 30-card average is calculated in the walkthrough.
The first four actual Mathematics marks sum to 229. Their average is 229 ÷ 4 = 57.25. The full 30-card average is calculated in the walkthrough.
YOUR COURSE DATA · TRY IT HERE

Find the average Mathematics mark

Use the supplied 30 score cards. No download or coding is needed.

READY TO START

A pile of 30 cards

Start with the first card. Read its fields, do one small action, then move on.

What we remember

Read
0

Nothing has been read yet. Choose Next card to begin.

Look through all 30 original records
BhuvaneshMaths 68 · Erode
CardNo
0
Name
Bhuvanesh
Gender
M
DateOfBirth
7 Nov
CityTown
Erode
Mathematics
68
Physics
64
Chemistry
78
Total
210
HarishMaths 62 · Salem
CardNo
1
Name
Harish
Gender
M
DateOfBirth
3 Jun
CityTown
Salem
Mathematics
62
Physics
45
Chemistry
91
Total
198
ShashankMaths 57 · Chennai
CardNo
2
Name
Shashank
Gender
M
DateOfBirth
4 Jan
CityTown
Chennai
Mathematics
57
Physics
54
Chemistry
77
Total
188
RidaMaths 42 · Chennai
CardNo
3
Name
Rida
Gender
F
DateOfBirth
5 May
CityTown
Chennai
Mathematics
42
Physics
53
Chemistry
78
Total
173
RitikaMaths 87 · Madurai
CardNo
4
Name
Ritika
Gender
F
DateOfBirth
17 Nov
CityTown
Madurai
Mathematics
87
Physics
64
Chemistry
89
Total
240
AkshayaMaths 71 · Chennai
CardNo
5
Name
Akshaya
Gender
F
DateOfBirth
8 Feb
CityTown
Chennai
Mathematics
71
Physics
92
Chemistry
84
Total
247
SameerMaths 81 · Ambur
CardNo
6
Name
Sameer
Gender
M
DateOfBirth
23 Mar
CityTown
Ambur
Mathematics
81
Physics
82
Chemistry
87
Total
250
AdityaMaths 84 · Vellore
CardNo
7
Name
Aditya
Gender
M
DateOfBirth
15 Mar
CityTown
Vellore
Mathematics
84
Physics
92
Chemistry
76
Total
252
SuryaMaths 74 · Bengaluru
CardNo
8
Name
Surya
Gender
M
DateOfBirth
28 Feb
CityTown
Bengaluru
Mathematics
74
Physics
64
Chemistry
51
Total
189
ClarenceMaths 63 · Bengaluru
CardNo
9
Name
Clarence
Gender
M
DateOfBirth
6 Dec
CityTown
Bengaluru
Mathematics
63
Physics
88
Chemistry
73
Total
224
KavyaMaths 64 · Chennai
CardNo
10
Name
Kavya
Gender
F
DateOfBirth
12 Jan
CityTown
Chennai
Mathematics
64
Physics
72
Chemistry
68
Total
204
RahulMaths 97 · Bengaluru
CardNo
11
Name
Rahul
Gender
M
DateOfBirth
30 Apr
CityTown
Bengaluru
Mathematics
97
Physics
92
Chemistry
92
Total
281
SrinidhiMaths 52 · Chennai
CardNo
12
Name
Srinidhi
Gender
F
DateOfBirth
14 Jan
CityTown
Chennai
Mathematics
52
Physics
64
Chemistry
71
Total
187
GopiMaths 65 · Madurai
CardNo
13
Name
Gopi
Gender
M
DateOfBirth
6 May
CityTown
Madurai
Mathematics
65
Physics
73
Chemistry
89
Total
227
SophiaMaths 89 · Trichy
CardNo
14
Name
Sophia
Gender
F
DateOfBirth
23 July
CityTown
Trichy
Mathematics
89
Physics
62
Chemistry
93
Total
244
GoutamiMaths 76 · Theni
CardNo
15
Name
Goutami
Gender
F
DateOfBirth
22 Sep
CityTown
Theni
Mathematics
76
Physics
58
Chemistry
90
Total
224
TauseefMaths 87 · Trichy
CardNo
16
Name
Tauseef
Gender
M
DateOfBirth
30 Dec
CityTown
Trichy
Mathematics
87
Physics
86
Chemistry
43
Total
216
ArshadMaths 62 · Chennai
CardNo
17
Name
Arshad
Gender
M
DateOfBirth
14 Dec
CityTown
Chennai
Mathematics
62
Physics
81
Chemistry
67
Total
210
AbiramiMaths 72 · Erode
CardNo
18
Name
Abirami
Gender
F
DateOfBirth
9 Oct
CityTown
Erode
Mathematics
72
Physics
92
Chemistry
97
Total
261
VetrivelMaths 56 · Trichy
CardNo
19
Name
Vetrivel
Gender
M
DateOfBirth
30 Aug
CityTown
Trichy
Mathematics
56
Physics
78
Chemistry
62
Total
196
KalyanMaths 93 · Vellore
CardNo
20
Name
Kalyan
Gender
M
DateOfBirth
17 Sep
CityTown
Vellore
Mathematics
93
Physics
68
Chemistry
91
Total
252
MonikaMaths 78 · Bengaluru
CardNo
21
Name
Monika
Gender
F
DateOfBirth
15 Mar
CityTown
Bengaluru
Mathematics
78
Physics
69
Chemistry
74
Total
221
PriyaMaths 62 · Nagercoil
CardNo
22
Name
Priya
Gender
F
DateOfBirth
17 Jul
CityTown
Nagercoil
Mathematics
62
Physics
62
Chemistry
57
Total
181
DeepikaMaths 97 · Bengaluru
CardNo
23
Name
Deepika
Gender
F
DateOfBirth
13 May
CityTown
Bengaluru
Mathematics
97
Physics
91
Chemistry
88
Total
276
SiddharthMaths 44 · Madurai
CardNo
24
Name
Siddharth
Gender
M
DateOfBirth
26 Dec
CityTown
Madurai
Mathematics
44
Physics
72
Chemistry
58
Total
174
GeetaMaths 87 · Chennai
CardNo
25
Name
Geeta
Gender
F
DateOfBirth
16 May
CityTown
Chennai
Mathematics
87
Physics
75
Chemistry
92
Total
254
JKMaths 74 · Chennai
CardNo
26
Name
JK
Gender
M
DateOfBirth
22 Jul
CityTown
Chennai
Mathematics
74
Physics
71
Chemistry
82
Total
227
JaganMaths 81 · Madurai
CardNo
27
Name
Jagan
Gender
M
DateOfBirth
4 Mar
CityTown
Madurai
Mathematics
81
Physics
76
Chemistry
52
Total
209
NishaMaths 74 · Madurai
CardNo
28
Name
Nisha
Gender
F
DateOfBirth
10 Sep
CityTown
Madurai
Mathematics
74
Physics
83
Chemistry
83
Total
240
NaveenMaths 72 · Vellore
CardNo
29
Name
Naveen
Gender
M
DateOfBirth
13 Oct
CityTown
Vellore
Mathematics
72
Physics
66
Chemistry
81
Total
219
YOUR COURSE DATA · TRY IT HERE

Add the Mathematics marks

Use the supplied 30 score cards. No download or coding is needed.

READY TO START

A pile of 30 cards

Start with the first card. Read its fields, do one small action, then move on.

What we remember

Read
0

Nothing has been read yet. Choose Next card to begin.

Look through all 30 original records
BhuvaneshMaths 68 · Erode
CardNo
0
Name
Bhuvanesh
Gender
M
DateOfBirth
7 Nov
CityTown
Erode
Mathematics
68
Physics
64
Chemistry
78
Total
210
HarishMaths 62 · Salem
CardNo
1
Name
Harish
Gender
M
DateOfBirth
3 Jun
CityTown
Salem
Mathematics
62
Physics
45
Chemistry
91
Total
198
ShashankMaths 57 · Chennai
CardNo
2
Name
Shashank
Gender
M
DateOfBirth
4 Jan
CityTown
Chennai
Mathematics
57
Physics
54
Chemistry
77
Total
188
RidaMaths 42 · Chennai
CardNo
3
Name
Rida
Gender
F
DateOfBirth
5 May
CityTown
Chennai
Mathematics
42
Physics
53
Chemistry
78
Total
173
RitikaMaths 87 · Madurai
CardNo
4
Name
Ritika
Gender
F
DateOfBirth
17 Nov
CityTown
Madurai
Mathematics
87
Physics
64
Chemistry
89
Total
240
AkshayaMaths 71 · Chennai
CardNo
5
Name
Akshaya
Gender
F
DateOfBirth
8 Feb
CityTown
Chennai
Mathematics
71
Physics
92
Chemistry
84
Total
247
SameerMaths 81 · Ambur
CardNo
6
Name
Sameer
Gender
M
DateOfBirth
23 Mar
CityTown
Ambur
Mathematics
81
Physics
82
Chemistry
87
Total
250
AdityaMaths 84 · Vellore
CardNo
7
Name
Aditya
Gender
M
DateOfBirth
15 Mar
CityTown
Vellore
Mathematics
84
Physics
92
Chemistry
76
Total
252
SuryaMaths 74 · Bengaluru
CardNo
8
Name
Surya
Gender
M
DateOfBirth
28 Feb
CityTown
Bengaluru
Mathematics
74
Physics
64
Chemistry
51
Total
189
ClarenceMaths 63 · Bengaluru
CardNo
9
Name
Clarence
Gender
M
DateOfBirth
6 Dec
CityTown
Bengaluru
Mathematics
63
Physics
88
Chemistry
73
Total
224
KavyaMaths 64 · Chennai
CardNo
10
Name
Kavya
Gender
F
DateOfBirth
12 Jan
CityTown
Chennai
Mathematics
64
Physics
72
Chemistry
68
Total
204
RahulMaths 97 · Bengaluru
CardNo
11
Name
Rahul
Gender
M
DateOfBirth
30 Apr
CityTown
Bengaluru
Mathematics
97
Physics
92
Chemistry
92
Total
281
SrinidhiMaths 52 · Chennai
CardNo
12
Name
Srinidhi
Gender
F
DateOfBirth
14 Jan
CityTown
Chennai
Mathematics
52
Physics
64
Chemistry
71
Total
187
GopiMaths 65 · Madurai
CardNo
13
Name
Gopi
Gender
M
DateOfBirth
6 May
CityTown
Madurai
Mathematics
65
Physics
73
Chemistry
89
Total
227
SophiaMaths 89 · Trichy
CardNo
14
Name
Sophia
Gender
F
DateOfBirth
23 July
CityTown
Trichy
Mathematics
89
Physics
62
Chemistry
93
Total
244
GoutamiMaths 76 · Theni
CardNo
15
Name
Goutami
Gender
F
DateOfBirth
22 Sep
CityTown
Theni
Mathematics
76
Physics
58
Chemistry
90
Total
224
TauseefMaths 87 · Trichy
CardNo
16
Name
Tauseef
Gender
M
DateOfBirth
30 Dec
CityTown
Trichy
Mathematics
87
Physics
86
Chemistry
43
Total
216
ArshadMaths 62 · Chennai
CardNo
17
Name
Arshad
Gender
M
DateOfBirth
14 Dec
CityTown
Chennai
Mathematics
62
Physics
81
Chemistry
67
Total
210
AbiramiMaths 72 · Erode
CardNo
18
Name
Abirami
Gender
F
DateOfBirth
9 Oct
CityTown
Erode
Mathematics
72
Physics
92
Chemistry
97
Total
261
VetrivelMaths 56 · Trichy
CardNo
19
Name
Vetrivel
Gender
M
DateOfBirth
30 Aug
CityTown
Trichy
Mathematics
56
Physics
78
Chemistry
62
Total
196
KalyanMaths 93 · Vellore
CardNo
20
Name
Kalyan
Gender
M
DateOfBirth
17 Sep
CityTown
Vellore
Mathematics
93
Physics
68
Chemistry
91
Total
252
MonikaMaths 78 · Bengaluru
CardNo
21
Name
Monika
Gender
F
DateOfBirth
15 Mar
CityTown
Bengaluru
Mathematics
78
Physics
69
Chemistry
74
Total
221
PriyaMaths 62 · Nagercoil
CardNo
22
Name
Priya
Gender
F
DateOfBirth
17 Jul
CityTown
Nagercoil
Mathematics
62
Physics
62
Chemistry
57
Total
181
DeepikaMaths 97 · Bengaluru
CardNo
23
Name
Deepika
Gender
F
DateOfBirth
13 May
CityTown
Bengaluru
Mathematics
97
Physics
91
Chemistry
88
Total
276
SiddharthMaths 44 · Madurai
CardNo
24
Name
Siddharth
Gender
M
DateOfBirth
26 Dec
CityTown
Madurai
Mathematics
44
Physics
72
Chemistry
58
Total
174
GeetaMaths 87 · Chennai
CardNo
25
Name
Geeta
Gender
F
DateOfBirth
16 May
CityTown
Chennai
Mathematics
87
Physics
75
Chemistry
92
Total
254
JKMaths 74 · Chennai
CardNo
26
Name
JK
Gender
M
DateOfBirth
22 Jul
CityTown
Chennai
Mathematics
74
Physics
71
Chemistry
82
Total
227
JaganMaths 81 · Madurai
CardNo
27
Name
Jagan
Gender
M
DateOfBirth
4 Mar
CityTown
Madurai
Mathematics
81
Physics
76
Chemistry
52
Total
209
NishaMaths 74 · Madurai
CardNo
28
Name
Nisha
Gender
F
DateOfBirth
10 Sep
CityTown
Madurai
Mathematics
74
Physics
83
Chemistry
83
Total
240
NaveenMaths 72 · Vellore
CardNo
29
Name
Naveen
Gender
M
DateOfBirth
13 Oct
CityTown
Vellore
Mathematics
72
Physics
66
Chemistry
81
Total
219
Four maths marks produce Count = 4, Sum = 229 and Average = 57.25.
Four maths marks produce Count = 4, Sum = 229 and Average = 57.25.

Trace a small example

Card maths markCount after cardSum after card
68168
622130
573187
424229

At every stage, Count means “cards seen so far” and Sum means “their maths marks added so far”. At the end, Average = Sum/Count = 229/4 = 57.25.

Count ← 0
Sum ← 0
For each marks card X:
    Count ← Count + 1
    Sum ← Sum + X.mathsMarks
If Count > 0:
    Average ← Sum / Count
Else:
    Report "No observations to average"

Reuse the pattern for the lecture dataset

The lecture reports 30 marks cards and a maths total of 2171. Their average is 2171/30 ≈ 72.3667. The same scan can accumulate physics marks too: keep MathsSum and PhysicsSum, and update both for every card. Separate variables keep separate meanings.

Tutorial example: average word length

YOUR COURSE DATA · TRY IT HERE

Find the average word length

Use the supplied 65 word cards. No download or coding is needed.

READY TO START

A pile of 65 cards

Start with the first card. Read its fields, do one small action, then move on.

What we remember

Read
0

Nothing has been read yet. Choose Next card to begin.

Look through all 65 original records
ItPronoun · 2 letters
SerialNumber
0
Word
It
PartOfSpeech
Pronoun
LetterCount
2
wasVerb · 3 letters
SerialNumber
1
Word
was
PartOfSpeech
Verb
LetterCount
3
MondayNoun · 6 letters
SerialNumber
2
Word
Monday
PartOfSpeech
Noun
LetterCount
6
morning.Noun · 7 letters
SerialNumber
3
Word
morning.
PartOfSpeech
Noun
LetterCount
7
SwaminathanNoun · 11 letters
SerialNumber
4
Word
Swaminathan
PartOfSpeech
Noun
LetterCount
11
wasVerb · 3 letters
SerialNumber
5
Word
was
PartOfSpeech
Verb
LetterCount
3
reluctantAdjective · 9 letters
SerialNumber
6
Word
reluctant
PartOfSpeech
Adjective
LetterCount
9
toPreposition · 2 letters
SerialNumber
7
Word
to
PartOfSpeech
Preposition
LetterCount
2
openVerb · 4 letters
SerialNumber
8
Word
open
PartOfSpeech
Verb
LetterCount
4
hisPronoun · 3 letters
SerialNumber
9
Word
his
PartOfSpeech
Pronoun
LetterCount
3
eyes.Noun · 4 letters
SerialNumber
10
Word
eyes.
PartOfSpeech
Noun
LetterCount
4
HePronoun · 2 letters
SerialNumber
11
Word
He
PartOfSpeech
Pronoun
LetterCount
2
consideredVerb · 10 letters
SerialNumber
12
Word
considered
PartOfSpeech
Verb
LetterCount
10
MondayNoun · 6 letters
SerialNumber
13
Word
Monday
PartOfSpeech
Noun
LetterCount
6
speciallyAdverb · 9 letters
SerialNumber
14
Word
specially
PartOfSpeech
Adverb
LetterCount
9
unpleasantAdjective · 10 letters
SerialNumber
15
Word
unpleasant
PartOfSpeech
Adjective
LetterCount
10
inPreposition · 2 letters
SerialNumber
16
Word
in
PartOfSpeech
Preposition
LetterCount
2
theArticle · 3 letters
SerialNumber
17
Word
the
PartOfSpeech
Article
LetterCount
3
calendar.Noun · 8 letters
SerialNumber
18
Word
calendar.
PartOfSpeech
Noun
LetterCount
8
AfterPreposition · 5 letters
SerialNumber
19
Word
After
PartOfSpeech
Preposition
LetterCount
5
theArticle · 3 letters
SerialNumber
20
Word
the
PartOfSpeech
Article
LetterCount
3
deliciousAdjective · 9 letters
SerialNumber
21
Word
delicious
PartOfSpeech
Adjective
LetterCount
9
freedomNoun · 7 letters
SerialNumber
22
Word
freedom
PartOfSpeech
Noun
LetterCount
7
ofPreposition · 2 letters
SerialNumber
23
Word
of
PartOfSpeech
Preposition
LetterCount
2
SaturdayNoun · 8 letters
SerialNumber
24
Word
Saturday
PartOfSpeech
Noun
LetterCount
8
AndConjunction · 3 letters
SerialNumber
25
Word
And
PartOfSpeech
Conjunction
LetterCount
3
Sunday,Noun · 6 letters
SerialNumber
26
Word
Sunday,
PartOfSpeech
Noun
LetterCount
6
itPronoun · 2 letters
SerialNumber
27
Word
it
PartOfSpeech
Pronoun
LetterCount
2
wasVerb · 3 letters
SerialNumber
28
Word
was
PartOfSpeech
Verb
LetterCount
3
difficultAdjective · 9 letters
SerialNumber
29
Word
difficult
PartOfSpeech
Adjective
LetterCount
9
toPreposition · 2 letters
SerialNumber
30
Word
to
PartOfSpeech
Preposition
LetterCount
2
getVerb · 3 letters
SerialNumber
31
Word
get
PartOfSpeech
Verb
LetterCount
3
intoPreposition · 4 letters
SerialNumber
32
Word
into
PartOfSpeech
Preposition
LetterCount
4
theArticle · 3 letters
SerialNumber
33
Word
the
PartOfSpeech
Article
LetterCount
3
MondayNoun · 6 letters
SerialNumber
34
Word
Monday
PartOfSpeech
Noun
LetterCount
6
moodNoun · 4 letters
SerialNumber
35
Word
mood
PartOfSpeech
Noun
LetterCount
4
ofPreposition · 2 letters
SerialNumber
36
Word
of
PartOfSpeech
Preposition
LetterCount
2
workNoun · 4 letters
SerialNumber
37
Word
work
PartOfSpeech
Noun
LetterCount
4
andConjunction · 3 letters
SerialNumber
38
Word
and
PartOfSpeech
Conjunction
LetterCount
3
discipline.Noun · 10 letters
SerialNumber
39
Word
discipline.
PartOfSpeech
Noun
LetterCount
10
HePronoun · 2 letters
SerialNumber
40
Word
He
PartOfSpeech
Pronoun
LetterCount
2
shudderedVerb · 9 letters
SerialNumber
41
Word
shuddered
PartOfSpeech
Verb
LetterCount
9
atPreposition · 2 letters
SerialNumber
42
Word
at
PartOfSpeech
Preposition
LetterCount
2
theArticle · 3 letters
SerialNumber
43
Word
the
PartOfSpeech
Article
LetterCount
3
veryAdverb · 4 letters
SerialNumber
44
Word
very
PartOfSpeech
Adverb
LetterCount
4
thoughtNoun · 7 letters
SerialNumber
45
Word
thought
PartOfSpeech
Noun
LetterCount
7
ofPreposition · 2 letters
SerialNumber
46
Word
of
PartOfSpeech
Preposition
LetterCount
2
school:Noun · 6 letters
SerialNumber
47
Word
school:
PartOfSpeech
Noun
LetterCount
6
theArticle · 3 letters
SerialNumber
48
Word
the
PartOfSpeech
Article
LetterCount
3
dismalAdjective · 6 letters
SerialNumber
49
Word
dismal
PartOfSpeech
Adjective
LetterCount
6
yellowAdjective · 6 letters
SerialNumber
50
Word
yellow
PartOfSpeech
Adjective
LetterCount
6
building;Noun · 8 letters
SerialNumber
51
Word
building;
PartOfSpeech
Noun
LetterCount
8
theArticle · 3 letters
SerialNumber
52
Word
the
PartOfSpeech
Article
LetterCount
3
fire-eyedAdjective · 8 letters
SerialNumber
53
Word
fire-eyed
PartOfSpeech
Adjective
LetterCount
8
Vedanayagam,Noun · 11 letters
SerialNumber
54
Word
Vedanayagam,
PartOfSpeech
Noun
LetterCount
11
hisPronoun · 3 letters
SerialNumber
55
Word
his
PartOfSpeech
Pronoun
LetterCount
3
classNoun · 5 letters
SerialNumber
56
Word
class
PartOfSpeech
Noun
LetterCount
5
teacher,Noun · 7 letters
SerialNumber
57
Word
teacher,
PartOfSpeech
Noun
LetterCount
7
andConjunction · 3 letters
SerialNumber
58
Word
and
PartOfSpeech
Conjunction
LetterCount
3
headmasterNoun · 10 letters
SerialNumber
59
Word
headmaster
PartOfSpeech
Noun
LetterCount
10
withPreposition · 4 letters
SerialNumber
60
Word
with
PartOfSpeech
Preposition
LetterCount
4
hisPronoun · 3 letters
SerialNumber
61
Word
his
PartOfSpeech
Pronoun
LetterCount
3
thinAdjective · 4 letters
SerialNumber
62
Word
thin
PartOfSpeech
Adjective
LetterCount
4
longAdjective · 4 letters
SerialNumber
63
Word
long
PartOfSpeech
Adjective
LetterCount
4
cane...Noun · 4 letters
SerialNumber
64
Word
cane...
PartOfSpeech
Noun
LetterCount
4

Use the stored letterCount field rather than the printed card position. Tutorial 1.1 reports 65 word cards and 329 letters in total, giving 329/65 ≈ 5.0615 letters per word. Keep the fractional average: individual word lengths are whole numbers, but their average need not be.

For the seven-word teaching dataset, letter counts are 3,6,5,4,3,4,9. Their sum is 34, so average length is 34/7 ≈ 4.8571. These teaching values are separate from the tutorial’s 65-word dataset.

Common mistakes to catch

Do not add 1 to Sum when you mean to add the mark. Do not divide before checking Count. Do not reset Sum inside the loop. If you calculate an average with an integer-quotient operation, you may discard a meaningful fractional part; specify the intended division.

Try it yourself

Three marks are 10,20,30. Give final Count, Sum and Average.

Show explained answer

Count = 3, Sum = 60, Average = 60/3 = 20.

Try it yourself

What variables are needed to find averages for maths and physics in one scan?

Show explained answer

One Count plus MathsSum and PhysicsSum, all initialized before the loop. Divide each subject’s final sum by Count if Count is positive.

Try it yourself

The letter total is 17 across seven words. What is the average?

Show explained answer

17/7 ≈ 2.4286 letters per word. It is not 17 letters per word, and integer quotient 2 would lose precision.

Try it yourself

Why not report an average of 0 when there are no cards?

Show explained answer

There are no observations, so the average is undefined. Count is zero and division by zero is invalid. Report “no observations” instead.

Source: Lecture 1.2; Tutorial 1.1 — Tutorial for Lecture 2. Small teaching datasets and diagrams are prepared for these notes; lecture and tutorial totals are identified explicitly.

YOUR STUDY PROGRESS

Finished this section? Save your progress.

Back to top ↑