Practice Midterm Exam #1

Similar documents
The examination is open-book, and you may make use of any texts, handouts, or course notes. You may not, however, use a computer of any kind.

Practice Midterm Exam #2

Practice Midterm Examination #1

Practice Final Examination #2

CS106B Handout #25 J Zelenski Feb 11, Midterm Practice

CS106B Sprint 2017 April 27 th, 2017 CS106B Practice Midterm (KEY)

Practice Midterm Examination

Practice Final Examination #2

PRACTICE MIDTERM EXAM #2

CSE 143 Sample Midterm Exam #1

Practice Final Examination #1

CSE wi: Practice Midterm

Practice Midterm Examination #2

CS106X Handout 35 Winter 2018 March 12 th, 2018 CS106X Midterm Examination

CS106A Handout 18 Winter February 3, 2014 Practice Midterm Exam

Practice CS106B Midterm Solutions

Practice Final Examination

Practice Midterm #2. Midterm Time: Monday, July 18 th, 7pm 9pm Midterm Location: Hewlett 200

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

COS 126 General Computer Science Fall Written Exam 1

CS32 Final Exam. E03, F15, Phill Conrad, UC Santa Barbara

CSE 143 Lecture 10. Recursion

Final Exam Time: Friday, August 12th, 12:15pm - 3:15pm Final Exam Location: Split by last name

PREPARING FOR PRELIM 1

Practice Midterm Examination

Announcements. Recursion and why study it. Recursive programming. Recursion basic idea

Computer Science E-119 Fall Problem Set 1. Due before lecture on Wednesday, September 26

CSE 143, Winter 2010 Midterm Exam Wednesday February 17, 2010

CSE413 Midterm. Question Max Points Total 100

Winter 2018 March 12 th, 2018 CS106X Midterm Examination Solution

CSE 143 Sample Midterm Exam #4

Final exam. Final exam will be 12 problems, drop any 2. Cumulative up to and including week 14 (emphasis on weeks 9-14: classes & pointers)

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

CSCI-1200 Data Structures Spring 2014 Lecture 5 Pointers, Arrays, Pointer Arithmetic

CSCI 102L - Data Structures Midterm Exam #2 Spring 2011

CSE373 Fall 2013, Midterm Examination October 18, 2013

You must bring your ID to the exam.

- 1 - Handout #22S May 24, 2013 Practice Second Midterm Exam Solutions. CS106B Spring 2013

CS106X Handout 24 Winter 2018 February 9 th, 2018 CS106X Midterm Examination

CS 106B, Lecture 1 Introduction to C++

Practice Final Examination

Practice Midterm Examination

0.1 Welcome. 0.2 Insertion sort. Jessica Su (some portions copied from CLRS)

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

You will not be tested on JUnit or the Eclipse debugger. The exam does not cover interfaces.

COS 126 General Computer Science Spring Midterm 1

Introduction to Functional Programming. Slides by Koen Claessen and Emil Axelsson

Assignment #1 Simple C++

Parameters and Objects

CPSC 211, Sections : Data Structures and Implementations, Honors Final Exam May 4, 2001

Programming Languages and Techniques (CIS120)

CSCI-1200 Data Structures Fall 2017 Lecture 7 Order Notation & Basic Recursion

CSE 143, Winter 2009 Sample Midterm Exam #2

CSE 143 SAMPLE MIDTERM

CS106X Handout 17 Winter 2015 January 28 th, 2015 CS106X Practice Exam

COS 126 Exam Review. Exams overview Example programming exam Example written exam questions (part 1)

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012

Searching for Information. A Simple Method for Searching. Simple Searching. Class #21: Searching/Sorting I

Programming Languages and Techniques (CIS120)

CSE 143 Sample Midterm Exam #7 (11wi)

CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators)

UNIVERSITY REGULATIONS

15-122: Principles of Imperative Computation, Fall 2015

CSCI-1200 Data Structures Fall 2012 Lecture 5 Pointers, Arrays, Pointer Arithmetic

Due Thursday, July 18 at 11:00AM

Name CIS 201 Midterm II: Chapters 1-8

Eric Roberts Handout #39 CS 106B February 20, 2015 Sets

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #34. Function with pointer Argument

CSE 143 Section Handout #13 Practice Midterm #5

Section Handout 4. Problem One: Weights and Balances (Courtesy of Eric Roberts)

CS106A Handout 27 Winter February 26, 2014 Practice Second CS106A Midterm Exam

Section 05: Solutions

Practice Final Examination #1

CSE 143 SAMPLE MIDTERM SOLUTION

mypoly

CS3: Introduction to Symbolic Programming. Lecture 11: Tree Recursion, beginning lists, and Midterm 2. Spring 2007 Nate Titterton

Autumn 2012 October 29 th, 2012 CS106X Midterm Examination Solution

Learning Recursion. Recursion [ Why is it important?] ~7 easy marks in Exam Paper. Step 1. Understand Code. Step 2. Understand Execution

