Chapter Questions
What is the difference between a size declarator and a subscript?
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?
Why should a function that accepts an array as an argument, and processes that array, also accept an argument specifying the array’s size?
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; __________
How do you define an array without providing a size declarator?
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]?
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;
Assuming that numbers is an array of doubles, will the following statement display the contents of the array?cout << numbers << endl;
Is an array passed to a function by value or by reference?
When you pass an array name as an argument to a function, what is actually being passed?
How do you establish a parallel relationship between two or more arrays?
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.
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?
What advantages does a vector offer over an array?
The _________ indicates the number of elements, or values, an array can hold.
The size declarator must be a(n) _________ with a value greater than _________.
Each element of an array is accessed and indexed by a number known as a(n) _________.
Subscript numbering in C++ always starts at _________.
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 _________.
C++ has no array _________ checking, which means you can inadvertently store data past the end of an array.
Starting values for an array may be specified with a(n) _________ list.
If an array is partially initialized, the uninitialized elements will be set to _________.
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.
By using the same _________ for multiple arrays, you can build relationships between the data stored in the arrays.
You cannot use the _________ operator to copy data from one array to another in a single statement.
Any time the name of an array is used without brackets and a subscript, it is seen as _________.
To pass an array to a function, pass the _________ of the array.
A(n) _________ array is like several arrays of the same type put together.
It’s best to think of a two-dimensional array as having _________ and _________.
To define a two-dimensional array, _________ size declarators are required.
When initializing a two-dimensional array, it helps to enclose each row’s initialization list in _________.
When a two-dimensional array is passed to a function, the _________ size must be specified.
The ____________________ is a collection of programmer-defined data types and algorithms that you may use in your programs.
The two types of containers defined by the STL are ___________ and ______________.
The vector data type is a(n) ______________ container.
To define a vector in your program, you must #include the ____________ header file.
To store a value in a vector that does not have a starting size, or that is already full, use the ________________ member function.
To determine the number of elements in a vector, use the _____________ member function.
Use the ________________ member function to remove the last element from a vector.
To completely clear the contents of a vector, use the ___________ member function.
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.
The arrays numberArray1 and numberArray2 have 100 elements. Write code that copies the values in numberArray1 to numberArray2.
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.
Define a two-dimensional array of integers named grades. It should have 30 rows and 10 columns.
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.
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; // Accumulatorint 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;
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.
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.
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.
True or FalseT F An array’s size declarator can be either a literal, a named constant, or a variable.
True or FalseT F To calculate the amount of memory used by an array, multiply the number of elements by the number of bytes each element uses.
True or FalseT F The individual elements of an array are accessed and indexed by unique numbers.
True or FalseT F The first element in an array is accessed by the subscript 1.
True or FalseT F The subscript of the last element in a single-dimensional array is one less than the total number of elements in the array.
True or FalseT F The contents of an array element cannot be displayed with cout.
True or FalseT F Subscript numbers may be stored in variables.
True or FalseT F You can write programs that use invalid subscripts for an array.
True or FalseT F Arrays cannot be initialized when they are defined. A loop or other means must be used.
True or FalseT F The values in an initialization list are stored in the array in the order they appear in the list.
True or FalseT F C++ allows you to partially initialize an array.
True or FalseT F If an array is partially initialized, the uninitialized elements will contain “garbage.”
True or FalseT F If you leave an element uninitialized, you do not have to leave all the ones that follow it uninitialized.
True or FalseT F If you leave out the size declarator of an array definition, you do not have to include an initialization list.
True or FalseT F The uninitialized elements of a string array will automatically be set to the value "0".
True or FalseT F You cannot use the assignment operator to copy one array’s contents to another in a single statement.
True or FalseT 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.
True or FalseT F To pass an array to a function, pass the name of the array.
True or FalseT F When defining a parameter variable to hold a single-dimensional array argument, you do not have to include the size declarator.
True or FalseT F When an array is passed to a function, the function has access to the original array.
True or FalseT F A two-dimensional array is like several identical arrays put together.
True or FalseT F It’s best to think of two-dimensional arrays as having rows and columns.
True or FalseT 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.
True or FalseT F Two-dimensional arrays may be passed to functions, but the row size must be specified in the definition of the parameter variable.
True or FalseT F C++ allows you to create arrays with three or more dimensions.
True or FalseT F A vector is an associative container.
True or FalseT F To use a vector, you must include the vector header file.
True or FalseT F vectors can report the number of elements they contain.
True or FalseT F You can use the [] operator to insert a value into a vector that has no elements.
True or FalseT 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.
Each of the following definitions and program segments has errors. Locate as many as you can.
int size;double values[size];
int collection[220];
int table[10];for (int x = 0; x < 20; x++){cout << "Enter the next value: ";cin >> table[x];}
int hours[3] = 8, 12, 16;
int numbers[8] = {1, 2, , 4, , 5};
float ratings[];
char greeting[] = {'H', 'e', 'l', 'l', 'o'};cout << greeting;
int array1[4], array2[4] = {3, 6, 9, 12};array1 = array2;
void showValues(int nums){for (int count = 0; count < 8; count++)cout << nums[count];}
void showValues(int nums[4][]){for (rows = 0; rows < 4; rows++)for (cols = 0; cols < 5; cols++)cout << nums[rows][cols];}
vector<int> numbers = { 1, 2, 3, 4 };