Chapter 7 Arrays and Vectors

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

What is the difference between an array's size declarator and a subscript?

The size declarator is used in the array declaration statement. It specifies the number of elements in the array. A subscript is used to access an individual element in an array.

Is each of the following a valid or invalid array definition? (If a definition is invalid, explain why.) int numbers[10] = {0, 0, 1, 0, 0, 1, 0, 0, 1, 1};

Valid

a vector offers several advantages over arrays. Here are just a few:

You do not have to declare the number of elements that the vector will have. If you add a value to a vector that is already full, the vector will automatically increase its size to accommodate the new value. vectors can report the number of elements they contain.

Remember, in C++ the name of an array without brackets and a subscript is actually the beginning _____ of the array.

address

Remember, in C++ the name of an array without brackets and a subscript is actually the beginning ________ of the array.

address

To declare the range variable as a reference variable, simply write an _________ in front of its name in the loop header.

ampersand (&)

You may also initialize a vector with the values in _______ vector.

another

An ____ works like a variable that can store a group of values, all of the same type. The values are stored together in consecutive memory locations.

array

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

array

The amount of memory used by an array depends on the ____ ___ _____ and the number of elements.

array's data type

An --- -- - ---- must be a constant integer expression with a value greater than zero.

array's size declarator

The extra _______ that enclose each row's initialization list are optional.

braces

When initializing a two-dimensional array, it helps to enclose each row's initialization list in a set of ______.

braces

important to understand. Anytime the name of an array is used without______ ___ ___ ____, it is seen as the array's beginning memory address.

brackets and a subscript

The range-based for loop is designed to work with a_________________ Each time the range-based for loop iterates, it copies an array element to the range variable.

built-in variable known as the range variable.

An array's initialization list _______ have more values than the array has elements.

cannot

C++ does not perform array bounds ________. This means you can write programs with subscripts that go beyond the boundaries of a particular array.

checking

C++ requires the _________ to be specified in the function prototype and header because of the way two-dimensional arrays are stored in memory.

columns

When a two-dimensional array is passed to a function, the parameter type must contain a size declarator for the number of ___________.

columns

When the compiler generates code for accessing the elements of a two-dimensional array, it needs to know how many bytes separate the rows in memory. The number of ____________ is a critical factor in this calculation.

columns

When using increment and decrement operators, be careful not to _______ the subscript with the array element.

confuse

you can prevent a function from making changes to an array argument by using the _____ ____ word in the parameter declaration.

const key

As the range-based for loop executes, its range variable contains only a _______ of an array element.

copy

Because the range-based for loop automatically knows the number of elements in an array, you do not have to use a _____ variable to control its iterations, as with a regular for loop.

counter

Here is a definition of an array of integers: int days[6]; The name of this array is _______. The number inside the brackets is the ___ ____ ____.

days, array's size declarator

To define a two-dimensional array, two size _________ are required. The first one is for the number of rows, and the second one is for the number of columns.

declarators

C++ ________ limit the number of dimensions that an array may have. It is possible to create arrays with multiple dimensions, to model data that occur in multiple sets.

does not

When an array is being initialized, C++ _______ ______ require a value for every element. It's possible to only initialize part of an array

does not

Define the following arrays: lightYears, a 1,000-element array of doubles

double lightYears[10000];

When an array parameter is declared as const, the function is not allowed to make changes to the array's contents. If a statement in the function attempts to modify the array, an ________ will occur at compile time.

error

When writing functions that accept multi-dimensional arrays as arguments, all but the first dimension must be ______________.

explicitly stated in the parameter list

Define the following arrays: payRates, a 25-element array of floats

float PayRates[25];

The syntax for defining a vector is somewhat different from the syntax used in defining a regular variable or array. Here is an example:

vector<int> numbers;

C++ allows you to create arrays with _______ any number of dimensions.

virtually

Because the extra braces__________________, however, it's a good idea to use them. In addition, the braces give you the ability to leave out initializers within a row without omitting the initializers for the rows that follow it.

visually separate each row

You must always make sure that any time you assign values to array elements, the values are written _____ the array's boundaries.

within

