Module: Introduction to Arrays in C++ (Basic Level)
Module Overview
This module introduces arrays in C++ using a basic, beginner-friendly coding approach. It is
designed for students who already know simple C++ syntax such as variables, data types, cin,
cout, and loops. The focus is on how to declare, initialize, access, and manipulate arrays, with
clear examples and simple logic.
Learning Outcomes
At the end of this module, students should be able to:
1. Define what an array is in C++.
2. Declare and initialize one-dimensional arrays.
3. Access and modify array elements using index numbers.
4. Use loops to process array values.
5. Apply arrays in basic problem-solving scenarios.
1. What is an Array?
An array is a data structure that stores multiple values of the same data type under a single
variable name.
Instead of declaring many variables:
int num1, num2, num3, num4, num5;
We can use an array:
int numbers[5];
Key Characteristics of Arrays
All elements have the same data type
Elements are stored in contiguous memory locations
Each element is accessed using an index
Array index starts at 0, not 1
2. Declaring an Array
Syntax:
dataType arrayName[arraySize];
Example:
int scores[5];
This declares an array named scores that can store 5 integers.
3. Array Indexing
Each element in an array is accessed using an index number.
scores[0] // first element
scores[1] // second element
scores[4] // last element
⚠️Accessing an index outside the range (e.g., scores[5]) causes runtime errors.
4. Initializing an Array
Method 1: Initialize during declaration
int numbers[5] = {10, 20, 30, 40, 50};
Method 2: Assign values individually
int numbers[3];
numbers[0] = 5;
numbers[1] = 10;
numbers[2] = 15;
5. Displaying Array Elements
Using cout
int numbers[3] = {5, 10, 15};
cout << numbers[0] << endl;
cout << numbers[1] << endl;
cout << numbers[2] << endl;
6. Using Loops with Arrays
Arrays are commonly used with loops to avoid repetitive code.
Example: Display array elements using a for loop
int numbers[5] = {10, 20, 30, 40, 50};
for (int i = 0; i < 5; i++) {
cout << numbers[i] << endl;
Explanation:
i starts at 0
Loop runs until i < 5
numbers[i] accesses each element
7. Input Values into an Array
Example: Using cin with arrays
int numbers[5];
for (int i = 0; i < 5; i++) {
cin >> numbers[i];
}
This allows the user to input multiple values efficiently.
8. Simple Array Program Example
Program: Input 5 numbers and display them
#include <iostream>
using namespace std;
int main() {
int numbers[5];
cout << "Enter 5 numbers:\n";
for (int i = 0; i < 5; i++) {
cin >> numbers[i];
cout << "You entered:\n";
for (int i = 0; i < 5; i++) {
cout << numbers[i] << endl;
return 0;
9. Basic Operations Using Arrays
A. Finding the Sum of Array Elements
int sum = 0;
for (int i = 0; i < 5; i++) {
sum += numbers[i];
B. Finding the Largest Value
int max = numbers[0];
for (int i = 1; i < 5; i++) {
if (numbers[i] > max) {
max = numbers[i];
10. Common Beginner Mistakes
Forgetting that array index starts at 0
Accessing array elements outside the declared size
Using the wrong loop condition (<= instead of <)
Mixing data types in an array
Practice Exercises
1. Create an array of 10 integers and display all elements.
2. Input 5 student ages and compute the average.
3. Find the smallest number in an array.
4. Reverse the elements of an array.
Summary
Arrays store multiple values in one variable
Indexing starts at 0
Loops are essential when working with arrays
Arrays simplify data handling and improve program efficiency
End of Module