C++ Arrays
An array is used when you need to store multiple values of the same data type. Instead of creating a separate variable for every value, you can store all the values under one name.
For example, if you need to store the marks of five students, you can use one array instead of creating five variables.
int marks[5] = {75, 82, 90, 68, 88};
Here, marks is the array name and 5 tells C++ that the array can hold five integer values.
Array in C++
The basic syntax for creating an array is:
dataType arrayName[size];
For example:
int numbers[5];
This creates an integer array named numbers that can store five values.
You can also assign values while creating the array:
int numbers[5] = {10, 20, 30, 40, 50};
One important thing to remember is that array indexing starts from 0.
So, the positions of the above values are:
Index: 0 1 2 3 4
Value: 10 20 30 40 50
To access a value, write the array name followed by its index.
cout << numbers[2];
Output
30
numbers[2] gives the third value because the first value is at index 0.
One Dimensional Array in C++
A One Dimensional Array in C++ stores values in a single list. It is useful for storing things like marks, prices, ages, or a list of numbers.
Example
int marks[5] = {75, 82, 90, 68, 88};
cout << marks[0] << endl;
cout << marks[3];
Output
75
68
You can use a for loop to access all the elements instead of writing separate statements.
int marks[5] = {75, 82, 90, 68, 88};
for (int i = 0; i < 5; i++)
{
cout << marks[i] << endl;
}
Output
75
82
90
68
88
Here, the loop starts with index 0 and continues until index 4.
Two Dimensional Array in C++
A Two Dimensional Array in C++ is useful when you want to store data in rows and columns. You can think of it like a table.
Syntax
dataType arrayName[rows][columns];
For example:
int numbers[2][3] = {
{10, 20, 30},
{40, 50, 60}
};
The array contains two rows and three columns.
You can imagine it like this:
10 20 30
40 50 60
To access an element, you need to provide both its row and column index.
cout << numbers[1][2];
Output
60
Here, 1 represents the second row and 2 represents the third column. Remember that both row and column indexing starts from 0.
You can also use nested loops to display all the elements:
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
cout << numbers[i][j] << " ";
}
cout << endl;
}
Output
10 20 30
40 50 60
The outer loop moves through the rows, while the inner loop moves through the columns.
Arrays make it easier to store and work with multiple related values. Once you understand how indexes work, you can use arrays with loops to perform different operations on stored data.
Frequently Asked Questions (FAQs)
Q1. What is an array in C++?
An array in C++ is a collection of elements of the same data type stored in consecutive memory locations. It allows multiple values to be stored under one variable name.
Q2. How do I declare an array in C++?
You can declare an array by specifying its data type, name, and size, such as int numbers[5];. This creates an array that can store five integer values.
Q3. What is a one-dimensional array in C++?
A One Dimensional Array in C++ stores elements in a single sequence and uses one index to access each value, such as numbers[0], numbers[1], and so on.
Q4. What is a two-dimensional array in C++ used for?
A Two Dimensional Array in C++ stores data in rows and columns. It is commonly used for tables, matrices, grids, and other structured data.
Q5. What is the difference between one-dimensional and two-dimensional arrays in C++?
A one-dimensional array stores values in a single sequence, while a two-dimensional array organizes values using rows and columns.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
