Beginner-friendly guide to C++ one-dimensional arrays: declaration, initialization, indexing, accessing elements, loops, and practical examples.
After completing this lesson, students will be able to:
for loops to input and display array elements.Suppose a teacher wants to store the marks of 5 students in a programming class.
One way is to declare a separate variable for each student’s marks.
int student1 = 85;
int student2 = 90;
int student3 = 75;
int student4 = 88;
int student5 = 92;
Here, we have declared five separate variables to store the marks of five students.
Problem: What if a teacher wants to store the marks of 100 students? Declaring 100 separate variables would make the program difficult to manage.
C++ provides an array that allows us to store multiple values of the same data type under a single variable name.
int marks[5] = {85, 90, 75, 88, 92};
Now, all five students’ marks are stored in a single array named marks.
| Code | Meaning |
|---|---|
int |
Data type of the elements |
marks |
Name of the array |
[5] |
Number of elements in the array |
{85, 90, 75, 88, 92} |
Initial values stored in the array |
Remember: An array stores multiple values of the same data type using a single variable name.
An array is a collection of multiple values of the same data type stored in contiguous memory locations and accessed using a single variable name with different indexes.
Instead of declaring separate variables for multiple values, we can use an array.
int marks[5];
This statement declares an array named marks that can store five integer values.
A one-dimensional array stores elements in a single sequence (a single row).
For example, the marks of five students can be stored in a one-dimensional array.
int marks[5] = {85, 90, 75, 88, 92};
| Index | 0 | 1 | 2 | 3 | 4 |
|---|---|---|---|---|---|
| Marks | 85 | 90 | 75 | 88 | 92 |
Each box represents one element of the array.
marks.Declaration means creating an array by specifying its data type, name, and size.
data_type array_name[size];
int numbers[5];
| Part | Explanation |
|---|---|
int |
Each element will store an integer value. |
numbers |
Name of the array. |
[5] |
The array can store five elements. |
Important: When an ordinary local array is declared without initialization, its elements are not automatically initialized. Assign values before reading them.

