An array is a fundamental data structure in computer programming that allows you to store a collection of elements of the same data type in contiguous memory locations. Each element in the array is identified by an index, which represents its position within the array. Arrays provide a way to efficiently access, store, and manipulate a group of related data elements.
Table of Contents
Key characteristics of an array:
- Homogeneous: All elements in an array must be of the same data type (e.g., integers, characters, floats, etc.).
- Fixed Size: Arrays have a fixed size, meaning the number of elements they can hold is determined at the time of declaration and cannot be changed during runtime.
- Contiguous Memory: The elements in an array are stored in adjacent memory locations, allowing for direct and fast access to any element using its index.
- Zero-based Indexing: In most programming languages, including C++, array indices start from 0. So, the first element is accessed using index 0, the second with index 1, and so on.
Arrays are commonly used for various tasks, such as:
- Storing a list of items, like a list of student names, scores, or employee IDs.
- Representing matrices and multi-dimensional data structures.
- Implementing other data structures like stacks, queues, and hash tables.
Array in C++
In C++, an array is a collection of elements of the same data type, stored in contiguous memory locations. Each element in the array is accessed using an index, which represents its position in the array. The index starts from 0 for the first element and goes up to (size – 1) for the last element. Arrays in C++ have a fixed size that must be known at compile-time.
Let’s look at some examples to better understand arrays in C++:
Example 1: Declaring and Accessing Elements in an Array
#include <iostream>
int main() {
// Declare an array of integers with size 5
int myArray[5];
// Assign values to the elements of the array
myArray[0] = 10;
myArray[1] = 20;
myArray[2] = 30;
myArray[3] = 40;
myArray[4] = 50;
// Access and print the elements of the array
std::cout << "Array Elements: ";
for (int i = 0; i < 5; i++) {
std::cout << myArray[i] << " ";
}
std::cout << std::endl;
return 0;
}
Explanation:
- We include the necessary header file
#include <iostream>
for input/output operations. - We declare an array of integers named
myArray
with a size of 5 elements using the syntaxint myArray[5];
. - We assign values to each element of the array using the array index, e.g.,
myArray[0] = 10;
,myArray[1] = 20;
, and so on. - We use a loop to access and print the elements of the array. The loop runs from 0 to 4 (size – 1) using the index
i
, and we print each element withstd::cout << myArray[i] << " ";
.
Output:
Array Elements: 10 20 30 40 50
Example 2: Initializing an Array with Values
#include <iostream>
int main() {
// Initialize an array of integers with values directly
int myArray[5] = {10, 20, 30, 40, 50};
// Access and print the elements of the array
std::cout << "Array Elements: ";
for (int i = 0; i < 5; i++) {
std::cout << myArray[i] << " ";
}
std::cout << std::endl;
return 0;
}
Explanation:
- In this example, we initialize the array
myArray
directly with values using curly braces{}
at the time of declaration. - The compiler automatically determines the size of the array based on the number of elements provided inside the curly braces.
Output:
Array Elements: 10 20 30 40 50
Arrays are commonly used when you know the size of the data in advance and want efficient random access to elements. However, be cautious about the fixed size limitation, as dynamically resizing an array in C++ requires more complex data structures like vectors or dynamic memory allocation.