- C++ Basics
- C++ Introduction
- C++ Installation
- C++ Syntax
- C++ Hello World
- C++ Comments
- C++ Variables
- C++ Data Types
- C++ Constants
- C++ Type Casting
- C++ Inline
- C++ File Inclusion
- C++ Date & Time
- C++ Return Types
- C++ Object Oriented
- C++ Classes
- C++ Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Exceptions
- C++ Advanced
- C++ Conditions
- C++ Loops
- C++ Functions
- C++ Structures
- C++ Enums
- C++ References
- C++ Pointers
- C++ Data Structures
- C++ Libs
- C++ Data Structures
- C++ Arrays
- C++ Vectors
- C++ Lists
- C++ Linked List
- C++ Deque
- C++ Stacks
- C++ Queues
- C++ Priority Queues
- C++ Sets
- C++ Maps
- C++ Unordered Sets and Maps
- C++ Graphs
C++ Arrays
In C++, an array is a collection of elements that are stored in contiguous memory locations. All elements in an array have the same data type, and the size of the array is fixed once it's declared. Arrays are used to store multiple values in a single variable, making it easier to manage groups of related data.
Declaring and Initializing Arrays
You can declare an array by specifying its type, name, and size. The size defines how many elements the array can hold.
Syntax:
cppdata_type array_name[array_size];
data_type: The type of elements stored in the array (e.g.,int,float,char).array_name: The name of the array.array_size: The number of elements the array can hold.
Example:
cppint numbers[5]; // Declares an array of 5 integers
You can also initialize an array at the time of declaration by providing values inside curly braces
{}.
Example:
cppint numbers[5] = {10, 20, 30, 40, 50}; // Declares and initializes the array
If you omit the size, the compiler will automatically determine it based on the number of elements provided.
cppint numbers[] = {10, 20, 30, 40, 50}; // Size automatically determined to be 5
Accessing Array Elements
You can access individual elements of an array using the array index. Array indices start from
0, meaning the first element has index 0, the second has index 1, and so
on.
Syntax:
cpparray_name[index]
Example:
cpp#include
using namespace std;
int main() {
int numbers[5] = {10, 20, 30, 40, 50};
cout << "First element: " << numbers[0] << endl;
cout << "Third element: " << numbers[2] << endl;
return 0;
}
Output:
yamlFirst element: 10
Third element: 30
You can also modify the value of an array element using its index.
cppnumbers[0] = 100; // Changes the first element to 100
Looping Through Arrays
You can use loops, such as for or while, to iterate through all elements in an array.
Example:
cpp#include
using namespace std;
int main() {
int numbers[5] = {10, 20, 30, 40, 50};
// Loop through the array using a for loop
for (int i = 0; i < 5; i++) {
cout << "Element at index " << i << ": " << numbers[i] << endl;
}
return 0;
}
Output:
mathematicaElement at index 0: 10
Element at index 1: 20
Element at index 2: 30
Element at index 3: 40
Element at index 4: 50
Multidimensional Arrays
C++ also supports multidimensional arrays, where an array can contain more than one row and column. The most common type of multidimensional array is the 2D array, which is essentially an array of arrays.
Declaring a 2D Array:
cppdata_type array_name[rows][columns];
Example:
cppint matrix[3][3]; // Declares a 2D array with 3 rows and 3 columns
You can also initialize a 2D array when declaring it.
cppint matrix[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
Accessing Elements in a 2D Array:
To access elements in a 2D array, you need to provide two indices: one for the row and one for the column.
cppcout << matrix[1][2]; // Accesses the element in the second row, third column (which is 6)
Example of Iterating Over a 2D Array:
cpp#include
using namespace std;
int main() {
int matrix[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
// Loop through the 2D array
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cout << matrix[i][j] << " ";
}
cout << endl;
}
return 0;
}
Output:
1 2 3 4 5 6 7 8 9
Array Size
You can use the sizeof operator to determine the size of an array in bytes. To get the number of
elements, you need to divide the total size of the array by the size of an individual element.
Example:
cpp#include
using namespace std;
int main() {
int numbers[5] = {10, 20, 30, 40, 50};
int totalSize = sizeof(numbers); // Total size in bytes
int elementSize = sizeof(numbers[0]); // Size of one element in bytes
int numberOfElements = totalSize / elementSize; // Number of elements
cout << "Array size: " << numberOfElements << endl;
return 0;
}
Output:
arduinoArray size: 5
C++ Arrays and Functions
You can pass an array to a function by specifying the array name without square brackets []. When
you pass an array to a function, it actually passes a pointer to the array's first element.
Example:
cpp#include
using namespace std;
// Function that takes an array and its size as parameters
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
int main() {
int numbers[5] = {10, 20, 30, 40, 50};
// Pass the array to the function
printArray(numbers, 5);
return 0;
}
Output:
10 20 30 40 50
Limitations of Arrays
- Fixed size: Once declared, the size of an array cannot be changed.
- No bounds checking: Accessing an element outside the array's size can lead to undefined behavior.
- Limited functionality: Arrays are basic data structures with no advanced functionality like resizing, inserting, or deleting elements (unlike other data structures like vectors).
Summary
- Arrays are used to store multiple values of the same type in contiguous memory locations.
- Array elements can be accessed and modified using their indices.
- Multidimensional arrays are arrays of arrays, commonly used for matrices or tables.
- Arrays can be passed to functions, allowing you to work with large sets of data efficiently.