Chapter 8 Checkpoint Questions

Ace your homework & exams now with Quizwiz!

Write a statement that displays the contents of the last column of the last row of the sales array defined in Question 8.20.

cout << sales[5][3];

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

int grades[30][10];

8.15 Write a typedef statement that makes the name TenInts an alias for an array that holds 10 integers.

typedef int TenInts[10];

What header file must you #include in order to define vector objects?

vector

Write definition statements for the following three vector objects: frogs (an empty vector of ints), lizards (a vector of 20 doubles), and toads (a vector of 100 chars, with each element initialized to 'Z').

vector <int> frogs; vector <float> lizards(20); vector <char> toads(100, 'Z');

Write a function called displayArray7 . The function should accept a two-dimensional array as an argument and display its contents on the screen. The function should work with any of the following arrays: int hours[5][7]; int stamps[8][7]; int autos[12][7];

void displayArray7(int array[][7], int numRows) { for (int x = 0; x < rows; x++) { for (int y = 0; y < 7; y++) { cout << arr[x][y] << " "; } cout << endl; } }

Given the following array definition: int nums[5] = {1, 2, 3}; What will the following statement display? cout << nums[3];

0

What would the valid subscript values be in a four-element array of double s?

0 through 3

What is the output of the following code? int values[5], count; for (count = 0; count < 5; count++) values[count] = count + 1; for (count = 0; count < 5; count++) cout << values[count] << endl;

1 2 3 4 5

What will the following program display on the screen? #include <iostream> using namespace std; class Tank { private: int gallons; public: Tank() { gallons = 50; } Tank (int gal) { gallons = gal; } int getGallons() { return gallons; } }; int main() { Tank storage[3] = { 10, 20 }; for (int index = 0; index < 3; index++) cout << storage[index].getGallons() << endl; return 0; }

10 20 50

(EXTRA) When used as function arguments, are arrays passed by value?

No.

Given the following array definitions: double array1[4] = {1.2, 3.2, 4.2, 5.2}; double array2[4]; will the following statement work? If not, why? array2 = array1;

No. An entire array cannot be copied in a single statement with the = operator. The array must be copied element-by-element.