Assignment #1: /Survey and Karel the Robot Karel problems due: 1:30pm on Friday, October 7th

CPSC 2380 Data Structures and Algorithms

Mehran Sahami Handout #7 CS 106A September 24, 2014

CS106B Midterm Review

CS 305j Midterm 2 Fall 2006

Problem 1. Multiple Choice (choose only one answer)

Lecture 19: Recursion

Data Structures and Algorithms Winter Semester

Lecture 15 Binary Search Trees

UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS INFR08013 INFORMATICS 1 - FUNCTIONAL PROGRAMMING

CS171 Final Practice Exam

COS 226 Algorithms and Data Structures Spring Midterm Exam

CS106X Handout 39 Autumn 2012 November 28 th, 2012 CS106X Midterm Examination II

Midterm 2 Exam Principles of Imperative Computation. Tuesday 31 st March, This exam is closed-book with one sheet of notes permitted.

Chapter 13. Recursion. Copyright 2016 Pearson, Inc. All rights reserved.

Data Structures CSci 1200 Test 1 Questions

Midterm II Exam Principles of Imperative Computation Frank Pfenning. March 31, 2011

double d0, d1, d2, d3; double * dp = new double[4]; double da[4];

Lecture 27: Learning from relational data

CS 134 Programming Exercise 9:

CS106B Handout #20 Winter February 4, Section Handout #4

Transcription:

Eric Roberts Handout #23 CS106B January 28, 2013 Practice Midterm Exam #1 Review session: Sunday, February 3, 7:00 9:00 P.M., Hewlett 201 (next door) Midterm #1: Tuesday, February 5, 3:15 5:15 P.M., Braun Auditorium (Chemistry) Midterm #2: Tuesday, February 5, 7:00 9:00 P.M., CEMEX Auditorium (GSB) This handout is intended to give you practice solving problems that are comparable in format and difficulty to the problems that will appear on the midterm examination on Tuesday, February 5. A solution set to this practice exam will be handed out on Friday along with a second practice exam. In addition to these practice exams, the handouts section of the web site contains a guide (Handout #23J) written by my colleague Julie Zelenski that provides useful strategic advice for taking exams in CS106B. Time and place of the exam The midterm exam is scheduled for a two-hour block at two different times and places (note that the exams are not in the regular lecture room). You may take the exam at either time and need not give advance notice of which exam you plan to take. If you are unable to take the exam at either of the scheduled times or if you need special accommodations, please send an e-mail message to eroberts@cs stating the following: The reason you cannot take the exam at either of the scheduled times. A two-hour period next week at which you could take the exam. This time must be during the regular working day, and must therefore start between 8:30 and 3:00 (so that it ends by 5:00). In order to schedule an alternate exam, I must receive an e-mail message from you by 5:00P.M. on Thursday, January 31. Instructions for taking the midterm at an alternate time will be sent to you by e-mail on Friday. Review session There will be a general review session for the midterm on Wednesday, February 8, from 7:00 9:00 P.M. next door in Hewlett 201. At the review session, Dawson and some of the section leaders will go over problems from the practice exams, but you should also feel free to ask any additional questions that you have. Coverage The midterm covers the material presented in class through the lecture on Wednesday, January 30, which means that you are responsible for the chapters in the text through Chapter 10 ( Algorithmic Analysis ).

2 General instructions Answer each of the questions given below. Write all of your answers directly on the examination paper, including any work that you wish to be considered for partial credit. Each question is marked with the number of points assigned to that problem. The total number of points on the exam is 60. We intend for the number of points to be roughly comparable to the number of minutes you should spend on that problem. This leaves you with an hour to check your work or recover from false starts. In all questions, you may include functions or definitions that have been developed in the course. First of all, we will assume that you have included any of the header files that we have covered in the text. Thus, if you want to use a Vector, you can simply do so without bothering to spend the time copying out the appropriate #include line. If you want to use a function that appears in the book that is not exported by an interface, you should give us the page number on which that function appears. If you want to include code from one of your own assignments, we won t have a copy, and you ll need to copy the code to your exam. Unless otherwise indicated as part of the instructions for a specific problem, comments are not required on the exam. Uncommented code that gets the job done will be sufficient for full credit on the problem. On the other hand, comments may help you to get partial credit on a problem if they help us determine what you were trying to do. The examination is open-book, and you may make use of any texts, handouts, or course notes. You may not, however, use a computer of any kind. Problem 1: Tracing C++ programs and big-o (10 points) Assume that the functions Mystery and Enigma have been defined as follows: int mystery(int n) { if (n == 0) { return 1; else { return enigma(2, mystery(n - 1)); int enigma(int n1, int n2) { if (n1 == 0) { return 0; else { return n2 + enigma(n1-1, n2); (a) What is the value of mystery(3)? (b) What is the computational complexity of the mystery function expressed in terms of big-o notation, where N is the value of the argument n. In this problem, you may assume that n is always a nonnegative integer.

