Arrays in C++
What are arrays?
An array is a collection of multiple values stored in a single variable.
Declaring an Array
This is how you create an array:
int numbers[5] = {1, 2, 3, 4, 5};
Accessing Array Elements
Array elements are accessed using an index (starting from 0):
#include <iostream>
using namespace std;
int main() {
int numbers[3] = {10, 20, 30};
cout << "First element: " << numbers[0] << endl; // Output: 10
return 0;
}
Looping Through an Array
You can use a loop to go through all elements:
#include <iostream>
using namespace std;
int main() {
int numbers[5] = {10, 20, 30, 40, 50};
for (int i = 0; i < 5; i++) {
cout << "Element " << i << ": " << numbers[i] << endl;
}
return 0;
}