An index is a number used to identify and access a particular element of an array.
In C++, array indexing starts from 0, not 1.
Consider the following array:
int marks[5] = {85, 90, 75, 88, 92};
| Student | Array Index | Marks |
|---|---|---|
| Student 1 | 0 | 85 |
| Student 2 | 1 | 90 |
| Student 3 | 2 | 75 |
| Student 4 | 3 | 88 |
| Student 5 | 4 | 92 |
0.1.size - 1.Formula:
Last Index = Array Size - 1
For an array of size 5:
Last Index = 5 - 1 = 4
Therefore, the valid indexes are 0, 1, 2, 3, 4.
Initialization means assigning initial values to an array.
There are three common ways to initialize an array.
We can declare an array and assign values at the same time.
int numbers[5] = {10, 20, 30, 40, 50};
Here, five integer values are assigned to the array.
We can omit the array size when values are provided during initialization.
int numbers[] = {5, 10, 15, 20};
The compiler calculates the array size as 4.
We can declare an array first and assign values to individual elements later.
int numbers[3];
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
Here, values are assigned using their respective indexes.
Remember: The array size must be sufficient to store all the elements.
We use the array name followed by an index inside square brackets to access an individual element.
int numbers[5] = {10, 20, 30, 40, 50};
cout << numbers[0] << endl;
cout << numbers[3] << endl;
10
40
numbers[0] accesses the first element, which is 10.numbers[3] accesses the fourth element, which is 40.Important: The index identifies the position of an element, not its actual value.
We can assign values to array elements using their indexes.
#include <iostream>
using namespace std;
int main()
{
int numbers[5];
// Assigning values
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
numbers[3] = 40;
numbers[4] = 50;
// Displaying values
cout << numbers[0] << endl;
cout << numbers[1] << endl;
cout << numbers[2] << endl;
cout << numbers[3] << endl;
cout << numbers[4] << endl;
return 0;
}
Step 1: Declare the array
int numbers[5];
Creates an array that can store five integers.
Step 2: Assign values
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
Assigns values to the first three elements. The remaining two elements are also assigned in the program.
Step 3: Display values
cout << numbers[0] << endl;
Displays the value stored at index 0.
10
20
30
40
50
| Index | Value |
|---|---|
| 0 | 10 |
| 1 | 20 |
| 2 | 30 |
| 3 | 40 |
| 4 | 50 |
for LoopImagine an array contains 100 elements. Writing 100 separate output statements would be time-consuming.
We can use a for loop to access and display all array elements.
#include <iostream>
using namespace std;
int main()
{
int numbers[5] = {10, 20, 30, 40, 50};
for(int i = 0; i < 5; i++)
{
cout << numbers[i] << endl;
}
return 0;
}
10
20
30
40
50
for(int i = 0; i < 5; i++)
The loop runs five times.
| Iteration | Value of i |
Accessed Element | Output |
|---|---|---|---|
| 1 | 0 | numbers[0] |
10 |
| 2 | 1 | numbers[1] |
20 |
| 3 | 2 | numbers[2] |
30 |
| 4 | 3 | numbers[3] |
40 |
| 5 | 4 | numbers[4] |
50 |
Important Points:
i represents the array index.i = 0 starts from the first element.i < 5 ensures that the loop accesses five elements.i++ increases the index by one after each iteration.numbers[i] accesses the element at the current index.Try the following modifications:
{5, 10, 15, 20, 25}.1 and 3.So far, we have assigned values directly in the program.
In practical applications, we often need to take values from the user.
We can use cin with a for loop to input values into an array.
Write a C++ program to store and display the marks of five students.
#include <iostream>
using namespace std;
int main()
{
int marks[5];
// Taking input
cout << "Enter marks of 5 students:" << endl;
for(int i = 0; i < 5; i++)
{
cin >> marks[i];
}
// Displaying output
cout << "Marks of students are:" << endl;
for(int i = 0; i < 5; i++)
{
cout << marks[i] << endl;
}
return 0;
}
Step 1: Declare the array
int marks[5];
Creates an array that can store marks of five students.
Step 2: Take input using a loop
for(int i = 0; i < 5; i++)
{
cin >> marks[i];
}
0.cin >> marks[i] takes input from the user.Step 3: Display the array elements
for(int i = 0; i < 5; i++)
{
cout << marks[i] << endl;
}
Displays all five stored values one by one.
Enter marks of 5 students:
78
85
90
66
88
Marks of students are:
78
85
90
66
88
| Index | Student | Stored Marks |
|---|---|---|
| 0 | Student 1 | 78 |
| 1 | Student 2 | 85 |
| 2 | Student 3 | 90 |
| 3 | Student 4 | 66 |
| 4 | Student 5 | 88 |
We can perform mathematical operations on array elements.
For example, suppose we want to calculate the total of five numbers stored in an array.
Write a C++ program to store five numbers in an array and calculate their sum.
#include <iostream>
using namespace std;
int main()
{
int numbers[5] = {10, 20, 30, 40, 50};
int sum = 0;
for(int i = 0; i < 5; i++)
{
sum = sum + numbers[i];
}
cout << "Sum = " << sum << endl;
return 0;
}
Step 1: Declare and initialize the array
int numbers[5] = {10, 20, 30, 40, 50};
Stores five numbers in the array.
Step 2: Initialize the sum variable
int sum = 0;
Creates a variable named sum and initializes it with zero.
Step 3: Calculate the sum
for(int i = 0; i < 5; i++)
{
sum = sum + numbers[i];
}
The loop accesses each array element and adds its value to sum.
| Iteration | Index (i) |
Array Value | Previous Sum | New Sum |
|---|---|---|---|---|
| 1 | 0 | 10 | 0 | 10 |
| 2 | 1 | 20 | 10 | 30 |
| 3 | 2 | 30 | 30 | 60 |
| 4 | 3 | 40 | 60 | 100 |
| 5 | 4 | 50 | 100 | 150 |
Sum = 150
| Mistake | Correct Approach |
|---|---|
Accessing the first element using index 1 |
Use index 0. |
| Accessing an element outside the array size | Use valid indexes from 0 to size - 1. |
| Using different data types in the same array | Use the same element data type. |
| Reading an uninitialized array element | Assign a value before reading it. |
Using i <= 5 for an array of size 5 |
Use i < 5. |
Suppose we have marks of five students:
75, 80, 65, 90, 85
We want to find:
#include <iostream>
using namespace std;
int main()
{
int marks[5] = {75, 80, 65, 90, 85};
int largest = marks[0];
int smallest = marks[0];
for (int i = 1; i < 5; i++)
{
if (marks[i] > largest)
{
largest = marks[i];
}
if (marks[i] < smallest)
{
smallest = marks[i];
}
}
cout << "Largest Marks: " << largest << endl;
cout << "Smallest Marks: " << smallest << endl;
return 0;
}
Step 1: Declare and initialize the array
int marks[5] = {75, 80, 65, 90, 85};
int means the array stores integers.marks is the array name.5 means the array contains five elements.Step 2: Initialize largest and smallest
int largest = marks[0];
int smallest = marks[0];
We assume the first element is both the largest and smallest.
Initially:
Step 3: Traverse the array
for (int i = 1; i < 5; i++)
The loop starts from index 1 because index 0 is already assigned to both variables.
Step 4: Find the largest value
if (marks[i] > largest)
{
largest = marks[i];
}
If the current element is greater than the largest value, update largest.
Step 5: Find the smallest value
if (marks[i] < smallest)
{
smallest = marks[i];
}
If the current element is smaller than the smallest value, update smallest.
| Index | Marks | Largest | Smallest |
|---|---|---|---|
| 0 | 75 | 75 | 75 |
| 1 | 80 | 80 | 75 |
| 2 | 65 | 80 | 65 |
| 3 | 90 | 90 | 65 |
| 4 | 85 | 90 | 65 |
Final Result
Largest = 90, Smallest = 65.
Modification 1: Change the array values to:
int marks[5] = {45, 88, 92, 67, 55};
Predict the largest and smallest values before running the program.
Modification 2: Change the array size from 5 to 10 and enter ten student marks using cin.
Suppose we have the following student marks:
75, 80, 65, 90, 85
We want to search for a particular number.
For example, is 90 present in the array?
#include <iostream>
using namespace std;
int main()
{
int marks[5] = {75, 80, 65, 90, 85};
int search;
cout << "Enter marks to search: ";
cin >> search;
bool found = false;
for (int i = 0; i < 5; i++)
{
if (marks[i] == search)
{
cout << "Element found at index: " << i << endl;
found = true;
break;
}
}
if (found == false)
{
cout << "Element not found";
}
return 0;
}
Step 1: Declare a variable for searching
int search;
This variable stores the value entered by the user.
Step 2: Take input
cin >> search;
Suppose the user enters 90.
Step 3: Declare a Boolean variable
bool found = false;
bool stores either true or false.Step 4: Check every array element
for (int i = 0; i < 5; i++)
The loop checks elements one by one.
Step 5: Compare the current element
if (marks[i] == search)
The == operator checks whether two values are equal.
Step 6: Update the result
found = true;
break;
found = true means the element exists.break stops the loop because we have found the element.Searching for 90:
| Index | Array Value | Comparison | Result |
|---|---|---|---|
| 0 | 75 | 75 == 90 | False |
| 1 | 80 | 80 == 90 | False |
| 2 | 65 | 65 == 90 | False |
| 3 | 90 | 90 == 90 | True |
The search stops at index 3.
Student Found instead of Element Found.break and observe what happens if duplicate values exist.Suppose we have these numbers:
10, 15, 20, 25, 30, 35
We want to count:
#include <iostream>
using namespace std;
int main()
{
int numbers[6] = {10, 15, 20, 25, 30, 35};
int even = 0;
int odd = 0;
for (int i = 0; i < 6; i++)
{
if (numbers[i] % 2 == 0)
{
even++;
}
else
{
odd++;
}
}
cout << "Even Numbers: " << even << endl;
cout << "Odd Numbers: " << odd << endl;
return 0;
}
Step 1: Initialize counters
int even = 0;
int odd = 0;
Both counters start from zero.
Step 2: Traverse the array
for (int i = 0; i < 6; i++)
The loop checks all six numbers.
Step 3: Check even numbers
if (numbers[i] % 2 == 0)
The % operator returns the remainder.
For example:
10 % 2 = 0 → Even15 % 2 = 1 → OddStep 4: Increase the counter
even++;
The ++ operator increases the counter by one.
It is equivalent to:
even = even + 1;
| Number | Remainder | Even | Odd |
|---|---|---|---|
| 10 | 0 | 1 | 0 |
| 15 | 1 | 1 | 1 |
| 20 | 0 | 2 | 1 |
| 25 | 1 | 2 | 2 |
| 30 | 0 | 3 | 2 |
| 35 | 1 | 3 | 3 |
Output:
Even Numbers: 3
Odd Numbers: 3
Suppose an array contains:
10, 20, 30, 40, 50
We want to display its elements in reverse order:
50, 40, 30, 20, 10
#include <iostream>
using namespace std;
int main()
{
int numbers[5] = {10, 20, 30, 40, 50};
for (int i = 4; i >= 0; i--)
{
cout << numbers[i] << " ";
}
return 0;
}
Step 1: Understand array indexes
| Index | Value |
|---|---|
| 0 | 10 |
| 1 | 20 |
| 2 | 30 |
| 3 | 40 |
| 4 | 50 |
Step 2: Start from the last index
int i = 4;
The last index of an array of size 5 is 4.
Step 3: Set the loop condition
i >= 0;
Continue until index 0.
Step 4: Decrease the index
i--;
Move one position backwards.
Step 5: Display the current element
cout << numbers[i] << " ";
This displays elements from the last index to the first.
| Iteration | Index | Value |
|---|---|---|
| 1 | 4 | 50 |
| 2 | 3 | 40 |
| 3 | 2 | 30 |
| 4 | 1 | 20 |
| 5 | 0 | 10 |
Suppose we have an array containing five student marks.
We want to copy all its values into another array.
Original array:
10, 20, 30, 40, 50
Copied array:
10, 20, 30, 40, 50
#include <iostream>
using namespace std;
int main()
{
int first[5] = {10, 20, 30, 40, 50};
int second[5];
for (int i = 0; i < 5; i++)
{
second[i] = first[i];
}
cout << "Copied Array: ";
for (int i = 0; i < 5; i++)
{
cout << second[i] << " ";
}
return 0;
}
Step 1: Create the original array
int first[5] = {10, 20, 30, 40, 50};
This array contains the original values.
Step 2: Declare another array
int second[5];
This array will store copied values.
Step 3: Copy elements
second[i] = first[i];
This statement copies one element at a time.
For example:
second[0] = first[0]second[1] = first[1]second[2] = first[2]The loop continues until all elements are copied.
| Index | First Array | Second Array |
|---|---|---|
| 0 | 10 | 10 |
| 1 | 20 | 20 |
| 2 | 30 | 30 |
| 3 | 40 | 40 |
| 4 | 50 | 50 |
Suppose we have five student marks:
50, 20, 40, 10, 30
We want to arrange them in ascending order:
10, 20, 30, 40, 50
Explore Bubble Sort – Interactive Step-by-Step Animation
Let’s understand how Bubble Sort arranges an array in ascending order by comparing and swapping adjacent elements.
Initial Array:
50, 20, 40, 10, 30
Step 1: First Pass
In the first pass, compare each pair of adjacent elements. If the left element is greater than the right element, swap their positions.
| Step | Comparison | Action | Array After Comparison |
|---|---|---|---|
| 1 | 50 and 20 | Swap | 20, 50, 40, 10, 30 |
| 2 | 50 and 40 | Swap | 20, 40, 50, 10, 30 |
| 3 | 50 and 10 | Swap | 20, 40, 10, 50, 30 |
| 4 | 50 and 30 | Swap | 20, 40, 10, 30, 50 |
End of Pass 1
Array after the first pass:
20, 40, 10, 30, 50
What happened?
50, moved to the last position.Step 2: Second Pass
Now compare the remaining unsorted elements. There is no need to compare the last element because 50 is already in its correct position.
| Step | Comparison | Action | Array After Comparison |
|---|---|---|---|
| 1 | 20 and 40 | No Swap | 20, 40, 10, 30, 50 |
| 2 | 40 and 10 | Swap | 20, 10, 40, 30, 50 |
| 3 | 40 and 30 | Swap | 20, 10, 30, 40, 50 |
End of Pass 2
Array after the second pass:
20, 10, 30, 40, 50
What happened?
40, reached its correct position.Step 3: Third Pass
Compare the remaining unsorted elements.
| Step | Comparison | Action | Array After Comparison |
|---|---|---|---|
| 1 | 20 and 10 | Swap | 10, 20, 30, 40, 50 |
| 2 | 20 and 30 | No Swap | 10, 20, 30, 40, 50 |
End of Pass 3
Array after the third pass:
10, 20, 30, 40, 50
Step 4: Fourth Pass
Compare the remaining two elements.
| Step | Comparison | Action | Array After Comparison |
|---|---|---|---|
| 1 | 10 and 20 | No Swap | 10, 20, 30, 40, 50 |
End of Pass 4
Final Sorted Array
10, 20, 30, 40, 50
The array is now sorted in ascending order.
#include <iostream>
using namespace std;
int main()
{
int numbers[5] = {50, 20, 40, 10, 30};
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4 - i; j++)
{
if (numbers[j] > numbers[j + 1])
{
int temp = numbers[j];
numbers[j] = numbers[j + 1];
numbers[j + 1] = temp;
}
}
}
cout << "Sorted Array: ";
for (int i = 0; i < 5; i++)
{
cout << numbers[i] << " ";
}
return 0;
}
Bubble Sort uses two loops.
Step 1: Outer loop
for (int i = 0; i < 4; i++)
Step 2: Inner loop
for (int j = 0; j < 4 - i; j++)
Step 3: Compare two elements
if (numbers[j] > numbers[j + 1])
If the left element is greater than the right element, they must be swapped.
Step 4: Understand swapping
int temp = numbers[j];
numbers[j] = numbers[j + 1];
numbers[j + 1] = temp;
Suppose:
numbers[j] = 50numbers[j + 1] = 20The temporary variable stores 50.
Then 20 is placed in the first position, and 50 is placed in the second position.
Swapping example
BEFORE
50
20
AFTER
20
50
#include <iostream>
using namespace std;
int main()
{
int first[7] = {10, 20, 30, 40, 50, 60, 70};
int second[7];
int arr_size = sizeof(first)/sizeof(first[0]);
cout << "Array Size = " << arr_size << endl;
for (int i = 0; i < arr_size; i++)
{
second[i] = first[arr_size-1-i];
}
cout << "Copied Array: ";
for (int i = 0; i < arr_size; i++)
{
cout << second[i] << " ";
}
return 0;
}
for more detail about Array Length, see C++ Array Length — How to Get Array Size with sizeof