An array is a collection of elements of the same type, stored in contiguous memory locations. In C++, arrays allow you to store multiple values of the same type in a single variable, making it easier to work with large amounts of data.
Key Points:
- Arrays in C++ are zero-indexed, meaning the first element is accessed with index
0
. - Arrays can be of any data type (integers, floats, strings, etc.).
- The size of an array is fixed when it is declared and cannot be changed after initialization.
1️⃣ Declaring and Initializing Arrays in C++
Syntax:
dataType arrayName[arraySize];
Where:
dataType
is the type of elements in the array (e.g.,int
,float
,char
).arrayName
is the name of the array.arraySize
is the number of elements the array will hold.
Example: Declaring and Initializing an Integer Array
#include <iostream>
using namespace std;
int main() {
// Declare an array of integers with 5 elements
int numbers[5] = {1, 2, 3, 4, 5};
// Accessing and printing array elements
for (int i = 0; i < 5; i++) {
cout << "Element " << i + 1 << ": " << numbers[i] << endl;
}
return 0;
}
Explanation:
int numbers[5]
declares an array of 5 integers.- The array is initialized with the values
{1, 2, 3, 4, 5}
. - We use a
for
loop to access and print each element.
Output:
Element 1: 1
Element 2: 2
Element 3: 3
Element 4: 4
Element 5: 5
2️⃣ Accessing Array Elements
Each element in an array can be accessed using its index. The first element is accessed using index 0
, the second element with index 1
, and so on.
Example: Accessing Array Elements by Index
#include <iostream>
using namespace std;
int main() {
int numbers[] = {10, 20, 30, 40, 50};
// Accessing elements using indices
cout << "First element: " << numbers[0] << endl;
cout << "Second element: " << numbers[1] << endl;
cout << "Third element: " << numbers[2] << endl;
cout << "Fourth element: " << numbers[3] << endl;
cout << "Fifth element: " << numbers[4] << endl;
return 0;
}
Explanation:
- We use the index inside square brackets (
numbers[index]
) to access each element. - The program outputs each element in the array by its respective index.
Output:
First element: 10
Second element: 20
Third element: 30
Fourth element: 40
Fifth element: 50
3️⃣ Multidimensional Arrays
C++ supports multidimensional arrays, which are arrays of arrays. The most common example is the 2D array.
Syntax for 2D Array:
dataType arrayName[rowSize][columnSize];
Example: 2D Array
#include <iostream>
using namespace std;
int main() {
// Declare and initialize a 2D array (3x3 matrix)
int matrix[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
// Accessing and printing elements of a 2D array
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cout << matrix[i][j] << " ";
}
cout << endl;
}
return 0;
}
Explanation:
- The 2D array
matrix[3][3]
is initialized with values in rows and columns. - We use two nested
for
loops to access and print each element in the 2D array.
Output:
1 2 3
4 5 6
7 8 9
4️⃣ Array Size
The size of an array is fixed when it's declared. However, in C++, you can use the sizeof
operator to determine the total size of the array in bytes.
Example: Finding Array Size
#include <iostream>
using namespace std;
int main() {
int numbers[] = {10, 20, 30, 40, 50};
// Find the total size of the array in bytes
int size = sizeof(numbers) / sizeof(numbers[0]); // Total size divided by size of one element
cout << "Number of elements in the array: " << size << endl;
return 0;
}
Explanation:
- The expression
sizeof(numbers)
gives the total size of the array in bytes. sizeof(numbers[0])
gives the size of one element in the array.- By dividing the total size by the size of one element, we get the number of elements in the array.
Output:
Number of elements in the array: 5
5️⃣ Array Input from User
You can also take input from the user to populate an array.
Example: User Input for Array
#include <iostream>
using namespace std;
int main() {
int numbers[5];
// Taking input for each element of the array
cout << "Enter 5 numbers: " << endl;
for (int i = 0; i < 5; i++) {
cin >> numbers[i]; // Input each element
}
// Display the entered numbers
cout << "You entered: ";
for (int i = 0; i < 5; i++) {
cout << numbers[i] << " ";
}
cout << endl;
return 0;
}
Explanation:
- The user is prompted to input 5 numbers, which are stored in the
numbers
array. - The program then prints out the entered numbers.
Output (Example when the user enters 1 2 3 4 5
):
Enter 5 numbers:
1
2
3
4
5
You entered: 1 2 3 4 5
6️⃣ Key Points to Remember:
- Array size is fixed at the time of declaration.
- Arrays are zero-indexed in C++, meaning the first element has an index of 0.
- You can use multidimensional arrays for matrices or tables.
- You can calculate the size of an array using the
sizeof
operator. - Arrays can be initialized during declaration, and you can also take user input to populate them.
Would you like to explore dynamic arrays, array sorting, or array functions in C++?
0 Comments