C H 15 Q U I Z

Ace your homework & exams now with Quizwiz!

[1530] What is printed? int mystery(const int a[], size_t n) { int x = 0; for (size_t i = 0; i < n; i++) if (a[i] < a[x]) x = i; return x; } int main() { int a[] = {4, 2, 5, 2, 5, 4}; cout << mystery(a, 6) << endl; } None of these 2 0 1 3

1

[1510] Below is a cumulative algorithm using an array and an iterator-based loop. What is printed? (Assume all includes have been added, etc.) double average(const int *beg, const int *end) { double sum = 0; size_t count = end - beg; while (beg != end) sum += *beg++; return sum / count; } int main() { int a[] = {2, 4, 6, 8}; cout << average(a, a + 1) << endl; } Does not compile 3 2 5 4

2

[1516] What is printed here? (Assume all includes have been added. Assume 4-bytes per int, 8 bytes per pointer.) size_t len(const int a[]) { return sizeof(a) / sizeof(a[0]); } int main() { int a[] = {2, 4, 6, 8}; cout << len(a) << endl; } 2 Does not compile 1 4

2

[1529] What is printed? int mystery(const int a[], size_t n) { int x = 0; for (size_t i = 0; i < n; i++) if (a[i] > a[x]) x = i; return x; } int main() { int a[] = {4, 2, 5, 2, 5, 4}; cout << mystery(a, 6) << endl; } 5 None of these 0 2 4

2

[1511] Below is a cumulative algorithm using an array and an iterator-based loop. What is printed? (Assume all includes have been added, etc.) double average(const int *beg, const int *end) { double sum = 0; size_t count = end - beg; while (beg != end) sum += *beg++; return sum / count; } int main() { int a[] = {2, 4, 6, 8}; cout << average(a, a + 2) << endl; } Does not compile 5 3 4 2

3

[1519] What is printed here? (Assume all includes have been added. Assume 4-bytes per int, 8 bytes per pointer.) size_t len(const int* a, const int* b) { return b - a; } int main() { int a[] = {2, 4, 6, 8}; cout << len(a, a + 3) << endl; } 2 3 4 Does not compile

3

[1527] What is printed? int mystery(const int a[], size_t n) { int x = n - 1; while (n > 0) { n--; if (a[n] < a[x]) x = n; } return x; } int main() { int a[] = {4, 2, 5, 2, 5, 4}; cout << mystery(a, 6) << endl; } 1 4 None of these 3 2

3

[1506] Below is a cumulative algorithm using an array and an iterator-based loop. What is printed? (Assume all includes have been added, etc.) double average(const int *beg, const int *end) { double sum = 0; size_t count = end - beg; while (beg != end) sum += *beg++; return sum / count; } int main() { int a[] = {2, 4, 6, 8}; cout << average(begin(a), end(a) - 1) << endl; } Endless loop when run; likely crashes. Does not compile 4 5 6

4

[1513] Below is a cumulative algorithm using an array and an iterator-based loop. What is printed? (Assume all includes have been added, etc.) double average(const int *beg, const int *end) { double sum = 0; size_t count = end - beg; while (beg != end) sum += *beg++; return sum / count; } int main() { int a[] = {2, 4, 6, 8}; cout << average(a, a + 3) << endl; } 5 Does not compile 4 2 3

4

[1517] What is printed here? (Assume all includes have been added. Assume 4-bytes per int, 8 bytes per pointer.) int main() { int a[] = {2, 4, 6, 8}; cout << sizeof(a) / sizeof(a[0]) << endl; } Does not compile 4 1 2

4

[1518] What is printed here? (Assume all includes have been added. Assume 4-bytes per int, 8 bytes per pointer.) size_t len(const int* a, const int* b) { return b - a; } int main() { int a[] = {2, 4, 6, 8}; cout << len(begin(a), end(a)) << endl; } Does not compile 4 2 1

4

[1528] What is printed? int mystery(const int a[], size_t n) { int x = n - 1; while (n > 0) { n--; if (a[n] > a[x]) x = n; } return x; } int main() { int a[] = {4, 2, 5, 2, 5, 4}; cout << mystery(a, 6) << endl; } None of these 4 3 1 2

