What is a built-in C++ array?
A built-in array stores a fixed number of elements of the same type in contiguous memory.
Study 6 Arrays and Collections with 12 free online flashcards. Review key terms, definitions, and concepts with this interactive flashcard deck.
What is a built-in C++ array?
A built-in array stores a fixed number of elements of the same type in contiguous memory.
What are the valid indexes of an array with N elements?
The valid indexes are from `0` through `N - 1`; the last valid index is therefore N−1.
What happens when a C++ array is accessed out of bounds?
Accessing an element outside the array's valid index range produces undefined behavior.
What is `int values[5] = {10, 20};` initialized to?
`values` becomes `{10, 20, 0, 0, 0}` because omitted elements are value-initialized.
How many elements does `int primes[] = {2, 3, 5, 7, 11};` contain?
`primes` has five elements because C++ deduces the bound from the number of initializers.
How many elements does `char word[] = "cat";` contain?
The array contains four characters: `'c'`, `'a'`, `'t'`, and the terminating null character `\0`.
When should a range-based loop use a reference?
A range-based loop visits each element directly; use a reference such as `int& value` when the loop must modify elements.
What is a two-dimensional C++ array?
A two-dimensional array is an array whose elements are one-dimensional arrays, commonly representing rows and columns of a table.
What is `matrix[1][2]` for `{{1,2,3},{4,5,6}}`?
`matrix[1][2]` is `6`: the first subscript selects the second row, and the second selects its third column.
How does a linear search examine an array?
A linear search examines elements sequentially from the beginning until it finds a match or reaches the end.
What is the worst-case complexity of linear search?
Its worst-case time complexity is O(n), because it may inspect every element.
Why cast the sum when calculating an array average?
Cast the sum to `double` before division, such as `static_cast<double>(sum) / count`; otherwise integer division discards the fractional part.