CIS-221 Test 3 ЁЯШЦЁЯФл
which version of increment "++" (pre or post) is generally more efficient
pre (++p); post (p++) makes a copy each time it activates, even if the return value is never used
in general, what are the two components of the body of a recursive function or method?
stopping case (case in which the entire computation is accomplished without recursion) and the recursive call
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?
(LAST + 1) % CAPACITY
explain why an iterator using prefix notation (++iter) returns the iterator while an iterator using postfix notation (iter++) returns a copy
++iter increments the value BEFORE returning the value iter++ makes a copy of iter before it's incremented, even if the return value is never used
what is the minimum number of nodes in a complete binary tree with depth 3
8 (?)
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 the top. { if (number < 0 ) { cout << '-' << endl; super_write_vertical(abs(number)); } else if (number < 10) { cout << number << endl; } else { super_write_vertical(number/10); cout << number % 10 << endl; } } what values of number are directly handled by the stopping case?
< 10 and >= 0
for the execution of recursive functions, the compiler and operating system store function invocation information. What data structure is used to store all the executions of the functions? Why is this data structure used? What is this data structure called in the context of programming languages?
A run-time stack it pushes the next activation record to the top of the run-time stack, useful because it uses LIFO instead of FIFO
why is it a bad idea to place USING directives in the implementation of a template class
because there can be conflicts with names in that namespace; could cause naming conflicts
give the traditional terms for adding and removing elements from a queue
enqueue- add dequeue- remove
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?
just rear
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 [1, 2, 3, 4, 5, 6]
Draw a full binary tree with at least 6 nodes
1 / \ 2 3 / \ / \ 4 5 6 7
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()?
15 x 12 is the upper bound
consider this function declaration: void quiz(int i) { if (i > 1) { quiz(i/2); quiz(i/2); } cout << "*"; } how many asterisks are printed by the function call quiz(5)?
7
suppose you have two iterators, s and t, over the same container and both *s and *t equal 42. Will (s == t) always be true? Why or why not?
Both places have the same data, but the corresponding links are not the same; just because the data is the same, doesn't mean the links will be the same
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
Internal/External Iterators; internal iterator: Uses member functions to access items pro: simpler to manage con: set number of iterators (typically 1) external: pro: can have any number of iterators con: create separate classes and write methods (more complex)
describe the two differences between the semantics of stacks and queues
LIFO (Stack)- one end to enter elements into FIFO(queue)- two ends to enter elements into
below is a small binary tree 14 /\ 2 11 /\ /\ 1 3 10 30 / / 7 40 circle all the leaves. Put a square box around the root. Draw a star above each ancestor of the node that contains 10. Put a big X through every descendant of the node that contains 10.
Leaves: 1, 3, 7, 40 Root: 14 Ancestors of 10: 11, 14 Descendants of 10: 7
what property of a fractal lends itself to recursive thinking
Recursion is permitting a function implementation to contain a call to itself A fractal is a pattern that produces a picture, which contains an infinite amount of copies of itself.
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 place the new entry in the array?
The new elements are inserted at data[12]
explain the advantage of template functions
allows for several different versions of a function to be created, eliminating the need to create multiple functions when just one can be used
at a minimum, what information does an activation record store?
contains info as to where the function should return after computation
consider the following code with ENQUEUES and DEQUEs const int CAPACITY =5; queue<int> q (CAPACITY); q.enqueue (2); q.enqueue (4); q.enqueue (6); q.enqueue (8); q.enqueue (10); q.dequeue ( ); q.dequeue ( ); q.enqueue (12); Suppose that q represented by a circular array. Draw the state of these private member variables of q after the above code has executed: first_: last_:
data_[0]: 12 data_[1]: data[2]: 6 data[3]: 8 data[4]: 10 first_: data[2] last_: data[0]
which queue operations (enqueue/dequeue, and is_empty), can result in a queue underflow? What are the conditions when this would happen?
dequeue, when is_empty is true
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<int>(i)
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
func(int times) { if (n > 0) { cout << "*"; func(n-1); cout << "!"; } }
consider an implementation of a queue with a circular array, keeping track of first, last, and count (the number of items in the array). Suppose first is zero, and last is CAPACITY-1. What can be deduced about count?
it could be 0 or full (at capacity); it could be empty or full
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?
it could call write_vertical in the function instead of super_write_vertical
in the singly linked list implementation of the queue class having the front_ptr_ and rear_ptr_ data members, where does the PUSH member function place the new entry in the linked list? Why?
places it at the rear of the queue, because it is easy to be inserted at the rear but to remove something at the rear requires for the whole list to be traversed to assign rear_ptr to the next value
provide a definition for the phrase EXHAUSTIVE SEARCH WITH BACKTRACKING
the term EXHAUSTIVE SEARCH means that all possibilities are tried. Backtracking is the process of a recursive call returning without finding the goal. When such a recursive call returns, we are back where we started, and we can explore other directions with further recursive calls.
you have computed the set union of two sets with sizes of 10 and 40 but the answer only has 42 items. How can this be?
the two sets had 8 items in common
when a template class is implemented the entire implementation file is included in the header file. Why is this needed?
to make the compiler's job possible; needs to have/know the template implementation code so it can get the ACTUAL implementation code
explain the advantage of template classes
when a class depends on an underlying data type, the class can be implemented as a template class, which gives the class the ability for several different versions of a class to be created from a single class, like a template function
does the compiler generate and compile the implementation of a template function or class when it encounters the implementation or when the implementation is instantiated (when the implementation is invoked) in the code? Why is this?
when implementation is invoked because data types must be filled in to generate code
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 (so data_[0] is always the front)
whenever the first item is removed, all other items must shift so the next item is at data[0]