Chapter 7 Flashcards
Array
allows you to store and work with multiple values of the same data type
write an array definition
int array[2];
an array’s size declarator
indicates the number of elements, or values, the array can hold.
int array[sizeDeclarator]; int days[7];
can be a literal or named constant
Accessing Array Elements
The individual elements of an array are assigned unique subscripts(number).
These subscripts are used as an index to access a specific element.
intialize an array
string days[7] = {"Mon", "Tues", "Wed", "Thurs", "Fri", "Sat", "Sun"};
Is it possible to initialize an array without every element containing a value?
Yes, C++ allows partial array initialization.
int number[7] = {1, 2, 3, 4};
implicit array sizing
it is possible to define an array without specifying its size, as long as you provide an initialization list
int numbers[] = { 2, 5, 6, 9 };
What is “array bounds checking”? Does C++ perform it?
There are no bounds checking in C++. there is no safeguard to prevent unsafe memory access found in other programs. Must always make sure all values assigned to array element are written with the array’s boundaries.
range-based for loop
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 variable.
you do not have access to the array element subscripts in a range-based for loop
range variable
built-in variable for a range-based loop. each time the range-based loop iterates, it copies an array element to the range variable.
for (dataType rangeVariable : arrayName) { statement; }
how does one modify an array with a range-based for loop?
by making the range variable a reference variable
for (dataType & rangeVariable : arrayName) { statement; }
Why can you not assign an array?
int oldValue[2], newValue[2] = { 1, 2 }; newValue = oldValue;
Anytime the name of an array is used without brackets or subscript it is seen as the array’s beginning memory address. You cannot change the starting memory address of an array.
What will be displayed from this statement?
const int SIZE = 5; int numbers [SIZE] = {10, 20, 30, 40, 50}; cout << numbers << endl;
the array named numbers’ beginning memory address
structured binding declarations
allow you to write one statement that defines a set of variables and initializes those variables with the values that are stored in an array. this process is known as unpacking the array.
number of elements listed in square brackets must be the same as the number of elements in the array. Can only be used if the size of the array is known.
auto [var1, var2, var3] = arrayName;
What will be displayed from this statement?
const int SIZE = 5; int numbers [SIZE] = {10, 20, 30, 40, 50}; cout << numbers[1] << endl;
the array named numbers’ beginning memory address
parallel arrays
using the same subscript you can build relationships between data stored in two or more arrays
Given the following array definition:int values[] = {2, 6, 10, 14};
What does each of the following display?B) cout << ++values[0];
prints the sum of 1 + the first element in array values
how do you pass an array as an argument in a function?
to pass an array as an argument to a function, pass the name of the array. this sets the starting memory address of the array as the argument
Write all parts of a function and pass in an entire array as an argument.
void func(int [], int); // function prototype func(arrayName[], SIZE); // function call void func(int num[], int size) { // function definition statement; }
What is num[] in the below function definition?
void func(int num[], int size) { // function definition statement;
}
num[] is not an array. It’s a special variable that can accept the address of an array. When an entire array is passed to a function, it is not passed by value, but passed by reference.
What is numbers in the function call below?
int numbers[]; `showValues(numbers, ARRAY_SIZE); // function call`
number is not an array. An array without bracket or subscript is the starting memory address of the array.
numbers[] // array numbers // starting memory address
Can you edit the original contents of an array within a function?
Yes, array parameters work similar to reference variables. They give the function direct access to the original array.
Can you use Structured Binding Declarations with Array Arguments?
No, since the array argument uses the starting memory address of the array the function does not know the size of the array. Without knowing the size of the array you cannot use structured binding declarations.
How can you prevent an array argument from being changed?
declare the array argument as a constant variable. If anything attempts to edit the const array argument an error will occur.
void func(const int arrayArgument[], int size) { statement }; // function definition
two dimensional arrays
several identical arrays put together. Useful for storing multiple sets of data.
double score[rowSizeDeclarator][columnSizeDeclarator];
intialize a 2D array
dataType arrayName[rowSizeDeclarator][columnSizeDeclarator] =
{{r0c0, r0c1},
{r1c0, r1c1},
{r2c0, r2c1}};
2D arrays and functions
must include column size declarator for function prototype and function header
void func(int [][col]) void func(int array[][col])
how do you add an element to a vector array?
use the member function push_back()
adds an elements into a vector array that has no size declared or has reached the size limit.
vectorArray.push_back(value);
how to you get the size of a vector array?
use the member function size
vectorArray.size();
how do you remove an element in a vector array?
use the member function pop_back() to remove the last element in a vector array
vectorArray.pop_back();
define a vector array.
#include <vector> using namespace;
vector<int> numbers;
// no size neededvector<int> numbers(startSize);
// optional
define a vector array with a starting size and initialized values
vector<int> numbers(startSize, initValue);
// optional
define a vector that is populated from another vector
vector<int> numbers1(numbers2);
// init with another vector