Ch 7 Checkpoint Questions

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.

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

cout << sales[5][3];

Write a function called displayArray7 . The function should accept a twodimensional 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]; int cats[50][7];

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

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

sales[0][0] = 56893.12;

snakes is a vector of double s, with 10 elements. Write a statement that stores the value 12.897 in element 4 of the snakes vector.

snakes[4] = 12.897;

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

vector

Write a definition statement for a vector named toads. toads should be a vector of 100 char s, with each element initialized to 'Z'.

vector <char> toads(100, 'Z');

Write a definition statement for a vector named lizards. lizards should be a vector of 20 float s.

vector <float> lizards(20);

Write a definition statement for a vector named frogs. frogs should be an empty vector of int s.

vector <int> frogs;

gators is an empty vector of int s. Write a statement that stores the value 27 in gators.

vector <int> gators; gators.push_back(27);

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 skeleton, when completed, will ask the user to enter 10 integers, which are stored in an array. The function avgArray, which you must write, is to calculate and return the average of the numbers entered. #include <iostream> using namespace std; // Write your function prototype here 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 function avgArray here. //

(The entire program is shown here.) #include <iostream> using namespace std; // Function prototype here double avgArray(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(int arr[]) { double total = 0.0, average; for (int count = 0; count < 10; count++) total += arr[count]; average = total / 10; return average; }

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 is the output of the following code? (You may need to use a calculator.) const int SIZE = 5; 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 (int 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 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) 0 B) 3 C) 6 D) 14

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

Define the following arrays: A) empNums, a 100-element array of int s B) payRates, a 25-element array of float s C) miles, a 14-element array of long s D) cityName, a 26-element array of string objects E) lightYears, a 1,000-element array of double s

A) int empNums[100]; B) float payRates[25]; C) long miles[14]; D) string cityNames[26]; E) double lightYears[1000];

What is the output of the following program? (You may need to consult the ASCII table in Appendix B .) #include <iostream> using namespace std; // Function prototypes void fillArray(char [], int); void showArray(const char [], int); int main () { const int SIZE = 8; char prodCode[SIZE] = {'0', '0', '0', '0', '0', '0', '0', '0'}; fillArray(prodCode, SIZE); showArray(prodCode, SIZE); 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]; cout << endl; }

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.

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.

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

The address of the 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}; int matrix[5] = {1, 2, 3, 4, 5, 6, 7}; double radii[10] = {3.2, 4.7}; int table[7] = {2, , , 27, , 45, 39}; char codes[] = {'A', 'X', '1', '2', 's'}; int blanks[];

The definition of numbers is valid. The declaration of matrix is invalid because there are too many values in the initialization list. The definition of radii is valid. The definition of table is invalid. Values cannot be skipped in the initialization list. The definition of codes is valid. The definition of blanks is invalid. An initialization list must be provided when an array is implicitly sized.

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

int grades[30][10];

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

Define a two-dimensional array named settings large enough to hold the table of data 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 video rental store keeps DVDs on 50 racks with 10 shelves each. Each shelf holds 25 DVDs. Define a three-dimensional array large enough to represent the store's storage system.

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


Set pelajaran terkait

Nursing Research/Evidence Based Practice Exam 1 (ch.1-3)

View Set

Plato Technical Communication Class

View Set

Chemistry Chapter 6 Homework Assignment

View Set

AC204 • Exam 2 • Chapters 5, 6, 13

View Set

EDUC 1300 chapters 1,2,3,7,8,& 9 hcc, Improvement of study, college skills, Chapter 1- Connect

View Set

medical sociology quiz questions

View Set