4

[1505] Below is a cumulative algorithm using an array and an iterator-based loop. What is printed? (Assume all includes have been added, etc.) double average(const int **beg, const int **end) { double sum = 0; size_t count = end - beg; while (beg != end) sum += *beg++; return sum / count; } int main() { int a[] = {2, 4, 6, 8}; cout << average(begin(a), end(a)) << endl; } 4 5 Does not compile 6 Endless loop when run; likely crashes.

5

[1512] Below is a cumulative algorithm using an array and an iterator-based loop. What is printed? (Assume all includes have been added, etc.) double average(const int *beg, const int *end) { double sum = 0; size_t count = end - beg; while (beg != end) sum += *beg++; return sum / count; } int main() { int a[] = {2, 4, 6, 8}; cout << average(a + 1, a + 3) << endl; } 5 2 Does not compile 4 3

5

[1525] What is printed? int mystery(const int a[], size_t n) { int x = a[n - 1]; while (n > 0) { n--; if (a[n] > a[x]) x = a[n]; } return x; } int main() { int a[] = {1, 3, 5, 3, 5, 4}; cout << mystery(a, 6) << endl; }

5

[1531] What is printed? const int* mystery(const int* p, size_t n) { const int *x = p, *y = p + n; while (++p != y) { if (*p > *x) x = p; } return x; } int main() { int a[] = {1, 2, 3, 4, 5, 1}; cout << *(mystery(a, 6)) << endl; } 0 5 2 None of these 4

5

[1532] What is printed? const int* mystery(const int* p, size_t n) { const int *x = p, *y = p + n; while (++p != y) { if (*p > *x) x = p; } return x; } int main() { int a[] = {1, 2, 3, 4, 5, 1}; cout << *(mystery(a, 6)) << endl; } 4 5 2 None of these 0

5

[1507] Below is a cumulative algorithm using an array and an iterator-based loop. What is printed? (Assume all includes have been added, etc.) double average(const int *beg, const int *end) { double sum = 0; size_t count = end - beg; while (beg != end) sum += *beg++; return sum / count; } int main() { int a[] = {2, 4, 6, 8}; cout << average(begin(a) + 1, end(a)) << endl; } 6 4 5 Does not compile Endless loop when run; likely crashes.

6

[1520] What is printed here? (Assume all includes have been added.) int odds(int a[], size_t len) { int sum = 0; for (size_t i = 0; i < len; i++) if (a[i] % 2 == 1) sum += a[i]++; return sum; } int main() { int a[] = {1, 3, 5}; cout << odds(a, 3) << odds(a, 2) << odds(a, 1) << endl; } 999 900 300 941 Does not compile

900

[1537] What is the name for this algorithm? template <typename T> ostream& mystery(ostream& out, const T* p, size_t n) { out << '['; if (n) { out << p[0]; for (size_t i = 1; i < n; i++) out << ", " << p[i]; } out << "]"; return out; } A cumulative algorithm An extreme values algorithm An iterator algorithm None of these A fencepost algorithm

A fencepost algorithm

Elements always allocated on the heap How arrays are passed to functions What happens to an array when passed to a function const int *array int * const array const int * const array sizeof(a) / sizeof(a[0]) end(a) - begin(a) for (auto e : a) . . x = 0; for (auto e : a) x += e; x = a[0]; for (auto e: a) if (e > x) x = e; auto p = a; while (p != end(a)) p++; cout << a[0]; while (i < len) cout << ", " << a[i++];

vector ⠀⠀ by address ⠀⠀ decays ⠀⠀ Elements may not be modified; pointer may be ⠀⠀ Elements in may be modified; pointer may not ⠀⠀ Neither pointer nor elements in may be modified ⠀⠀ Elements in array using arithmetic ⠀⠀ Elements in array using pointer difference ⠀⠀ A range-based loop ⠀⠀ Cumulative algorithm ⠀⠀ Extreme values algorithm ⠀⠀ Iterator-based loop ⠀⠀ Fence-post algorithm

