• Home
  • Textbooks
  • Starting out with C++: From Control Structures through Objects
  • Arrays and Vectors

Starting out with C++: From Control Structures through Objects

Tony Gaddis

Chapter 7

Arrays and Vectors - all with Video Answers

Educators


Chapter Questions

01:29

Problem 1

What is the difference between a size declarator and a subscript?

Mayank Tripathi
Mayank Tripathi
Numerade Educator

Problem 2

Look at the following array definition:
int values[10];
How many elements does the array have?
What is the subscript of the first element in the array?
What is the subscript of the last element in the array?
Assuming that an int uses 4 bytes of memory, how much memory does the array use?

Check back soon!

Problem 3

Why should a function that accepts an array as an argument, and processes that array, also accept an argument specifying the array’s size?

Check back soon!

Problem 4

Consider the following array definition:
int values[5] = { 4, 7, 6, 8, 2 };
What does each of the following statements display?
cout << values[4] << endl; __________
cout << (values[2] + values[3]) << endl; __________
cout << ++values[1] << endl; __________

Check back soon!

Problem 5

How do you define an array without providing a size declarator?

Check back soon!
01:41

Problem 6

Look at the following array definition:
int numbers[5] = { 1, 2, 3 };
What value is stored in numbers[2]?
What value is stored in numbers[4]?

Willis James
Willis James
Numerade Educator

Problem 7

Assuming that array1 and array2 are both arrays, why is it not possible to assign the contents of array2 to array1 with the following statement?
array1 = array2;

Check back soon!

Problem 8

Assuming that numbers is an array of doubles, will the following statement display the contents of the array?
cout << numbers << endl;

Check back soon!

Problem 9

Is an array passed to a function by value or by reference?

Check back soon!

Problem 10

When you pass an array name as an argument to a function, what is actually being passed?

Check back soon!

Problem 11

How do you establish a parallel relationship between two or more arrays?

Check back soon!

Problem 12

Look at the following array definition:
double sales[8][10];
How many rows does the array have?
How many columns does the array have?
How many elements does the array have?
Write a statement that stores a number in the last column of the last row in the array.

Check back soon!

Problem 13

When writing a function that accepts a two-dimensional array as an argument, which size declarator must you provide in the parameter for the array?

Check back soon!

Problem 14

What advantages does a vector offer over an array?

Check back soon!

Problem 15

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

Check back soon!

Problem 16

The size declarator must be a(n) _________ with a value greater than _________.

Check back soon!

Problem 17

Each element of an array is accessed and indexed by a number known as a(n) _________.

Check back soon!

Problem 18

Subscript numbering in C++ always starts at _________.

Check back soon!

Problem 19

The number inside the brackets of an array definition is the _________, but the number inside an array’s brackets in an assignment statement, or any other statement that works with the contents of the array, is the _________.

Check back soon!

Problem 20

C++ has no array _________ checking, which means you can inadvertently store data past the end of an array.

Check back soon!

Problem 21

Starting values for an array may be specified with a(n) _________ list.

Check back soon!

Problem 22

If an array is partially initialized, the uninitialized elements will be set to _________.

Check back soon!

Problem 23

If the size declarator of an array definition is omitted, C++ counts the number of items in the _________ to determine how large the array should be.

Check back soon!

Problem 24

By using the same _________ for multiple arrays, you can build relationships between the data stored in the arrays.

Check back soon!

Problem 25

You cannot use the _________ operator to copy data from one array to another in a single statement.

Check back soon!

Problem 26

Any time the name of an array is used without brackets and a subscript, it is seen as _________.

Check back soon!

Problem 27

To pass an array to a function, pass the _________ of the array.

Check back soon!

Problem 28

A(n) _________ array is like several arrays of the same type put together.

Check back soon!
00:33

Problem 29

It’s best to think of a two-dimensional array as having _________ and _________.

Thomas Emment
Thomas Emment
Numerade Educator

Problem 30

To define a two-dimensional array, _________ size declarators are required.

Check back soon!

Problem 31

When initializing a two-dimensional array, it helps to enclose each row’s initialization list in _________.

Check back soon!

Problem 32