It's possible to define an array ______ specifying its size, as long as you provide an initialization list. C++ automatically makes the array large enough to hold all the initialization values. For example, the following definition creates an array with five elements: double ratings[] = {1.0, 1.5, 2.0, 2.5, 3.0};

without

If an array is defined globally, all of its elements are initialized to ____ by default. Local arrays, however, have no default initialization value.

zero

Subscript numbering in C++ always starts at _______.

zero

It's important to note that if an array is partially initialized, the uninitialized elements will be set to _____. The uninitialized elements of a string array will contain ___ ___. This is true even if the array is defined locally.

zero, empty strings

In working with arrays, a common type of mistake is the _______. This is an easy mistake to make because array subscripts start at 0 rather than 1.

off-by-one error

Because the size declarator is ______, C++ counts the number of items in the initialization list and gives the array that many elements.

omitted

When you use the range-based for loop with an array, the loop automatically iterates ___ ____ ___ ____ in the array.

once for each element

Also, outputting an array's contents must normally be done _________

one element at a time.

Inputting data into an array must normally be done __________

one element at a time.

To sum all the elements of a two-dimensional array, you can use a ____________ to add the contents of each element to an accumulator.

pair of nested loops

If you specify a starting size for a vector, the size declarator is enclosed in __________, not in square brackets.

parentheses

C++ does not _________ you from overwriting an array's bounds.

prevent

The range-based for loop is designed to work with a built-in variable known as the -------- ____. Each time the range-based for loop iterates, it copies an array element to the _____ ______.

range variable

The _____-____ ___ ___ is 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.

range-based for loop

The ________ is 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.

range-based for loop

you cannot use a range-based for loop to modify the contents of an array unless you declare the range variable as a _________

reference

The range-based for loop can be used in any situation where you need to step through the elements of an array, and you do not need to use the element subscripts. It will not work, however, in situations where you need the element subscript for some purpose. In those situations, you need to use the ___ ____ ____.

regular for loop

Array elements may also be used in ________ expressions.

relational

The subscripted references are used in a program just like the references to elements in a single-dimensional array, except now you use two subscripts. The first subscript represents the ______ position, and the second subscript represents the _______ position.

row, column

When a ____ ____ of an array is passed to a function, it is handled like any other variable.

single element

Understand the difference between the array size declarator and a subscript. The number inside the brackets of an array definition is the ____ ____. The number inside the brackets of an assignment statement or any statement that works with the contents of an array is a_____.

size declarator, subscript

If you leave an element uninitialized, you must leave all the elements that follow it uninitialized as well. C++ does not provide a way to ____ elements in the initialization list. For example, the following is not legal: int numbers[6] = {2, 4, , 8, , 12}; // NOT Legal!

skip

A ______ is used as an index to pinpoint a specific element within an array.

subscript

Even though an entire array has only one name, the elements may be accessed and used as individual variables. This is possible because each element is assigned a number known as a ______.

subscript

When processing the data in a two-dimensional array, each element has two ________: one for its row, and another for its column.

subscripts

A _______ -______ _____ is like several identical arrays put together. It is useful for storing multiple sets of data.

two-dimensional array

To use the vector data type, you should also have the _________ statement in your program.

using namespace std;

Is each of the following a valid or invalid array definition? char codes[] = {'A', 'X', '1', '2', 's'};

valid

Is each of the following a valid or invalid array definition? double radii[10] = {3.2, 4.7};

valid

When a single element of an array is passed to a function, it is handled like any other _______.

variable

A _________ is a container that can store data.

vector

To pass an array as an argument to a function, pass the ______ of the array.

name

Size of Each Element of double

8 bytes

What would the valid subscript values be in a 4-element array of doubles?

0 through 3

Size of Each Element of char

1 byte

o use vectors in your program, you must include the _______ header file

<vector>

Size of Each Element of short

2 bytes

Two-dimensional arrays, which are sometimes called ______ arrays, can hold multiple sets of data. It's best to think of a two-dimensional array as having rows and columns of elements

2D

Size of Each Element of float

4 bytes

Size of Each Element of int

4 bytes