[1522] What does this function do? int mystery(const int a[], size_t n) { int x = n - 1; while (n > 0) { n--; if (a[n] < a[x]) x = n; } return x; } Returns the smallest number in the array Returns the index of the last occurrence of the smallest number in the array Does not compile Returns the index of the first occurrence of the smallest number in the array Returns the largest number in the array

Returns the index of the last occurrence of the smallest number in the array

[1524] What does this function do? int mystery(const int a[], size_t n) { int x = a[n - 1]; while (n > 0) { n--; if (a[n] > a[x]) x = a[n]; } return x; } Returns the index of the last occurrence of the smallest number in the array Does not compile Returns the largest number in the array Returns the smallest number in the array Returns the index of the first occurrence of the smallest number in the array

Returns the largest number in the array

[1533] What does this function do? double mystery(const double a[], size_t len) { double x = a[0]; for (size_t i = 1; i < len; i++) if (a[i] > x) x = a[i]; return x; } Does not compile Returns the largest number in the array Returns the smallest number in the array Undefined. Depends on the input.

Returns the largest number in the array

[1523] What does this function do? int mystery(const int a[], size_t n) { int x = a[n - 1]; while (n > 0) { n--; if (a[n] < a[x]) x = a[n]; } return x; } Returns the index of the first occurrence of the smallest number in the array Returns the largest number in the array Returns the index of the last occurrence of the smallest number in the array Returns the smallest number in the array Does not compile

Returns the smallest number in the array

[1534] What does this function do? double mystery(const double a[], size_t len) { double x = a[0]; for (size_t i = 1; i < len; i++) if (a[i] < x) x = a[i]; return x; } Returns the largest number in the array Does not compile Returns the smallest number in the array Undefined. Depends on the input.

Returns the smallest number in the array

[1514] What is the correct prototype for mystery? (It is not supposed to modify the array.) const int a[] = {2, 4, 6, 8}; cout << mystery(a, 4) << endl; void mystery(const int a[], size_t n); int mystery(int a[], size_t n); int mystery(const int a*, size_t n); int mystery(const int *a, size_t n); int mystery(const int[] a, size_t n);

int mystery(const int *a, size_t n);

[1515] What is the correct prototype for mystery? (It may modify the array.) const int a[] = {2, 4, 6, 8}; cout << mystery(a, 4) << endl; int mystery(int[] a, size_t n); int mystery(int a, size_t n); int mystery(int *a, size_t n); int mystery(int a*, size_t n); void mystery(const int a[], size_t n);

int mystery(int *a, size_t n);

[1501] Below is a cumulative algorithm using an array and a range-based loop. What is printed? (Assume this is inside main() with all includes, etc.) int a[] = {2, 4, 6, 8}; int sum = 0; for (auto e : a) sum += e; cout << "sum->" << sum << endl; Compiles but crashes with an endless loop. Does not compile. Cannot use range-loop on arrays. sum->20 sum->0 Compiles and runs, but results are undefined.

sum->20

[1504] Below is a cumulative algorithm using an array and a range-based loop. What is printed? (Assume this is inside main() with all includes, etc.) int a[] = {2, 4, 6, 8}; int sum = 0; for (auto e : a) sum =+ e; cout << "sum->" << sum << endl; Does not compile. Cannot use range-loop on arrays. sum->8 Compiles and runs, but results are undefined. sum->20 Does not compile; e is undefined.

sum->8

[1521] What does this function do? int mystery(const int a[], size_t n) { int x = n - 1; while (n > 0) { n--; if (a[n] > a[x]) x = n; } return x; } Returns the largest number in the array Returns the index of the last occurrence of the largest number in the array Returns the smallest number in the array Returns the index of the first occurrence of the largest number in the array Does not compile

Returns the index of the last occurrence of the largest number in the array

