- (14 points) For each of the following questions circle the best answer from the
above list.
Sometimes it may seem as though two or more choices would
be equally good. In those cases think about the operations that the
data structures support and what I said about them in class. Then
choose the data structure whose operations
are best suited for the problem.
You may have to use the same answer for more than one
question.
a. vector
b. list
c. deque
d. map
e. set
f. multimap
g. multiset
h. hash table
- a b c d e f g h : The data structure you should use
to implement a mail folder that for each email
needs to store a key, which is the date (dates might
not be unique), and a value, which is a pointer to
an object that contains information about the email.
The mail in the folder should be ordered in ascending
order by date.
- a b c d e f g h The data structure you could use to determine whether
a device has already been registered on a network. You need to be
able to insert new devices into the data structure
and determine whether a device is in the data
structure. The devices do not need to be ordered.
- a b c d e f g h The data structure you could use to store rainfall amounts for each month of the year. Assume the input is a month, day, and amount and that you need to add the amount to the correct month. The month is an integer from 1-12. You do not have to store the day.
- a b c d e f g h The data structure you would use if you are maintaining a play list of songs and want to be able to insert a new song anywhere in the list or delete a song from anywhere in the list. The songs are not in alphabetical order--they are in any order the user prefers.
- a b c d e f g h The data structure you should use if you want to
simulate an unbounded
line of customers at a grocery store, where
customers are served (i.e., are removed)
from the front of the line and
customers enter (i.e., are added) at the back of the line.
- a b c d e f g h The data structure you should use to store race results
that are keyed on finish time and that store a value
which is the name of the runner with that finish time.
Finish times are unique and we need to be able to execute queries that return all runners in a certain time range (e.g., runners finishing
in 15-16 minutes).
- a b c d e f g h The data structure you should use
if you wanted to print out the last n lines of
a file where n is a command-line argument.
You should only hold n lines of the file in memory
at any time.
- (8 points) Suppose I have the following declarations and code:
int a, b;
int *x, *y;
Also suppose that the above variables are assigned the following memory addresses:
a: 0x1000
b: 0x1004
x: 0x1008
y: 0x100c
After the following code sequence executes, what are the values of a, b, x,
and y?
x = &b;
y = x;
a = 10;
b = 40;
*x = 30;
*y = *x * 3;
a:
b:
x:
y:
- (6 points) _____________________________________ What term is used to describe the problem with the following code? If you can't think of the term,
then for 3 points give a 2 sentence or less answer that describes the problem.
int *x;
x = new int;
*x = 40;
x = new int;
*x = 60;
- (10 points) Suppose I have the following declarations:
class PersonNode {
public:
string name;
PersonNode *next;
PersonNode(string n) : name(n), next(NULL) {}
}
PersonNode *x;
PersonNode *y;
PersonNode *newnode;
- Draw the diagram that results when the following code is executed.
Your diagram should look like the ones for linked structures in the class
notes and should include the three pointer variables x, y, and newnode, plus
any PersonNodes the code creates. If a variable has an unknown value, then
put a ? next to it.
newnode = new PersonNode("Frank");
x = newnode;
newnode = new PersonNode("Jackie");
newnode->next = x;
x = newnode;
- Execute the following, additional code and draw the resulting
diagram using the same instructions for drawing the diagram that were
given in part a. Do not start from scratch. This code builds on the code
that was executed in part a:
newnode = new PersonNode("Sue");
y = x->next;
newnode->next = y;
x->next = newnode;
- (10 points) Explain what is wrong with the following code segment. The intent of the code is to maintain a map keyed by name. For
each name we keep track of that person's friends. The code is
supposed to add friend2 to friend1's set of friends. You should assume
that the input is error-free.
Use no more than 3 sentences to describe the problem. You do not have to explain
how to fix the code, only what is wrong with it.
map<string, set<string> > friendsMap;
map<string, set<string> >::iterator friends_it;
set<string> friendsSet;
string friend1, friend2;
while (cin >> friend1 >> friend2) {
friends_it = friendsMap.find(friend1);
if (friends_it == friendsMap.end()) {
friendsMap.insert(make_pair(friend1, set<string>()));
friends_it = friendsMap.find(friend1);
}
friendsSet = friends_it->second;
friendsSet.insert(friend2);
}
- (10 points) You are given the following code fragment:
class Person {
public:
string name;
int age;
};
Person *p;
p->name = "Nancy";
p->age = 25;
Answer the following questions about the code fragment:
- Using 3 sentences or less, describe what
is wrong with this code fragment. The problem is not
with the class declaration--it is provided only for descriptive
purposes.
- What C++ statement(s) need to be added to this code fragment to
correct it and where should they be added. I want to see syntactically correct C++ code.
- (12 points) Your boss has asked you to choose between two algorithms. The
two algorithms have the following running times:
Algorithm 1: T(n) = 60n3 + 5n*log n + 50000n + 20
Algorithm 2: T(n) = 0.10 * 2n + n + 8
- What is the Big-O running time of Algorithm 1?
- What is the Big-O running time of Algorithm 2?
- The size of the input will be typically on the order
of n > 50. Which of the two algorithms should you choose and why?
- (10 points) You have been asked to print out all names in a multimap
whose salaries lie between $15,000 and $20,000.
The declaration for the multimap
is:
multimap<int, string> salaries;
The key is an integer denoting the person's salary and the value is
a string representing the person's name.
Below are two code fragments which will correctly print out the names
and salaries.
lower_bound(15000) returns an iterator that points to the first element
in the multimap whose salary is greater than or equal to 15,000.
lower_bound() has the same Big-O
running time as the find function.
| (a) | (b) |
multimap<int, string>::iterator mit;
for (mit = m.begin(); mit != m.end(); mit++) {
if (mit->first >= 15000 && mit->first <= 20000) {
cout << mit->second << " " << mit->first << endl;
}
}
|
multimap<int, string>::iterator mit;
mit = m.lower_bound(15000);
for (; mit != m.end(); mit++) {
if (mit->first > 20000)
return;
else
cout << mit->second << " " << mit->first << endl;
}
|
Make the following assumptions:
- There are n persons in the salaries multimap.
- n is a very large number (e.g., > 1,000).
- The number of salaries in the range 15000-2000 is capped by a
constant c (c < 50).
Answer the following questions:
- Which code segment is more efficient (a or b)?
- Explain why the code segment you selected is more efficient using
Big-O notation. You will need to compute the Big-O running time of
each of the two functions in order to justify your answer.
- (10 points) Draw a list diagram that shows what the following list looks
like pictorially after the following code executes.
Make sure that you include sentinel
nodes and that you show where each list iterator points. Your diagram
should look like the ones in the class notes.
list<string> names;
list<string>::iterator nit, nit1;
list<string>::reverse_iterator rnit;
names.push_back("joe");
names.push_back("mary");
names.push_front("nancy");
names.push_front("tom");
nit = names.begin();
nit++;
rnit = names.rbegin();
nit1 = names.end();