Scoped Exam #3

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Consider the following function: void super_write_vertical (int number) // Postcondition: The digits of the number have been // written, stacked vertically. If number is negative, then a // negative sign appears on top. // Library facilities used: iostream.h, math.h { if (number < 0) { std::cout << '-' << std::endl; super_write_vertical (abs(number)); } else if (number < 10) std::cout << number << std::endl; else { super_write_vertical(number/10); std::cout << number % 10 << std::endl; } } What values of number are directly handled by the stopping case?

0-9 ( Stopping Case: else if (number < 10) )

Why is it a bad idea to place using directives in the implementation of a template class?

A template class has its implementation included in the header file, otherwise every program using the template will pick up out using directives.

Does the complier generate and compile the implementation of a template function or class when it encounters the implementation of when the implementation is instantiated (e.g., when the implementation is invoked) in the code? Why is this?

Instantiated because the compiler compiles a certain version of the template function using the specified type for the parameter

For the execution of recursive functions, the compiler and operating system store function invocation information. What data structures is used to store all the executions of the functions? Why is this data structures used? What is this data structure called in the context of programming languages?

Stack because LIFO call Stack

Describe two differences between the semantics of stacks and queues.

Stacks: LIFO access one side Queues: FIFO access both sides

Give the traditional terms for adding and removing elements from a queue.

enqueue dequeue

Provide a definition for the phrase exhaustive search with backtracking.

exploring all possibilities and backing up if we didn't find the solution

Suppose that p is a pointer to a node in a linked list of strings and the data of that node is the string "help". What is the value of that data after the following two statements? p -> data( ).erase (3, 1); p -> data( ).append ("lo");

hello

At a minimum what information does an activation record store?

local variables, parameters, return value

If data is a circular array of CAPACITY elements, and last is an index into that array, what is the formula for the index after last?

modulus of CAPACITY (last + 1) % CAPACITY

Draw a complete binary tree with exactly six nodes. Put a different value in each node. Next, assume an array implementation for the binary tree ADT and draw an array with six components showing where each of the six node values would be placed in the array.

1 2 3 4 5 6 Array: 1 | 2 | 3 | 4 | 5 | 6

Draw a full binary tree with at least 6 nodes

1 2 3 4 5 6 7

Below is a small binary tree: 14 2 11 1 3 10 30 7 40 Write the order of the nodes visited in: 1. An in-order traversal 2. A pre-order traversal 3. A post-order traversal 4. A level-order traversal

1. 1, 2, 3, 14, 7, 10, 11, 40, 30 2. 14, 2, 1, 3, 11, 10, 7, 30, 40 3. 1, 3, 2, 7, 10, 40, 30, 11, 14 4. 14, 2, 11, 1, 3, 10, 30, 7, 40

In general, what are the two components of the body of a recursive function or method?

1. Pair down problem (call to itself) 2. Stopping case

What is the minimum number of nodes in a full binary tree with depth 3?

15

Suppose that you are exploring a rectangular maze containing 15 rows and 12 columns. What is the maximum number of recursive calls that can be generated if you start at the entrance of the maze and call traverse_maze( )?

180

Consider this function declaration: void quiz (int i) { if (i > 1) { quiz (i/2) quiz(i/2) } std::cout << "*"; } How many asterisks are printed by the function call quiz(5)?

7

What is the minimum number of nodes in a complete binary tree with depth 3?

8

From a Christian perspective, provide an argument for how data structures inherently (i.e., regardless of who uses it or for what purpose) points to God.

Creator created humanity with ability to create. By creating, we imitate God. Data structures points to God specifically because it reveals how vast and complex, yet simple, God is. Bring order and manage data like God.

Consider the implementation of the queue using a circular array. What problem arises if we try to keep all the items at the front of a partially-filled array (i.e., so that data_[0] is always the front).

It is FIFO so you would have to shift the queue down to enqueue or dequeue. Becomes linear time instead of constant time.

What technique is used to provide the capability to step through items of a container class? Explain the two types of this technique and a pro and con of these types.

Iterator Internal and External Internal: Pro: simple, less management Con: only have one External: Pro: you can have more than one Con: management, have to create separate class

When a template class is implemented the entire implementation file is included in the header file. Why is this needed?

Makes the compiler's job easier The template prefix occurs before template class definitions and before each member function implementation

Suppose you have two iterators, s and t, over the same container and both **s and **t are 42. Will (s==t) always be true? Why or why not?

Not necessarily, the two iterators must be in the exact same spot in the container.

Which version of increment "++" (i.e., pre or post) is generally more efficient?

Prefix increment because it doesn't have to make a copy of the iterator

You have computed the set union of two sets with the sizes of 10 and 40 but the answer only has 42 items. How can this be?

The two sets must have had eight items in common.

The following code performs a set union of the elements in the sets actors1 and actors2 and puts the results in the set result. Explain why the inserter function is needed (i.e., rather than just having the 3rd line be result.begin( ))? set_union (actors1.begin( ), actors1.end( ), actors2.begin( ), actors2.end( ), inserter (result, result.begin( )));

