2013 CS140 Final Exam


Question 1 - Basic Programming

Write the procedure get_n_rev() with the prototype to the right.

This procedure should return a vector composed of the last n lines of standard input in reverse order. If standard input has fewer than n lines, it should return all of the lines of standard input in reverse order.

You may use any data structure from the standard template library or from class (I've included them all on the cheat sheet). If standard input contains l lines, your program should run in O(l) time and O(n) space. You will lose half credit otherwise.

vector <string> get_n_rev(int n);

Question 2 - Basic Data Structures and Big-O

Tell me the running time of the following actions in terms of Big-O. Use the answer sheet provided. If the answer depends on the state of the data structure, give the worst-case.

Question 3 - Were you in this class?

Recall the Towers of Hanoi problem from class/lecture notes. We had a class Towers that had a public method called Make_Move() with the following prototypes:

void Make_Move(int from, int to);

This double-checks the legality of moving a disk from tower from to tower to, where from and to are 0, 1 or 2. If the move is legal, it prints the move and moves the top disk from tower from to tower to.

Write a procedure called Solve() with the following prototype:

void Solve(Towers *t, int from, int to, int npieces);

This procedure should make the proper Make_Move() calls on the instance t to move the top npieces disks from tower from to tower to.