capypad
0 day streak
cpp / beginner
Snippet

Fixed-Size Arrays: Storing Multiple Values

Arrays store multiple values of the same type in consecutive memory locations. The syntax int scores[5] creates an array that holds 5 integers. Access each element using brackets with an index (starting at 0). Unlike vectors, fixed arrays cannot change size after creation.

snippet.cpp
cpp
1
2
3
4
5
6
7
8
#include <iostream>
int main() {
int scores[5] = {95, 87, 92, 78, 88};
for (int i = 0; i < 5; i++) {
std::cout << "Score " << i << ": " << scores[i] << std::endl;
}
return 0;
}
Breakdown
1
int scores[5] = {95, 87, 92, 78, 88}
Declares array of 5 integers and initializes with values
2
scores[i]
Accesses element at index i (0-based: scores[0] is 95)