Slide 2
Quick Recap of Lecture 17
4 minutes · homework check
- Quick-fire
- First index? 0
- Last index of a[n]? n − 1
- a[n]? out of bounds!
- Today
- Lecture 14 gave us patterns: count, sum, max/min, flag, search. Today we run them over an array — and because the values stay in memory, we can visit the list again and again.
Recap
Programming Fundamentals · Lecture 18
Slide 3
Today's Plan
50 minutes · 10 parts
Recap & homework check
4 min
Learning outcomes
2 min
Traversal & partly-filled arrays
6 min
Sum, average & above average
6 min
Linear search
9 min
Maximum & minimum with position
8 min
Counting
4 min
Class activity
6 min
Mistakes & recap
4 min
Homework & next lecture
1 min
Programming Fundamentals · Lecture 18
Slide 4
Learning Outcomes (SLOs)
By the end of this lecture, you will be able to: (tag = course outcome it builds · Bloom level)
Traverse an array with a for loop to read, print, total and average its elements
SLO 18.1 → CLO4 · C3
Apply linear search, maximum/minimum and counting patterns to an array
SLO 18.2 → CLO1 · C3
SLO 18.3 → CLO1 · C4
Trace array loops and find off-by-one and wrong-start-value mistakes
- CLO1 Identify the necessary properties of good problem-solving techniques.
- CLO4 Describe the syntax and semantics of the C++ programming language.
Programming Fundamentals · Lecture 18
Slide 5
Traversal and Partly-Filled Arrays
Declare for the largest class; use only the first n boxes
Partly-filled array
- const int MAX = 30;
- int marks[MAX], n;
- do {
- cout << "How many students (1-30)? ";
- cin >> n;
- } while (n < 1 || n > MAX);
- for (int i = 0; i < n; i++) // only n boxes
- cin >> marks[i];
[0]
[1]
[2]
[3]
[4]
[5]
[6]
n = 5 used · rest empty (up to MAX = 30)
- Template
- for (int i = 0; i < n; i++)
- … a[i] …
Every pattern today is this one loop with a different body. Change only the body: add, compare, or count.
Module 1 · Traversal
Programming Fundamentals · Lecture 18
Slide 6
Sum, Average and Above Average
Two passes over the same list — the reason arrays exist
above_avg.cpp (main part)
Session
- int total = 0;
- for (int i = 0; i < n; i++) { // pass 1
- cin >> marks[i];
- total += marks[i];
- }
- double avg = (double) total / n;
- int above = 0;
- for (int i = 0; i < n; i++) // pass 2
- if (marks[i] > avg) above++;
- How many students (1-30)? 5
- 70 85 64 90 58
- Average: 73.4
- Above average: 85 90
- Count above average: 2
- Why an array?
- The average is known only after ALL marks are read.
Remember (double): 367 / 5 would give 73, not 73.4.
Module 2 · Sum & Average
Programming Fundamentals · Lecture 18
Slide 7
Linear Search
Check the elements one by one until you find the key
1021
1034
1047
1052
1068
1075
1083
1096
[0]
[1]
[2]
[3]
[4]
[5]
[6]
[7]
search.cpp (main part)
Two runs
- int pos = -1; // not found yet
- for (int i = 0; i < N; i++) {
- if (roll[i] == key) {
- pos = i;
- break; // stop at first match
- }
- }
- Roll number to find: 1052
- 1052 found at index 3 (seat 4)
- Roll number to find: 1050
- 1050 is not in the list
Index 3 = seat 4: people count from 1, arrays count from 0 — print pos + 1.
Module 3 · Linear Search
Programming Fundamentals · Lecture 18
Slide 8
Search: Decide 'Not Found' After the Loop
One mismatch does not mean the key is missing
search_bug.cpp (key = 1047)
Output — wrong
- for (int i = 0; i < 5; i++) {
- if (roll[i] == key)
- cout << "Found at index " << i;
- else
- cout << "Not found"; // BUG: too early
- }
- Not found
- Not found
- Found at index 2
- Not found
- Not found
Fix
- // after the loop:
- if (pos != -1) cout << "found at " << pos;
- else cout << "not in the list";
- Rule
- 'Found' can be decided early.
- 'Not found' only after checking every element.
Module 3 · Linear Search
Programming Fundamentals · Lecture 18
Slide 9
Maximum and Minimum With Position
Remember WHERE the biggest value is, not only its value
[0]
[1]
[2]
[3]
[4]
[5]
[6]
maxmin.cpp (main part)
Output
- int maxPos = 0, minPos = 0; // first element
- for (int i = 1; i < N; i++) {
- if (temp[i] > temp[maxPos]) maxPos = i;
- if (temp[i] < temp[minPos]) minPos = i;
- }
- Hottest: 41 on day 4
- Coolest: 27 on day 6
Value of the maximum = temp[maxPos]; the day = maxPos + 1. One variable gives both.
Module 4 · Maximum & Minimum
Programming Fundamentals · Lecture 18
Slide 10
The Start-Value Trap
Never start a maximum at 0 — start at the first element
max_zero_bug.cpp
Output — wrong (should be -1)
- int t[5] = {-3, -7, -1, -5, -4};
- int highest = 0; // BUG
- for (int i = 0; i < 5; i++)
- if (t[i] > highest) highest = t[i];
Highest: 0
- Fix
- int highest = t[0]; → -1
Module 4 · Maximum & Minimum
Programming Fundamentals · Lecture 18
Slide 11
Counting in an Array
count = 0; if (rule) count++;
counting.cpp (main part)
Output
- int runs[N] = {45, 0, 102, 67, 0, 12, 50, 88, 3, 0};
- int fifties = 0, ducks = 0;
- for (int i = 0; i < N; i++) {
- if (runs[i] >= 50) fifties++;
- if (runs[i] == 0) ducks++;
- }
- Scores of 50 or more: 4
- Ducks (0 runs): 3
- Two separate ifs
- Not else-if: one score could match more than one rule in other problems.
- All patterns together
- Sum → total += a[i] · Count → if (rule) c++ · Max → if (a[i] > a[pos]) pos = i · Search → if (a[i] == key) { pos = i; break; }
Module 5 · Counting
Programming Fundamentals · Lecture 18
Slide 12
CLASS ACTIVITY · 6 MIN · IN PAIRS
Trace the List
a = {14, 9, 27, 3, 27, 11}. Trace by hand, then check by running.
Questions
Hints
- 1. Value and index of the maximum (maxPos starts at 0, test >).
- 2. How many values are greater than 10?
- 3. Linear search for 3: which index, and how many comparisons?
- 4. What changes in Q1 if the test is >= instead of >?
- 1: two 27s — which one wins?
- 2: count pattern
- 3: count the checks until break
- 4: later equal value
Slide 13
Tracing Array Loops: Common Mistakes
Most array bugs are in the loop limits and start values
Test with: one element (n = 1), all equal values, all negative values, key at the first and the last position, key missing.
Module 6 · Mistakes
Programming Fundamentals · Lecture 18
Slide 14
Quick Recap
Answer aloud
Q1
Why do we need an array to count the marks above average?
Q2
In linear search, what does pos = -1 mean and why -1?
Q3
Why does maxPos start at 0 and the loop at i = 1?
Q4
Where must 'Not found' be printed, and why?
Module 6 · Mistakes
Programming Fundamentals · Lecture 18
Slide 15
Homework
Find the largest and the second largest value in {45, 78, 62, 91, 78, 55}.
Read n prices (1–20). Print how many are above Rs. 500 and their positions (1-based).
Search {5, 2, 5, 8, 5, 1, 2, 5} for a key and print ALL the positions where it appears and the count.
Make a trace table (i, temp[i], maxPos, minPos) for maxmin.cpp.
Next lecture: Mid-term review & problem-solving practice