[1502] Below is a cumulative algorithm using an array and a range-based loop. What is printed? (Assume this is inside main() with all includes, etc.) int a[] = {2, 4, 6, 8}; int sum; for (auto e : a) sum += e; cout << "sum->" << sum << endl; Compiles and runs, but results are undefined. sum->20 sum->8 Does not compile. Cannot use range-loop on arrays. Compiles but crashes with an endless loop.

Compiles and runs, but results are undefined.

[1503] Below is a cumulative algorithm using an array and a range-based loop. What is printed? (Assume this is inside main() with all includes, etc.) int a[] = {2, 4, 6, 8}; int sum = 0; for (auto e : a) sum += e; cout << "sum->" << e << endl; Does not compile; e is undefined. Does not compile. Cannot use range-loop on arrays. Compiles and runs, but results are undefined. sum->20 sum->8

Does not compile; e is undefined.

[1508] Below is a cumulative algorithm using an array and an iterator-based loop. What is printed? (Assume all includes have been added, etc.) double average(const int *beg, const int *end) { double sum = 0; size_t count = end - beg; while (beg != end) sum += *beg++; return sum / count; } int main() { int a[] = {2, 4, 6, 8}; cout << average(end(a), begin(a)) << endl; } Does not compile Endless loop when run; likely crashes. 5 6 4

Endless loop when run; likely crashes.

The parameter declarations int p* and int[] p mean the same thing. After passing an array to a function, sizeof(a)/sizeof(a[0]) will tell the number of elements in the array. If p points to the first element in [1, 3, 5] then cout << *++p prints 1. If p points to the first element in [1, 3, 5] then cout << ++*p prints 1. The library function begin(a) returns a pointer to the element right before the first in the array a. For embedded systems, vector is preferred over arrays. For systems programming (such as operating systems), vectors are used more often than arrays. For an equivalent number of elements, a vector will use less memory than an array. The expression *p++ means the same as (*p)++. An array passed to a function f(const int *a, ...) may have its elements changed. The elements of a vector may be allocated on the stack. For an equivalent number of elements, a vector will use more memory than an array. The algorithm that prints elements separated by commas is called a cumulative algorithm. The algorithm that finds the position of the largest element in an array is called a cumulative algorithm. The algorithm that finds the position of the largest element in an array is called a cumulative algorithm. After passing an array to a function, sizeof(a) will tell you the array's allocated size, but not the number of elements. If p points to the first element in [1, 3, 5] then cout << *p++ prints 3. The library function end(a) returns a pointer to the last element in the array a. A vector generally has higher performance than an array. If size_t len = 0; then len - 1 is the smallest possible unsigned number. The expression begin(a) + 1 returns a pointer to the first element in the array a. The function mystery(const int*, const int*) likely employs a counter-controlled loop. An array passed to a function is passed by reference.

False

[1526] What is printed? int mystery(const int a[], size_t n) { int x = n - 1; while (n > 0) { n--; if (a[n] < a[x]) x = n; } return x; } int main() { int a[] = {1, 2, 5, 2, 5, 4}; cout << mystery(a, 6) << endl; } 1 2 3 None of these 4

None of these

[1539] What is printed? template <typename T> ostream& mystery(ostream& out, const T* p, size_t n) { out << '['; if (n) { out << p[0]; for (size_t i = 1; i < n; i++) out << ", " << p[i]; } out << "]"; return out; } int a[] = {1,2,3,4,5,1}; mystery(cout, a, sizeof(a)) << endl; [1, 2, 3, 4, 5, 1] [1, 2, 3, 4] [1, 2, 3, 4, 5] None of these or undefined output. [1, 2, 3]

None of these or undefined output.

