M2: Arrays and Dynamic Arrays
Prof. Justin David Pineda CISSP, CISM
Why Arrays Still Matter
Learning Objectives
What is an Array?
Example:
[10, 20, 30, 40]
Memory Layout
Why Contiguous Memory Matters
Indexing Efficiency
Example:
Access arr[3] instantly
Array Operations
For more information: Learn Big O notation in 6 minutes 📈
Insertion Cost
Deletion Cost
Static Arrays
Example:
int arr[5];
Dynamic Arrays
Examples:
Python list
Java ArrayList
C++ vector
How Dynamic Arrays Work
Resizing Strategy
Example:
2 → 4 → 8 → 16
Why doubling?
Amortized Cost
Memory Trade-Off
When Arrays Fail
Database Tables
Example:
Indexing in SQL systems
Cache Storage
Packet Buffers
Cyber Context:
Security Insight
Example:
Demo Example
Key Takeaways
Knowledge Check (Recitation)
5. What happens when a dynamic array becomes full?
6. Why is inserting an element in the middle of an array slow?
7. Give one real-world system (e.g., apps, banking, networking) where arrays are used.
8. What can go wrong if a program does not check the size or limit of an array?
Exercise 2: Arrays in Real-World Programs
Objective
You will demonstrate:
What You Will Create
Simple pseudocode or code snippet
Exercise 2: Instructions
Choose ONE (1) scenario and design a simple program.
Your answer should include:
Important Reminders
Exercise 2: Scenarios
Exercise 2: Sample Answer Scenario 1
START
Input n
Create array names[n]
Create array grades[n]
Set total = 0
Set highest = 0
For i = 0 to n - 1
Input names[i]
Input grades[i]
total = total + grades[i]
If grades[i] > highest
highest = grades[i]
End If
End For
average = total / n
Display "Class Record"
For i = 0 to n - 1
Display names[i], grades[i]
End For
Display "Average Grade: ", average
Display "Highest Grade: ", highest
END
Exercise 2: Demo next session
Be ready to explain your answer in class: