Beginner-friendly guide to C++ one-dimensional arrays: declaration, initialization, indexing, accessing elements, loops, and practical examples.
An array is a collection of multiple values of the same data type stored in continuous memory locations under one variable name.
👉 Instead of creating many variables, we use an array.
int marks[5];
This stores 5 integer values.
A one-dimensional array stores data in a single line (row).
📌 Syntax:
data_type array_name[size];
Declaration means reserving memory for the array.
int numbers[5];
📌 This creates an array named numbers that can store 5 integers.

Initialization means assigning values to the array.
int numbers[5] = {10, 20, 30, 40, 50};
int numbers[] = {5, 10, 15, 20};
int numbers[3];
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
size - 1| Index | Value |
|---|---|
| 0 | First element |
| 1 | Second element |
| 2 | Third element |
We use index number to access elements.
int numbers[5] = {10, 20, 30, 40, 50};
cout << numbers[0]; // Output: 10
cout << numbers[3]; // Output: 40
#include <iostream>
using namespace std;
int main() {
int numbers[5]; // Array declaration
// Assigning values to individual elements
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;
}
int numbers[5]; → creates an array of size 5numbers[0] = 10; → assigns 10 to the first elementnumbers[1] = 20; → assigns 20 to the second element| Index | Value |
|---|---|
| 0 | 10 |
| 1 | 20 |
| 2 | 30 |
| 3 | 40 |
| 4 | 50 |
for LoopThe for loop is best for accessing 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;
}
📌 i represents the index number.
Store and display marks of 5 students.
#include <iostream>
using namespace std;
int main() {
int marks[5] = {78, 85, 90, 66, 88};
for(int i = 0; i < 5; i++) {
cout << "Student " << i+1 << " Marks: " << marks[i] << endl;
}
return 0;
}
Find sum of array values.
#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;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int marks[5]; // Declare an array of size 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;
}
1️⃣ Array Declaration
int marks[5];
int → data typemarks → array name5 → size (number of elements)2️⃣ Taking Input Using for Loop
for(int i = 0; i < 5; i++) {
cin >> marks[i];
}
i is the index3️⃣ Displaying Array Values
for(int i = 0; i < 5; i++) {
cout << marks[i] << endl;
}
📊 How Data is Stored
| Index | Value |
|---|---|
| 0 | marks of student 1 |
| 1 | marks of student 2 |
| 2 | marks of student 3 |
| 3 | marks of student 4 |
| 4 | marks of student 5 |
💡 Problem Statement
Store 5 numbers in an array and calculate their sum.
#include <iostream>
using namespace std;
int main() {
int numbers[5];
int sum = 0;
// Taking input
cout << "Enter 5 numbers:" << endl;
for(int i = 0; i < 5; i++) {
cin >> numbers[i];
}
// Calculating sum
for(int i = 0; i < 5; i++) {
sum = sum + numbers[i];
}
// Displaying result
cout << "Sum of numbers = " << sum << endl;
return 0;
}
1️⃣ Array Declaration
int numbers[5];
Creates an array to store 5 integers.
2️⃣ Input Using Loop
numbers[0] to numbers[4]
3️⃣ Sum Calculation
sum = sum + numbers[i];
Adds each element to sum.
📊 Example Input / Output
Input:
10 20 30 40 50
Output:
Sum of numbers = 150