Chapter 7 Flashcards

1
Q

Array

A

allows you to store and work with multiple values of the same data type

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

write an array definition

A

int array[2];

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

an array’s size declarator

A

indicates the number of elements, or values, the array can hold.

int array[sizeDeclarator];
int days[7];

can be a literal or named constant

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Accessing Array Elements

A

The individual elements of an array are assigned unique subscripts(number).
These subscripts are used as an index to access a specific element.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

intialize an array

A

string days[7] = {"Mon", "Tues", "Wed", "Thurs", "Fri", "Sat", "Sun"};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Is it possible to initialize an array without every element containing a value?

A

Yes, C++ allows partial array initialization.

int number[7] = {1, 2, 3, 4};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

implicit array sizing

A

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 };

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is “array bounds checking”? Does C++ perform it?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

range-based for loop

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

range variable

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

how does one modify an array with a range-based for loop?

A

by making the range variable a reference variable

for (dataType & rangeVariable : arrayName) { statement; }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Why can you not assign an array?

int oldValue[2], newValue[2] = { 1, 2 };
newValue = oldValue;
A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What will be displayed from this statement?

const int SIZE = 5;
int numbers [SIZE] = {10, 20, 30, 40, 50};
cout << numbers << endl;
A

the array named numbers’ beginning memory address

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

structured binding declarations

A

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;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What will be displayed from this statement?

const int SIZE = 5;
int numbers [SIZE] = {10, 20, 30, 40, 50};
cout << numbers[1] << endl;
A

the array named numbers’ beginning memory address

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

parallel arrays

A

using the same subscript you can build relationships between data stored in two or more arrays

17
Q

Given the following array definition:
int values[] = {2, 6, 10, 14};
What does each of the following display?
B) cout << ++values[0];

A

prints the sum of 1 + the first element in array values

18
Q

how do you pass an array as an argument in a function?

A

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

19
Q

Write all parts of a function and pass in an entire array as an argument.

A
void func(int [], int); // function prototype
func(arrayName[], SIZE); // function call
void func(int num[], int size) { // function definition
  statement; }
20
Q

What is num[] in the below function definition?

void func(int num[], int size) { // function definition
  statement; 

}

A

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.

21
Q

What is numbers in the function call below?

int numbers[];
`showValues(numbers, ARRAY_SIZE); // function call`
A

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
22
Q

Can you edit the original contents of an array within a function?

A

Yes, array parameters work similar to reference variables. They give the function direct access to the original array.

23
Q

Can you use Structured Binding Declarations with Array Arguments?

A

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.

24
Q

How can you prevent an array argument from being changed?

A

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

25
Q

two dimensional arrays

A

several identical arrays put together. Useful for storing multiple sets of data.

double score[rowSizeDeclarator][columnSizeDeclarator];

26
Q

intialize a 2D array

A

dataType arrayName[rowSizeDeclarator][columnSizeDeclarator] = {{r0c0, r0c1}, {r1c0, r1c1}, {r2c0, r2c1}};

27
Q

2D arrays and functions

A

must include column size declarator for function prototype and function header

void func(int [][col])
void func(int array[][col])
28
Q

how do you add an element to a vector array?

A

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);

29
Q

how to you get the size of a vector array?

A

use the member function size

vectorArray.size();

30
Q

how do you remove an element in a vector array?

A

use the member function pop_back() to remove the last element in a vector array

vectorArray.pop_back();

31
Q

define a vector array.

A
#include <vector>
using namespace;

vector<int> numbers; // no size needed
vector<int> numbers(startSize); // optional

32
Q

define a vector array with a starting size and initialized values

A

vector<int> numbers(startSize, initValue); // optional

33
Q

define a vector that is populated from another vector

A

vector<int> numbers1(numbers2); // init with another vector