When a two-dimensional array is passed to a function, the _________ size must be specified.

Check back soon!

Problem 33

The ____________________ is a collection of programmer-defined data types and algorithms that you may use in your programs.

Check back soon!

Problem 34

The two types of containers defined by the STL are ___________ and ______________.

Check back soon!
01:33

Problem 35

The vector data type is a(n) ______________ container.

Eleanor Johnson
Eleanor Johnson
Numerade Educator

Problem 36

To define a vector in your program, you must #include the ____________ header file.

Check back soon!

Problem 37

To store a value in a vector that does not have a starting size, or that is already full, use the ________________ member function.

Check back soon!

Problem 38

To determine the number of elements in a vector, use the _____________ member function.

Check back soon!

Problem 39

Use the ________________ member function to remove the last element from a vector.

Check back soon!

Problem 40

To completely clear the contents of a vector, use the ___________ member function.

Check back soon!

Problem 41

names is an integer array with 20 elements. Write a regular for loop, as well as a range-based for loop that prints each element of the array.

Check back soon!

Problem 42

The arrays numberArray1 and numberArray2 have 100 elements. Write code that copies the values in numberArray1 to numberArray2.

Check back soon!

Problem 43

In a program, you need to store the identification numbers of ten employees (as ints) and their weekly gross pay (as doubles).
A) Define two arrays that may be used in parallel to store the ten employee identification numbers and gross pay amounts.
B) Write a loop that uses these arrays to print each employee’s identification number and weekly gross pay.

Check back soon!

Problem 44

Define a two-dimensional array of integers named grades. It should have 30 rows and 10 columns.

Check back soon!

Problem 45

In a program, you need to store the populations of 12 countries.
A) Define two arrays that may be used in parallel to store the names of the countries and their populations.
B) Write a loop that uses these arrays to print each country’s name and its population.

Check back soon!
02:20

Problem 46

The following code totals the values in two arrays: numberArray1 and numberArray2. Both arrays have 25 elements. Will the code print the correct sum of values for both arrays? Why or why not?
int total = 0; // Accumulator
int count; // Loop counter
// Calculate and display the total of the first array.
for (count = 0; count < 24; count++)
total += numberArray1[count];
cout << "The total for numberArray1 is " << total << endl;
// Calculate and display the total of the second array.
for (count = 0; count < 24; count++)
total += numberArray2[count];
cout << "The total for numberArray2 is " << total << endl;

Ernest Castorena
Ernest Castorena
Numerade Educator

Problem 47

Look at the following array definition:
int numberArray[9][11];
Write a statement that assigns 145 to the first column of the first row of this array.
Write a statement that assigns 18 to the last column of the last row of this array.

Check back soon!

Problem 48

values is a two-dimensional array of floats with 10 rows and 20 columns. Write code that sums all the elements in the array and stores the sum in the variable total.

Check back soon!

Problem 49

An application uses a two-dimensional array defined as follows:
int days[29][5];
Write code that sums each row in the array and displays the results.
Write code that sums each column in the array and displays the results.

Check back soon!

Problem 50

True or False
T F An array’s size declarator can be either a literal, a named constant, or a variable.

Check back soon!

Problem 51

True or False
T F To calculate the amount of memory used by an array, multiply the number of elements by the number of bytes each element uses.

Check back soon!

Problem 52

True or False
T F The individual elements of an array are accessed and indexed by unique numbers.

Check back soon!

Problem 53

True or False
T F The first element in an array is accessed by the subscript 1.

Check back soon!

Problem 54

True or False
T F The subscript of the last element in a single-dimensional array is one less than the total number of elements in the array.

Check back soon!

Problem 55

True or False
T F The contents of an array element cannot be displayed with cout.

Check back soon!

Problem 56

True or False
T F Subscript numbers may be stored in variables.

Check back soon!

Problem 57

True or False
T F You can write programs that use invalid subscripts for an array.

Check back soon!

Problem 58

True or False
T F Arrays cannot be initialized when they are defined. A loop or other means must be used.

Check back soon!

Problem 59

True or False
T F The values in an initialization list are stored in the array in the order they appear in the list.

Check back soon!