For questions 8.34-8.38, assume the Product structure is declared as follows: struct Product { string description; // Product description int partNum; // Part number double cost; // Product cost }; Write a definition for an array named items that can hold 100 Product structures.

Product items[100];

(EXTRA) 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.

8.16 When an array name is passed to a function, what is actually being passed?

The starting address of the array.

Define gators to be an empty vector of ints and snakes to be a 10-element vector of doubles. Then write a statement that stores the value 27 in gators and a statement that stores the value 12.897 in element 4 of snakes.

vector <int> gators; vector <double> snakes(10); gators.push_back(27); snakes[4] = 12.897;

Write a statement that assigns the value 56893.12 to the first column of the first row of the sales array defined in Question 8.20.

sales[0][0] = 56893.12;

Write a structure declaration called Destination, with the following members: city, a string object travelTime, a Measurement structure (declared in checkpoint 8.39)

struct Destination { string city; Measurement travelTime; };

Write a structure declaration called Measurement that holds an int named miles and a double named hours.

struct Measurement { int miles; double hours; };

Complete the following program so that it defines an array of 10 Yard objects. The program should use a loop to ask the user for the length and width of each yard. Then it should use a second loop to display the length and width of each yard. To do this, you will need to add two member functions to the Yard class. #include<iostream> using namespace std; class Yard { private: int length, width; public: Yard() { Length = 0; width = 0; } void setLength(int 1) { length = 1; } void setWidth(int w) { width = w; } }; int main() { // Finish this Program. }

#include <iostream> using namespace std; class yard { private: int length, width; public: yard() { length = 0; width = 0; } void setLength(int len) { length = len; } void setWidth(int wide) { width = wide; } int getLength() {return length;} int getWidth() {return width;} }; int main() { const int SIZE = 10; Yard lawns[SIZE]; cout << "Enter the length and width of " << "each yard.\n"; for (int count = 0; count < SIZE; count++) { int input; cout << "Yard " << (count + 1) << ":\n"; cout << "length: "; cin >> input; lawns[count].setLength(input); cout << "width: "; cin << input; lawns[count].setWidth(input); } cout << "\nHere are the yard dimensions.\n"; for (int yard = 0; yard < SIZE; yard++) { cout << "Yard " << (yard+1) << " " << lawns[yard].getLength() << " X " << lawns[yard].getWidth() << endl; } return 0; }

Complete the following program skeleton so it will have a 10-element array of int values called fish. When completed, the program should ask how many fish were caught by fisherman 1 through 10, and store this information in the array. Then it should display the data. #include <iostream> using namespace std; int main() { const int NUM_MEN = 10; // Define an array named fish that can hold 10 int // values. // You must finish this program so it works as // described above. return 0; }

#include <iostream> using namespace std; int main() { const int NUM_MEN = 10; int fish[NUM_MEN], count; cout << "Enter the number of fish caught\n"; cout << "by each fisherman.\n"; for (int count = 0; count < NUM_MEN; count++) { cout << "fisherman " << (count+1) << ": "; cin >> fish[count]; } cout << "\n\nFish Report\n\n"; for (int count = 0; count < NUM_MEN; count++) { cout << "Fisherman #" << count+1 << " caught " << fish[count] << " fish.\n"; } return 0; }

(EXTRA) The following program skeleton contains a 20-element array of int s called fish. When completed, the program should ask how many fish were caught by fishermen 1 through 20, and store this data in the array. Complete the program. #include <iostream> using namespace std; int main() { const int NUM_FISH = 20; int fish[NUM_FISH]; // You must finish this program. It should ask how // many fish were caught by fishermen 1-20, and // store this data in the array fish. return 0; }

#include <iostream> using namespace std; int main() { const int NUM_FISH = 20; int fish[NUM_FISH], count; cout << "Enter the number of fish caught\n"; cout << "by each fisherman.\n"; for (count = 0; count < NUM_FISH; count++) { cout << "fisherman " << (count+1) << ": "; cin >> fish[count]; } return 0; }

The following program segments, when completed, will ask the user to enter 10 integers, which are stored in an array. The function avgArray, which you must write, should calculate and return the average of the numbers entered. // Write the avgArray function prototype here. // It should have a const array parameter. int main() { const int SIZE = 10; int userNums[SIZE]; cout << "Enter 10 numbers: "; for (int count = 0; count < SIZE; count++) { cout << "#" << (count + 1) << " "; cin >> userNums[count]; } cout << "The average of those numbers is "; cout << avgArray(userNums, SIZE) << endl; return 0; } // Write the avgArray function here.

(The entire program is shown here.) #include <iostream> using namespace std; // Function prototype here double avgArray(const int [], int); int main() { const int SIZE = 10; int userNums[SIZE]; cout << "Enter 10 numbers: "; for (int count = 0; count < SIZE; count++) { cout << "#" << (count + 1) << " "; cin >> userNums[count]; } cout << "The average of those numbers is "; cout << avgArray(userNums, SIZE) << endl; return 0; } // Function avgArray double avgArray(const int array[], size) { double total = 0.0, average; for (int count = 0; count < size; count++) total += array[count]; average = total / size; return average; }

What is the output of the following code? (You may need to use a calculator.) const int SIZE 5; int count; int time[SIZE] = {1, 2, 3, 4, 5}, speed[SIZE] = {18, 4, 27, 52, 100}, dist[SIZE]; for (int count = 0; count < SIZE; count++) dist[count] = time[count] * speed[count]; for (count = 0; count < SIZE; count++) { cout << time[count] << " "; cout << speed[count] << " "; cout << dist[count] << endl; }

1 18 18 2 4 8 3 27 81 4 52 208 5 100 500

What is the output of the following code? (You may need to use a calculator.) double balance[5] = {100.0, 250.0, 325.0, 500.0, 1100.0}; const double INTRATE = 0.1; cout << fixed << showpoint << setprecision(2); for (int count = 0; count < 5; count++) cout << (balance[count] * INTRATE) << endl;

10.00 25.00 32.50 50.00 110.00

Fill in the empty table below so it shows the contents of the following array: int table[3][4] = {{2, 3}, {7, 9, 2}, {1}};

2 3 0 0 7 9 2 0 1 0 0 0

How many elements are in the following array? double sales[6][4];

24

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

A) 10 B) 3 C) 6 D) 14

Assume a program contains the following two statements. They both use square brackets, but they mean different things. int score[15]; score[6] = 45; A) Which number is the size declarator? B) Which number is a subscript?

A) 15 B) 6

Indicate if each of the following array definitions is valid or invalid. (If a definition is invalid, explain why.) A) int numbers[10] = {0, 0, 1, 0, 0, 1, 0, 0, 1, 1}; B) int matrix[5] = {1, 2, 3, 4, 5, 6, 7}; C) double radii[10] = {3.2, 4.7}; D) int table[7] = {2, , , 27, , 45, 39}; E) char codes[] = {'A', 'X', '1', '2', 's'}; F) int blanks[]; G) string suit[4] = {"Clubs", "Diamonds", "Hearts", Spades"};

A) The definition is valid. B) The definition is invalid because there are too many values in the initialization list. C) The definition is valid. Elements 2 through 9 will be initialized to 0.0. D) The definition is invalid. Values cannot be skipped in the initialization list. E) The definition is valid. The codes array will be allocated space for five characters. F) The definition is invalid. An initialization list must be provided when an array is implicitly sized. G) The definition is valid.

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 doubles 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) double 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'};

