Suppose you want to store the marks of five students. You could create five different variables:
int mark1 = 80;
int mark2 = 75;
int mark3 = 90;
int mark4 = 85;
int mark5 = 70;
This works, but it becomes difficult to manage when you have many values.
C++ arrays store multiple values of the same data type in one variable.
What is an Array?
An array is a collection of multiple values of the same data type stored under one name.
For example:
int marks[5] = {80, 75, 90, 85, 70};
Here:
intis the data type.marksis the array name.[5]means the array can store 5 values.- The numbers inside
{ }are the values.
Creating an Array
The basic syntax is:
dataType arrayName[size];
For example:
int numbers[5];
This creates an integer array that can store 5 values.
You can also give values when creating the array:
int numbers[5] = {10, 20, 30, 40, 50};
Accessing Array Elements
Array elements are accessed using an index.
An important rule is that array indexing starts from 0, not 1.
For example:
Index: 0 1 2 3 4
Value: 10 20 30 40 50
So:
numbers[0]
contains 10, while:
numbers[4]
contains 50.
Example:
#include <iostream>
using namespace std;
int main() {
int numbers[5] = {10, 20, 30, 40, 50};
cout << numbers[0] << "\n";
cout << numbers[2] << "\n";
cout << numbers[4];
return 0;
}
Output:
10
30
50
Changing an Array Element
You can change the value of a particular element using its index.
#include <iostream>
using namespace std;
int main() {
int numbers[3] = {10, 20, 30};
numbers[1] = 50;
cout << numbers[1];
return 0;
}
Output:
50
The value at index 1 was originally 20, but we changed it to 50.
Looping Through an Array
Loops make it easy to access every element of an array.
#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] << "\n";
}
return 0;
}
Output:
10
20
30
40
50
The loop starts from index 0 and continues until index 4.
Taking Array Input
You can also ask the user to enter the values.
#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] << "\n";
}
return 0;
}
Example input:
10
20
30
40
50
Output:
You entered:
10
20
30
40
50
Finding the Sum of Array Elements
Arrays become especially useful when combined with calculations.
#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 += numbers[i];
}
cout << "Sum: " << sum;
return 0;
}
Output:
Sum: 150
The loop visits each element and adds it to sum.
Multidimensional Arrays
C++ also allows you to create arrays with more than one dimension.
A two-dimensional array can be used to store data in rows and columns.
For example:
#include <iostream>
using namespace std;
int main() {
int numbers[2][3] = {
{10, 20, 30},
{40, 50, 60}
};
cout << numbers[0][1] << "\n";
cout << numbers[1][2];
return 0;
}
Output:
20
60
Two-dimensional arrays are useful for tables, grids, and similar data.
A Simple Example
Let’s create a program to find the largest number in an array.
#include <iostream>
using namespace std;
int main() {
int numbers[5] = {25, 10, 45, 30, 15};
int largest = numbers[0];
for (int i = 1; i < 5; i++) {
if (numbers[i] > largest) {
largest = numbers[i];
}
}
cout << "Largest number: " << largest;
return 0;
}
Output:
Largest number: 45
Key Points to Remember
- An array stores multiple values of the same data type.
- Array indexing starts from
0. - The first element is at index
0. - You can access an element using its index.
- Loops are useful for working with arrays.
- Arrays can have one or more dimensions.
- The size of a basic array is fixed when it is created.
Mini Quiz
Mini Project
Mini Project: Student Marks Analyzer
Question:
Create a program that:
- Stores marks of 5 students in an array
- Displays all marks
- Calculates the total marks
- Calculates the average marks
- Finds the highest mark
View Answer Code
#include <iostream>
using namespace std;
int main() {
int marks[5] = {80, 75, 90, 85, 70};
int total = 0;
int highest = marks[0];
cout << "Student Marks:\n";
for (int i = 0; i < 5; i++) {
cout << marks[i] << "\n";
total += marks[i];
if (marks[i] > highest) {
highest = marks[i];
}
}
double average = total / 5.0;
cout << "\nTotal: " << total << "\n";
cout << "Average: " << average << "\n";
cout << "Highest Mark: " << highest;
return 0;
}
What’s Next?
In the next chapter, we will learn about C++ Strings and see how to work with names, sentences, and other text in C++.
Frequently Asked Questions (FAQs)
Q1. What are C++ Arrays?
C++ Arrays are collections that store multiple values of the same data type under a single name.
Q2. How do you declare a C++ array?
A C++ array declaration includes the data type, array name, and size. For example, int numbers[5]; creates an array that can store five integers.
Q3. What is C++ array indexing?
C++ array indexing is used to access individual elements. Indexing starts from 0, so the first element is at index 0 and the second is at index 1.
Q4. How can you access elements in a C++ array?
You can access an element using its index. For example, numbers[0] accesses the first element of a C++ array.
Q5. Can C++ arrays store different data types?
No. A basic C++ array stores elements of the same data type. For example, an int array stores integer values.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