3 Problem 2: Vectors, grids, stacks, and queues (10 points) I produce the figures in my books by creating pictures in PostScript, a powerful graphics language developed by the Adobe Corporation in the early 1980s. PostScript programs store their data on a stack. Many of the operators available in the PostScript language have the effect of manipulating the stack in some way. You can, for example, invoke the pop operator, which pops the top element off the stack, or the exch operator, which swaps the top two elements. One of the most interesting (and surprisingly useful) PostScript operators is the roll operator, which takes two arguments: n and k. The effect of applying roll(n, k) is to rotate the top n elements of a stack by k positions, where the general direction of the rotation is toward the top of the stack. More specifically, roll(n, k) has the effect of removing the top n elements, cycling the top element to the last position k times, and then replacing the reordered elements back on the stack. Figure 1 at the bottom of the page shows before-and-after pictures for three different examples of roll. Your job in this problem is to write a function void roll(stack<char> & s, int n, int k) that implements the roll(n, k) operation on the character stack s. In doing so, you will probably find it useful to use other structures (stacks, queues, vectors, and so forth) as temporary storage. Your implementation should check to make sure that n and k are both nonnegative and that n is not larger than the stack size; if either of these conditions is violated, your implementation should call error with the message roll: argument out of range Note, however, that k can be larger than n, in which case the roll operation continues through more than a complete cycle. This case is illustrated in the final example in Figure 1, in which the top two elements on the stack are rolled four times, leaving the stack exactly as it started. Figure 1. Three examples of the roll operator

4 Problem 3: Lexicons, maps, and iterators (15 points) For the cell-phone mind-reading problem on Assignment #3, you wrote a procedure void listcompletions(string digits, Lexicon & lex); that printed all words from the lexicon that could be formed by extending the given digit sequence. Suppose that your boss has come back to you with a last-minute change in the product design a situation that happens all the time in the industry. The new plan is to embed this facility in a web service where many users may ask for such a list. The new concept has the following characteristics: Instead of printing the list of words to cout, the new version of the method must return it as a Vector<string>. Given that many customers in a particular telephone exchange will ask for the same prefix string, your boss now wants the implementation to store previously computed results in a map. The first time someone asks for the words generated by a given prefix, your implementation should compute the answer and store it under that prefix in the map. Thereafter, your implementation should simply return the stored value whenever asked about a prefix it has already seen. This technique is called caching. Your boss has also become somewhat wiser on the issue of efficiency. Having learned that asking for a recursive solution generated too much panic among the programmers, your manager is now willing to let you use the easy solution previously rejected as too inefficient. That solution, as it was expressed on the assignment handout, was to iterate through the words in the lexicon and print out every one that matches the specified digit string. In the redesign, the implementation has to add the word to a vector instead of printing it out, but the process is still far easier to implement than the recursive approach. Write an iterative function Vector<string> generatecompletions(string digits, Lexicon & lex, Map< string, Vector<string> > & cache); that implements this new design. The first two arguments are the same as before; the third is the map that holds the cache of previously computed results. This map will initially be empty, but will grow over time as new prefixes are added to the cache of previously computed results.

5 Problem 4: Recursive functions (10 points) As you saw in Chapter 1, it is easy to implement an iterative function raiseinttopower that computes n raised to the k th power: int raiseinttopower(int n, int k) { int result = 1; for (int i = 0; i < k; i++) { result *= n; return result; Rewrite this function so that it operates recursively, taking advantage of the following insight: If k is even, n k is the square of n raised to the power k / 2. If k is odd, n k is the square of n raised to the power k / 2 times n. In solving this problem, you need to identify the simple cases necessary to complete the recursive definition. You must also make sure that your code is efficient in the sense that it makes only one recursive call per level of the recursive decomposition. Problem 5: Recursive procedures (15 points) The game of dominos is played with rectangular pieces composed of two connected squares, each of which is marked with a certain number of dots. For example, each of the following five rectangles represents a domino: Dominos can be connected end-to-end to form chains, subject to the condition that two dominos can be linked together only if the numbers match. For example, you can form a chain consisting of all five of these dominos by connecting them in the following order: As in the traditional game, dominos can be rotated by 180 so that their numbers are reversed. In this chain, for example, the 1-6 and 3-4 dominos have been turned over so that they fit into the chain. Dominos can be represented in C++ using the following structure type: struct Domino { int leftdots; int rightdots; ; Given this domino type, write a recursive function

6 bool formsdominochain(vector<domino> & dominos); that returns true if it possible to build a chain consisting of every domino in the vector. For example, if you initialized the variable mydominos to contain the five dominot values shown at the top of the page, calling formsdominochain(mydominos) would return true because it is possible to form the chain shown in the second diagram. On the other hand, if you remove the first domino, calling formsdominochain(mydominos) returns false because there is no way to form a domino chain if the 1-4 domino is missing.