Define the following arrays: A) empNums, a 100-element array of int s B) payRate, a 25-element array of doubles C) miles, a 14-element array of longs D) stateCapital, a 50-element array of string objects E) lightYears, a 1,000-element array of doubles

A) int empNums[100]; B) double payRate[25]; C) long miles[14]; D) string stateCapital[50]; E) double lightYears[1000];

What is the output of the following program segments? (You may need to consult the ASCII table in Appendix B .) // Function prototypes void fillArray(char [], int); void showArray(const char [], int); int main () { char prodCode[8] = {'0', '0', '0', '0', '0', '0', '0', '0'}; fillArray(prodCode, 8); showArray(prodCode, 8); return 0; } // Definition of function fillArray. // (Hint: 65 is the ASCII code for 'A') void fillArray(char arr[], int size) { char code = 65; for (int k = 0; k < size; code++, k++) arr[k] = code; } // Definition of function showArray. void showArray(const char codes[], int size) { for (int k = 0; k < size; k++) cout << codes[k]; }

ABCDEFGH

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

Array bounds checking is a safeguard provided by some languages. It prevents a program from using a subscript that is beyond the boundaries of an array. C++ does not perform array bounds checking.

Define an array of 20 Destination structures (see checkpoint 8.40). Write statements that store the following information in the fifth array element: City: Tupelo Miles: 375 Hours: 7.5

Destination places [20]; places[4].city = "Tupelo"; places[4].travelTime.miles = 375; places[4].travelTime.hours = 7.5;

True or False: All elements in an array of objects must use the same constructor.

False

True or False: The default constructor is the only constructor that may be called for objects in an array of objects.

False

For questions 8.34-8.38, assume the Product structure is declared as follows: struct Product { string description; // Product description int partNum; // Part number double cost; // Product cost }; Write the definition for an array of five Product structures, initializing the first three elements with the following information: Description: Part Number: Cost: Screwdriver 621 $1.72 Socket set 892 18.97 Claw hammer 547 8.29

Product items[5] = { Product("Screw driver", 621, 1.72), Product("Socket set", 892, 19.97), Product("Claw hammer", 547, 8.29) };

For questions 8.34-8.38, assume the Product structure is declared as follows: struct Product { string description; // Product description int partNum; // Part number double cost; // Product cost }; Add two constructors to the Product structure declaration. The first should be a default constructor that sets the description member to the null string and the partNum and cost members to zero. The second constructor should have three parameters: a string, an int, and a double. It should copy the values of the arguments into the description, partNum, and cost members.

Product() // default constructor { description = ""; partNum = cost = 0; } Product(string d, int p, double c) // Constructor { description = d; partNum = p; cost = c; }

For questions 8.34-8.38, assume the Product structure is declared as follows: struct Product { string description; // Product description int partNum; // Part number double cost; // Product cost }; Write a loop that displays the contents of the entire items array you created in question 8.35.

for (int x = 0; x < 100; x++) { cout << items[x].description << endl; cout << items[x].partNum << endl; cout << items[x].cost << endl << endl; }

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 literal or named constant

Define a two-dimensional array named settings large enough to hold the table of information below. Initialize the array with the values in the table. 12 24 32 21 42 14 67 87 65 90 19 1 24 12 8

int settings[3][5] = {{12, 24, 32, 21, 42}, {14, 67, 87, 65, 90}, {19, 1, 24, 12, 8}};

A DVD rental store keeps DVDs on 50 racks with 10 shelves each. Each shelf holds 25 DVDs. Define a 3D array to represent this storage system.

int vidNum[50][10][25];

For questions 8.34-8.38, assume the Product structure is declared as follows: struct Product { string description; // Product description int partNum; // Part number double cost; // Product cost }; Write statements that store the following information in the first element of the items array you defined in question 8.35. Description: Claw Hammer Part Number: 547 Part Cost: $8.29

items[0].description = "Claw Hammer"; items[0].partNum = 547; items[0].cost = 8.29;


Related study sets

Second Story of Creation- Genesis 2:4-10

View Set

INCIDENCIA ECONÓMICA FINANCIERA EN EL SECTOR PÚBLICO

View Set

Adaptive Quizzing: Med Surg "Respiratory"

View Set

239 Ch 19: Aminoglycosides and Fluoroquinolones PREP U

View Set

Marketing 311 Chapter 5, Mktg Ch 9, MKTG Chapter 8, MKTG Ch 11, MKTG Ch10, Mktg Ch 17, Mktg Ch14, Marketing 311 Chapter 7, Chapter 6: Customer Value- Driven Marketing Strategy, Chapter 16: Sustainable Marketing, Marketing 12 and 13, Product Mix, Prin...

View Set