Slide 2
Quick Recap of Lecture 16
4 minutes · homework check
- A problem
- We printed the primes one by one.
- Could we use them again later — say, print them backwards?
- Not with one variable!
- Today
- One name that holds many values: the list (array). We learn how to make one, use it, and how it sits in memory.
Recap
Programming Fundamentals · Lecture 17
Slide 3
Today's Plan
50 minutes · 9 parts
Recap & homework check
4 min
Learning outcomes
2 min
Why do we need lists?
5 min
Declaring & initialising an array
8 min
Using elements with an index
7 min
Memory organisation
8 min
Class activity
7 min
Common mistakes & recap
7 min
Homework & next lecture
2 min
Programming Fundamentals · Lecture 17
Slide 4
Learning Outcomes (SLOs)
By the end of this lecture, you will be able to: (tag = course outcome it builds · Bloom level)
Explain why a list (array) is needed to keep many values of the same type under one name
SLO 17.1 → CLO4 · C2
Declare and initialise a one-dimensional array and use its elements by index (0 to size − 1)
SLO 17.2 → CLO4 · C3
SLO 17.3 → CLO4 · C2
Describe how an array is stored in memory (side-by-side boxes, addresses, sizeof) and the danger of an index out of bounds
CLO4 Describe the syntax and semantics of the C++ programming language.
Programming Fundamentals · Lecture 17
Slide 5
Why Do We Need Lists?
40 students, 40 marks — and we must use them more than once
Without a list
With a list (array)
- int m1, m2, m3, m4, m5; // ... up to m40?
- cin >> m1 >> m2 >> m3 >> m4 >> m5;
- // 40 cin, 40 if statements ...
- int marks[40]; // one name
- for (int i = 0; i < 40; i++)
- cin >> marks[i]; // one loop
- Real-world lists
- Seats in a Daewoo bus: 1, 2, 3 …
- An egg tray: 12 places, one type
- A cricket team sheet: 11 players
- A list (array) is…
- a group of values of the SAME type,
- under ONE name,
- each found by its position number (index).
Module 1 · Why Lists?
Programming Fundamentals · Lecture 17
Slide 6
What an Array Looks Like
int marks[5] = {72, 85, 64, 90, 58};
- marks[3]
- value 90
- (4th element)
marks
[0]
[1]
[2]
[3]
[4]
- Remember
- The first index is 0, not 1.
- The last index is size − 1.
Module 1 · Why Lists?
Programming Fundamentals · Lecture 17
Slide 7
Declaring an Array
type name[SIZE];
Declarations
- int marks[40]; // 40 ints
- double price[6]; // 6 doubles
- char grade[40]; // 40 chars
- const int SIZE = 30; // good habit
- int roll[SIZE];
- Rules
- Size is a whole-number constant.
- All elements have the same type.
- Without values: garbage (like any local variable).
- Why const SIZE?
- If the class grows from 30 to 35 students, change ONE line, not every loop.
Module 2 · Declaring & Initialising
Programming Fundamentals · Lecture 17
Slide 8
Initialising an Array
Give the values in { } when you declare it
declare.cpp (main part)
Output
- const int SIZE = 5;
- int a[SIZE] = {10, 20, 30, 40, 50}; // full list
- int b[SIZE] = {7, 9}; // rest become 0
- int c[SIZE] = {}; // all 0
- double price[] = {120.5, 80, 45.25}; // size 3
- a[2] = 99; // change one
- 10 20 30 40 50
- 7 9 0 0 0
- 0 0 0 0 0
- 120.5 80 45.25
- a[2] is now 99
- Too many values?
- int m[3] = {72, 85, 64, 90};
- error: too many initializers for 'int [3]'
Tip: int count[10] = {}; is the quickest way to make 10 counters that start at 0.
Module 2 · Declaring & Initialising
Programming Fundamentals · Lecture 17
Slide 9
Using Elements With an Index
marks[i] works like an ordinary int variable
marks.cpp (main part)
Output
- int marks[5] = {72, 85, 64, 90, 58};
- for (int i = 0; i < 5; i++) {
- cout << "marks[" << i << "] = " << marks[i] << endl;
- }
- marks[0] = 72
- marks[1] = 85
- marks[2] = 64
- marks[3] = 90
- marks[4] = 58
- Loop rule
- for (int i = 0; i < SIZE; i++)
- starts at 0 and stops BEFORE SIZE.
Module 3 · Using Elements
Programming Fundamentals · Lecture 17
Slide 10
Reading Values Into an Array
Keep every value — then use them again
reverse_input.cpp (main part)
Session
- const int DAYS = 7;
- int temp[DAYS];
- for (int i = 0; i < DAYS; i++)
- cin >> temp[i]; // fill each box
- for (int i = DAYS - 1; i >= 0; i--)
- cout << temp[i] << " "; // last day first
- Enter 7 temperatures:
- 31 33 35 29 28 34 36
- Last day first:
- 36 34 28 29 35 33 31
- Two loops
- Loop 1 stores the values.
- Loop 2 uses them again — impossible with one variable.
Backwards: start at DAYS − 1 (= 6), stop after 0.
Module 3 · Using Elements
Programming Fundamentals · Lecture 17
Slide 11
How an Array Sits in Memory
Side-by-side boxes · same size · no gaps
1000
1004
1008
1012
1016
← example addresses
Each box = 4 bytes (int)
[0]
[1]
[2]
[3]
[4]
memory.cpp (main part)
Output
- cout << sizeof(marks[0]); // 4
- cout << sizeof(marks); // 20
- cout << sizeof(marks) / sizeof(marks[0]); // 5
- long gap = (char*)&marks[i] - (char*)&marks[0];
- sizeof(marks[0]) = 4 bytes
- sizeof(marks) = 20 bytes
- elements = 5
- marks[1] starts 4 bytes …
- marks[4] starts 16 bytes …
Module 4 · Memory Organisation
Programming Fundamentals · Lecture 17
Slide 12
The Address Formula
address of a[i] = start address + i × (size of one element)
- Why start at 0?
- Index = how many elements from the start.
- a[0] is 0 steps away.
- No copy with =
- int a[3] = {1, 2, 3}, b[3];
- b = a; → error: invalid array assignment
- Copy in a loop: for (i…) b[i] = a[i];
- cout << marks;
- prints an address such as 0x7ffe…, not the values.
Module 4 · Memory Organisation
Programming Fundamentals · Lecture 17
Slide 13
CLASS ACTIVITY · 7 MIN · IN PAIRS
Draw, Declare, Predict
Draw the boxes for each array, then answer. Check your answers by running the code.
Tasks
Hints
- 1. int b[6] = {2, 4, 6}; What are b[3] and b[5]? What is the sum of all six?
- 2. Write the declaration for the prices of 8 items (decimals).
- 3. int a[5] = {3, 8, 1, 6, 4}; a[2] = a[0] + a[4]; What is a[2]?
- 4. How many bytes does int score[11] use?
- 1: missing values become 0
- 2: double … [8]
- 3: index 0 and index 4
- 4: 11 × 4
Slide 14
Common Mistakes With Arrays
The compiler catches some — but not the worst one
bounds_bug.cpp
- g++ -Wall -O2 says
- warning: iteration 5 invokes undefined behavior
- int marks[5] = {72, 85, 64, 90, 58};
- int total = 0;
- for (int i = 0; i <= 5; i++) // BUG: <=
- total += marks[i]; // reads marks[5]!
Module 5 · Common Mistakes
Programming Fundamentals · Lecture 17
Slide 15
Quick Recap
Answer aloud
Q1
What is the first and the last index of int a[10]?
Q2
What does int c[6] = {5}; store in c[0] … c[5]?
Q3
How many bytes does double rain[12] use, and where does rain[5] start?
Q4
Why is for (int i = 0; i <= SIZE; i++) wrong?
Programming Fundamentals · Lecture 17
Slide 16
Homework
Declare an array of 6 shop item prices with values, then print each index and price.
Read 10 numbers into an array. Print the values at the even indexes (0, 2, 4, 6, 8) and count the even values.
How many bytes do int roll[50] and char section[50] use? Where does roll[10] start?
Find and fix the two errors: int a[4] = {1, 2, 3, 4, 5}; for (int i = 1; i <= 4; i++) cout << a[i];
Next lecture: Arrays II — traversal, searching, max/min & counting