cpp / beginner
Snippet
Arrays: Storing Multiple Values
Arrays store multiple values of the same type in contiguous memory. The size must be known at compile time for fixed arrays. Access elements using index notation starting from 0. Arrays have fixed size - once created, they cannot grow or shrink.
snippet.cpp
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>int main() {int scores[5] = {90, 85, 77, 92, 88};for (int i = 0; i < 5; i++) {std::cout << "Score " << i << ": " << scores[i] << std::endl;}return 0;}
Breakdown
1
int scores[5]
Declares array of 5 integers
2
= {90, 85, 77, 92, 88}
Initializes with 5 values in braces
3
scores[i]
Accesses element at index i (0-based)