1/11
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
|---|
No study sessions yet.
Array
A variable that can story multiple values of the same type
values are stored in adjacent memory locations
declared using [ ] operator
int tests[5];
the size of an array is the total number of bytes allocated for it: (number of elements) * (size of each element)
Accessing Array Elements
each element in an array is assigned a unique subscript (starting at 0 to n - 1)
array elements can be used as regular variables
arrays must be accessed via individual elements
can access element with a constant or literal subscript
can use integer expression as subscript

Using a Loop to Step Through an Array

Array Initialization
global array ➡ all elements initialized to 0 by default
local array ➡ all elements uninitialized by default
arrays can be initialized with an initialization list
int tests[5] = {79, 82, 91, 77, 84};
you can also determine the array size by the size of the initialization list
No Bounds Checking in C++
when you use a value as an array subscript, C++ does not check it to make sure it is a valid subscript
you can use subscripts beyond the bounds of the array, doing so can corrupt other memory locations, crash program, lock up computer, cause elusive bugs…BE CAREFUL!
The Range-Based for Loop
C++ 11
a loop that iterates once for each element in an array
each time the loop iterates, it copies an element from the array to a built-in variable, known as the range variable
the range-based for loop automatically knows the number of elements in an array, you do not have to:
use a counter variable
worry about stepping outside the bounds of the array
can be used in any situation where you do not need to use the element subscripts

Copying one array to another
assign element-by-element

Printing an array
print element-by-element

Structured Binding Declarations
defines a set of variables and initialized them with the values that are stored in an array
auto [variable1, variable2, etc…] = array;
![<p>defines a set of variables and initialized them with the values that are stored in an array</p><ul><li><p><strong>auto [variable1, variable2, etc…] = array;</strong></p></li></ul><p></p>](https://knowt-user-attachments.s3.amazonaws.com/d8c82664-6f46-403e-9b32-abc78ed1c38c.png)
Parallel Arrays
two or more arrays that contain related data
a subscript is used to relate arrays (elements at the same subscript are related)
arrays may be of different types
Arrays as Function Arguments
to pass an array to a function, use the array name
showScores(tests);
to define a function that takes an array parameter, use empty [] for array argument
void showScores(int []);
it is common to pass array size so that the function knows how many elements to process
showScores(tests, ARRAY_SIZE);
array size must be reflected in prototype header
Two-Dimensional Arrays
First declarator is number of rows, second is number of columns.
const int ROWS = 4, COLS = 3;
int exams[ROWS][COLS]
![<p>First declarator is number of rows, second is number of columns.</p><ul><li><p><strong>const int ROWS = 4, COLS = 3;</strong></p></li><li><p><strong>int exams[ROWS][COLS]</strong></p></li></ul><p></p>](https://knowt-user-attachments.s3.amazonaws.com/68a7d2ce-ebff-4e91-8235-571378f3f148.png)