capypad
0 day streak
cpp / beginner
Snippet

Working with Arrays

Arrays store multiple values of the same type in contiguous memory. Access elements using index notation starting from 0. Arrays have fixed size once created.

snippet.cpp
cpp
1
2
3
4
5
6
7
8
#include <iostream>
int main() {
int numbers[] = {10, 20, 30, 40, 50};
for (int i = 0; i < 5; i++) {
std::cout << numbers[i] << std::endl;
}
return 0;
}
Breakdown
1
int numbers[] = {10, 20, 30, 40, 50};
Declare and initialize an array with 5 integer values
2
for (int i = 0; i < 5; i++)
Loop through array indices from 0 to 4
3
numbers[i]
Access element at index i using bracket notation
4
std::cout << numbers[i] << std::endl;
Print each array element on a new line