A vector is a container that can store data. It is like an array in the following ways:

A vector holds a sequence of values or elements. A vector stores its elements in contiguous memory locations. You can use the array subscript operator [] to read the individual elements in the vector.

Define the following arrays: A) ages, a 10-element array of ints initialized with the values 5, 7, 9, 14, 15, 17, 18, 19, 21, and 23. B) temps, a 7-element array of floats initialized with the values 14.7, 16.3, 18.43, 21.09, 17.9, 18.76, and 26.7. C) alpha, an 8-element array of chars initialized with the values 'J', 'B', 'L', 'A', '*', '$', 'H', and 'M'.

A) int ages[10] = {5, 7, 9, 14, 15, 17, 18, 19, 21, 23}; B) float temps[7] = {14.7, 16.3, 18.43, 21.09, 17.9, 18.76, 26.7}; C) char alpha[8] = {'J', 'B', 'L', 'A', '*', '$', 'H', 'M'};

______ ______ work very much like reference variables. They give the function direct access to the original array. Any changes made with the array parameter are actually made on the original array used as the argument.

Array parameters

What is "array bounds checking"? Does C++ perform it?

It prevents a program from using a subscript that is beyond the boundaries of an array. C++does not perform array bounds checking.

________ array elements is no different than processing other variables.

Processing

The ________ is a collection of data types and algorithms that you may use in your programs. These data types and algorithms are programmer-defined. They are not part of the C++ language, but were created in addition to the built-in data types.

Standard Template Library (STL)

If a local array is completely uninitialized, its elements will contain "________," like all other local variables.

garbage

The expression hours[0] is pronounced "______ _____ ___."

hours sub zero

The only way to assign one array to another is to assign the____ ____ in the arrays. Usually, this is best done with a loop

individual elements

Like regular variables, C++ allows you to initialize an array's elements when you create the array. Here is an example: const int MONTHS = 12; int days[MONTHS] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; The series of values inside the braces and separated with commas is called an ____ _____. These values are stored in the array elements in the order they appear in the list.

initialization list

You must provide an ___ ____ if you leave out an array's size declarator. Otherwise, the compiler doesn't know how large to make the array.

initialization list

When you specify a starting size for a vector, you may also specify an _______. The initialization value is copied to each element.

initialization value

Define the following arrays: empNums, a 100-element array of ints

int empNumbs[100];

What's wrong with the following array definitions? int readings[−1]; float measurements[4.5]; int size; string names[size];

int readings[-1]; // Size declarator cannot be negative float measurements[4.5]; // Size declarator must be an integer int size; string names[size]; // Size declarator must be a constant

To pass an array as an argument to a function, pass the _____ of the array.

name

Is each of the following a valid or invalid array definition? int matrix[5] = {1, 2, 3, 4, 5, 6, 7};

invalid because there are too many values in the initialization list.

Is each of the following a valid or invalid array definition? int blanks[];

invalid. The definition of blanks is invalid. An initialization list must be provided when an array is implicitly sized.

Is each of the following a valid or invalid array definition? int table[7] = {2, , , 27, , 45, 39};

invalid. Values cannot be skipped in the initialization list.

The subscript of the ______ element in an array is one less than the total number of elements in the array.

last

To sum the values in an array, you must use a ______________. The loop adds the value in each array element to the accumulator.

loop with an accumulator variable

you cannot use a range-based for loop to ______ the contents of an array unless you declare the range variable as a reference.

modify

you should always use const array parameters in any function that is not intended to _______ its array argument. That way, the function will fail to compile if you inadvertently write code in it that modifies the array.

modify

C++ allows you to spread the initialization list across ____ _____.

multiple lines

The size of an array can be calculated by _______ the size of an individual element by the number of elements in the array.

multiplying


Set pelajaran terkait

Plessy V Ferguson; Du Bois; Washington

View Set

REVIEW OF PATHOLOGY Questions Chapter 15. The Lung + WEBPATH pulmonagy

View Set

CH.8 POLITICAL PARTIES, CH.9 NOMINATIONS AND CAMPAIGNS, CH.10 ELECTIONS, CH.11 Interest Groups

View Set