[1509] Below is a cumulative algorithm using an array and an iterator-based loop. What is printed? (Assume all includes have been added, etc.) double average(const int *beg, const int *end) { if (end <= beg) return 0.0 / 0.0; // nan double sum = 0; size_t count = end - beg; while (beg != end) sum += *beg++; return sum / count; } int main() { int a[] = {2, 4, 6, 8}; cout << average(end(a), begin(a)) << endl; } 4 Does not compile 5 Not a number (NaN) Endless loop when run; likely crashes.

Not a number (NaN)

An array passed to a function f(int *a, ...) may have its elements changed. An array passed to a function decays to a pointer. An array passed to a function f(int * const a, ...) may have its elements changed. The elements of an array may be allocated on the stack. If p points to the first element in [1, 3, 5] then cout << ++*p prints 2. The library function begin(a) returns a pointer to the first element in the array a. The elements of an array may be allocated in the static storage area. Arrays generally have higher performance than a vector. The function mystery(const int*, const int*) likely employs an iterator loop. The expression begin(a) + 1 returns a pointer to the second element in the array a. Array subscripts are not range checked An array passed to a function is passed by address. If size_t len = 0; then len - 1 is the largest possible unsigned number. If p points to the first element in [1, 3, 5] then cout << *++p prints 3. The algorithm that finds the address of the smallest element in an array is called an extreme values algorithm. The expression *p++ means the same as *(p++). Before passing an array to a function, sizeof(a)/sizeof(a[0]) will tell the number of elements in the array. For systems programming (such as operating systems), arrays are used more often than vectors. The library function end(a) returns a pointer to position right past the last element in the array a. For embedded systems, arrays are preferred over vector. The parameter declarations int *p and int p[] mean the same thing. The algorithm that prints elements separated by commas is called the fencepost algorithm. The elements of a vector are allocated on the heap. A vector variable may be allocated on the stack. Before passing an array to a function, sizeof(a) will tell you the array's allocated size, but not the number of elements.

True

[1535] What does this function do? double mystery(const double a[], size_t len) { double x = 0; for (size_t i = 0; i < len; i++) if (a[i] > x) x = a[i]; return x; } Undefined. Depends on the input. Does not compile Returns the largest number in the array Returns the smallest number in the array

Undefined. Depends on the input.

[1536] What does this function do? double mystery(const double a[], size_t len) { double x = 0; for (size_t i = 0; i < len; i++) if (a[i] < x) x = a[i]; return x; } Returns the largest number in the array Returns the smallest number in the array Undefined. Depends on the input. Does not compile

Undefined. Depends on the input.

[1540] What is printed? template <typename T> ostream& mystery(ostream& out, const T* p, size_t n) { out << '['; if (n) { out << p[0]; for (size_t i = 1; i < n; i++) out << ", " << p[i]; } out << "]"; return out; } . . . int a[] = {1,2,3,4,5,1}; mystery(cout, a, sizeof(a) / sizeof(a[0])) << endl; None of these or undefined output. [1, 2, 3, 4] [1, 2, 3] [1, 2, 3, 4, 5] [1, 2, 3, 4, 5, 1]

[1, 2, 3, 4, 5, 1]

[1538] What is printed? template <typename T> ostream& mystery(ostream& out, const T* p, size_t n) { out << '['; if (n) { out << p[0]; for (size_t i = 1; i < n; i++) out << ", " << p[i]; } out << "]"; return out; } int a[] = {1,2,3,4,5,1}; mystery(cout, a, 4) << endl; [1, 2, 3] [1, 2, 3, 4, 5, 1] None of these or undefined output. [1, 2, 3, 4, 5] [1, 2, 3, 4]

[1, 2, 3, 4]

[1541] What is printed? template <typename T> ostream& mystery(ostream& out, const T* p, size_t n) { out << '['; if (n) { out << p[0]; for (size_t i = 1; i < n; i++) out << ", " << p[i]; } out << "]"; return out; } . . . int a[] = {1,2,3,4,5,1}; mystery(cout, a, 0)) << endl; [0] Does not compile. Arrays cannot be 0 length. [] [1] No output

[]


Related study sets

Unit 16: Individual Retirement Accounts

View Set

Quarter 4 Bells Supreme Court Cases

View Set

Med-Surg Ch 22: Care of Patients With Head and Spinal Cord Injuries

View Set

NURS 241 Exam 4- Cirrhosis, Liver Cancer, Drug & Alcohol Abuse

View Set

Cisco Semester 1, CH 11-13 Quiz Questions

View Set