To create an output iterator as an alternative way to insert new elements

Explain the advantage of template classes.

a single program can use different types for container classes

Considering the write_vertical and super_write_vertical discussed in the textbook, what is another way the super_write_vertical function could use recursion but not by calling itself?

add a '-', and have it call write_vertical

In the singly linked list implementation of the queue class having front_ptr and rear_ptr data members, where does the push member function place the new entry in the linked list? Why?

at the rear_ptr because it is easy to insert/remove at the head, and hard at the tail

Suppose we have a circular array implementation of the queue class, with ten items in the queue stored at data_[2] through data_[11]. The CAPACITY is 42. Where does the push member function places the new entry in the array?

data_[12]

Which queue operations, i.e., enqueue, dequeue, and is_empty, can result in a queue underflow? What are the condition when this would happen?

dequeue when there are no items in the queue

Consider the following code with enqueues and dequeues: const int CAPACITY = 5; queue<int> q (CAPACITY); q.enqueue(2); q.enqueue(4); q.enqueue(6); q.enqueue(8); q.enqueue(10); q.enqueue(); q.enqueue(); q.enqueue(12); Suppose that q is represented by a circular array. Draw the state of these private member variables of q after the above code has executed:

first_: [2] last_: [0] data_ | 12 | | 6 | 8 | 10 | [0] [1] [2] [3] [4]

Consider this prototype for a template function: template <class Item> void foo(Item x); Which is the right way to call the foo function with an integer argument i?

foo( i );

Why is it a bad idea to have a size_t parameter for a template function?

for some compilers, an int argument doesn't provide an exact match to size_t, and each argument must exactly match to the data type of the formal parameter

Consider the following code with enqueues and dequeues: const int CAPACITY = 5; queue<int> q (CAPACITY); q.enqueue(2); q.enqueue(4); q.enqueue(6); q.enqueue(8); q.enqueue(10); q.enqueue(); q.enqueue(); q.enqueue(12); Suppose that q is represented by a linked list. Draw the state of these private member variables of q after the above code:

front_ptr_ = 6 rear_ptr_ = 12

What property of a fractal lends itself to recursive thinking?

going back to the center of the line, repeating, until you reach a stopping case

Explain why an iterator using prefix notation returns the iterator while an iterator using postfix notation returns a copy.

prefix returns iterator after it has moved postfix returns a copy of the iterator before it has moved prefix is more efficient

Consider an implementation of a queue with a circular array, keeping track of first, last, and count (i.e., the number of items in the array). Suppose first is zero, and last is CAPACITY-1. What can be deduced about count?

queue is empty or full count = 0 or count = CAPACITY

Consider a queue implemented with a linked list, keeping track of a front pointer and a rear pointer. Which, if either, of these pointers will change during an insertion into a non-empty queue?

rear pointer if you insert at the front, you would have to remove from the back

Explain the advantage of template functions.

reuse; provides different usages for data types

Write a recursive function that has one parameter that is a non-negative integer. The function writes out that number of asterisks ('*') to the screen, followed by that number of exclamation points ('!'). Do not use any loops or local variables.

void function (unsigned int n) { if ( n >0 ) { std::cout << '*'; function (n - 1); std::cout << '!'; } }

Complete the body of this template function. Check the precondition as much as possible, and don't cause a heap leak. template<class Item> void list_head_remove(node<Item>*& head_ptr) // Precondition: head_ptr is the head pointer of a linked list, // with at least one node. // Postcondition: the head node has been removed and // returned to the heap; head_ptr is now the head pointer of // the new, shorter linked list.

{ assert (head_ptr != NULL) node<Item>* temp; temp = head_ptr; head_ptr = head_ptr -> link( ); delete temp; }

Implement the following function. Do not use any local variables or loops. void pattern (unsigned int n) // Precondition: n > 0; // Postcondition: The output consists of lines of integers. The first line is the number n. The next line is the number 2n. The next line is the number 4n, and so on until you reach a number that is larger than 4242. This list of numbers is then repeated backward until you get back to n. /* Example output with n = 840: 840 1680 3360 6720 6720 3360 1680 840 */

{ std::cout << n << std::endl; if ( n <= 4242 ) { pattern (2 * n); } std::cout << n << std::endl; }


Kaugnay na mga set ng pag-aaral

HESI: Delegation of Nursing Management and Rationale

View Set

Lección 8: Estructura: 8.2, 8.3, y 8.4

View Set

Scratch: Chapter 3--A Review of the Basic Components of Scratch Projects

View Set

Adult Development and Aging-Chapter 4

View Set

ARS 280 ToolingU Math Fundamentals 101

View Set

MKT 300 exam 1 PRACTICE QUESTIONS ONLY

View Set

Social Capstone exam 2 study guide

View Set

Chapter 28: Infection Prevention and Control

View Set

Prep U for Brunner and Suddarth's Textbook of Medical Surgical Nursing, 13th Edition Chapter 42: Management of Patients With Musculoskeletal Disorders

View Set