Problem 60

True or False
T F C++ allows you to partially initialize an array.

Check back soon!

Problem 61

True or False
T F If an array is partially initialized, the uninitialized elements will contain “garbage.”

Check back soon!

Problem 62

True or False
T F If you leave an element uninitialized, you do not have to leave all the ones that follow it uninitialized.

Check back soon!

Problem 63

True or False
T F If you leave out the size declarator of an array definition, you do not have to include an initialization list.

Check back soon!

Problem 64

True or False
T F The uninitialized elements of a string array will automatically be set to the value "0".

Check back soon!

Problem 65

True or False
T F You cannot use the assignment operator to copy one array’s contents to another in a single statement.

Check back soon!

Problem 66

True or False
T F When an array name is used without brackets and a subscript, it is seen as the value of the first element in the array.

Check back soon!

Problem 67

True or False
T F To pass an array to a function, pass the name of the array.

Check back soon!

Problem 68

True or False
T F When defining a parameter variable to hold a single-dimensional array argument, you do not have to include the size declarator.

Check back soon!

Problem 69

True or False
T F When an array is passed to a function, the function has access to the original array.

Check back soon!

Problem 70

True or False
T F A two-dimensional array is like several identical arrays put together.

Check back soon!

Problem 71

True or False
T F It’s best to think of two-dimensional arrays as having rows and columns.

Check back soon!

Problem 72

True or False
T F The first size declarator (in the declaration of a two-dimensional array) represents the number of columns. The second size definition represents the number of rows.

Check back soon!

Problem 73

True or False
T F Two-dimensional arrays may be passed to functions, but the row size must be specified in the definition of the parameter variable.

Check back soon!

Problem 74

True or False
T F C++ allows you to create arrays with three or more dimensions.

Check back soon!

Problem 75

True or False
T F A vector is an associative container.

Check back soon!

Problem 76

True or False
T F To use a vector, you must include the vector header file.

Check back soon!

Problem 77

True or False
T F vectors can report the number of elements they contain.

Check back soon!

Problem 78

True or False
T F You can use the [] operator to insert a value into a vector that has no elements.

Check back soon!

Problem 79

True or False
T F If you add a value to a vector that is already full, the vector will automatically increase its size to accommodate the new value.

Check back soon!

Problem 80

Each of the following definitions and program segments has errors. Locate as many as you can.

int size;
double values[size];

Check back soon!

Problem 81

Each of the following definitions and program segments has errors. Locate as many as you can.

int collection[220];

Check back soon!

Problem 82

Each of the following definitions and program segments has errors. Locate as many as you can.

int table[10];
for (int x = 0; x < 20; x++)
{
cout << "Enter the next value: ";
cin >> table[x];
}

Check back soon!

Problem 83

Each of the following definitions and program segments has errors. Locate as many as you can.

int hours[3] = 8, 12, 16;

Check back soon!

Problem 84

Each of the following definitions and program segments has errors. Locate as many as you can.

int numbers[8] = {1, 2, , 4, , 5};

Check back soon!

Problem 85

Each of the following definitions and program segments has errors. Locate as many as you can.

float ratings[];

Check back soon!

Problem 86

Each of the following definitions and program segments has errors. Locate as many as you can.

char greeting[] = {'H', 'e', 'l', 'l', 'o'};
cout << greeting;

Check back soon!

Problem 87

Each of the following definitions and program segments has errors. Locate as many as you can.

int array1[4], array2[4] = {3, 6, 9, 12};
array1 = array2;

Check back soon!

Problem 88

Each of the following definitions and program segments has errors. Locate as many as you can.

void showValues(int nums)
{
for (int count = 0; count < 8; count++)
cout << nums[count];
}

Check back soon!

Problem 89

Each of the following definitions and program segments has errors. Locate as many as you can.

void showValues(int nums[4][])
{
for (rows = 0; rows < 4; rows++)
for (cols = 0; cols < 5; cols++)
cout << nums[rows][cols];
}

Check back soon!

Problem 90

Each of the following definitions and program segments has errors. Locate as many as you can.

vector<int> numbers = { 1, 2, 3, 4 };

Check back soon!