Exam Principles of Imperative Computation, Summer 2011 William Lovas. June 24, 2011

Similar documents
Exam Principles of Imperative Computation, Summer 2011 William Lovas. June 24, 2011

Exam I Principles of Imperative Computation, Summer 2011 William Lovas. May 27, 2011

Final Exam Principles of Imperative Computation Frank Pfenning, Tom Cortina, William Lovas. December 10, Name: Andrew ID: Section:

Midterm I Exam Principles of Imperative Computation Frank Pfenning. February 17, 2011

Midterm I Exam Principles of Imperative Computation André Platzer Ananda Gunawardena. February 23, Name: Andrew ID: Section:

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

Lecture Notes on Binary Decision Diagrams

Midterm I Exam Principles of Imperative Computation Frank Pfenning, Tom Cortina, William Lovas. September 30, 2010

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

Midterm I Exam Principles of Imperative Computation André Platzer Ananda Gunawardena. February 23, 2012

CSE 143 Final Exam Part 1 - August 18, 2011, 9:40 am

Lecture Notes on Dynamic Programming

CSE 250 Final Exam. Fall 2013 Time: 3 hours. Dec 11, No electronic devices of any kind. You can open your textbook and notes

CSE 373 Winter 2009: Midterm #1 (closed book, closed notes, NO calculators allowed)

11 'e' 'x' 'e' 'm' 'p' 'l' 'i' 'f' 'i' 'e' 'd' bool equal(const unsigned char pstr[], const char *cstr) {

CSE 333 Midterm Exam 2/14/14

Midterm 1 Solutions Principles of Imperative Computation. Thursday 6 th October, 2016

Lecture Notes on Tries

Midterm 1 Exam Principles of Imperative Computation. Thursday 6 th October, This exam is closed-book with one sheet of notes permitted.

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

Lecture Notes on Tries

: Principles of Imperative Computation Victor Adamchik. Practice Exam - I

Computer Organization & Systems Exam I Example Questions

CSE 373 Autumn 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

Computer Science Foundation Exam

CSE 373 Spring 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

Computer Science Foundation Exam

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

Final Examination CSE 100 UCSD (Practice)

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

Exam 1 Practice CSE 232 Summer 2018 (1) DO NOT OPEN YOUR EXAM BOOKLET UNTIL YOU HAVE BEEN TOLD TO BEGIN.

Foundation Exam - Sample Problems for New Topics

Do not start the test until instructed to do so!

Computer Science Foundation Exam

Final Exam in Algorithms and Data Structures 1 (1DL210)

COMPUTER APPLICATION

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

Solution printed. Do not start the test until instructed to do so! CS 2604 Data Structures Midterm Spring Instructions:

CS61c Summer 2014 Midterm Exam

Introduction to Computer Systems. Exam 1. February 22, This is an open-book exam. Notes are permitted, but not computers.

Final Exam. COMP Summer I June 26, points

Second Examination Solution

Solution to CSE 250 Final Exam

General Syntax. Operators. Variables. Arithmetic. Comparison. Assignment. Boolean. Types. Syntax int i; float j = 1.35; int k = (int) j;

CSE100 Practice Final Exam Section C Fall 2015: Dec 10 th, Problem Topic Points Possible Points Earned Grader

Solution printed. Do not start the test until instructed to do so! CS 2604 Data Structures Midterm Summer I Instructions:

a) Write the signed (two s complement) binary number in decimal: b) Write the unsigned binary number in hexadecimal:

Read this before starting!

University of Toronto Department of Electrical and Computer Engineering. Midterm Examination. ECE 345 Algorithms and Data Structures Fall 2010

York University AS/AK/ITEC INTRODUCTION TO DATA STRUCTURES. Midterm Sample I. Examiner: S. Chen Duration: One Hour and 30 Minutes

Contents of Lecture 3

CS 373: Combinatorial Algorithms, Spring 1999

Midterm Exam Answers Instructor: Randy Shepherd CSCI-UA.0201 Spring 2017

Wawrzynek & Weaver CS 61C. Sp 2018 Great Ideas in Computer Architecture MT 1. Print your name:,

University of California, Berkeley College of Engineering

Computer Science Foundation Exam

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Prelim 2 SOLUTION. 5:30 PM, 25 April Total Question Name Short Search/ Collections Trees Graphs

Prelim 2, CS :30 PM, 25 April Total Question Name Short Search/ Collections Trees Graphs

C Introduction. Comparison w/ Java, Memory Model, and Pointers

Prelim 2. 5:30 PM, 25 April Total Question Name Short Search/ Collections Trees Graphs. Max Score Grader

Lecture Notes on Tries

CS 222/122C Fall 2016, Midterm Exam

Department of Electrical Engineering and Computer Sciences Spring 2007 Instructor: Dr. Dan Garcia

: Principles of Imperative Computation. Summer Assignment 4. Due: Monday, June 11, 2012 in class

CS1800 Discrete Structures Final Version B

CS 112 Final May 8, 2008 (Lightly edited for 2011 Practice) Name: BU ID: Instructions GOOD LUCK!

Computer Science 302 Spring 2007 Practice Final Examination: Part I

Do not start the test until instructed to do so!

Introduction to Computer Systems. Exam 1. February 22, Model Solution fp

Wawrzynek & Weaver CS 61C. Sp 2018 Great Ideas in Computer Architecture MT 1. Print your name:,

ECE264 Fall 2013 Exam 3, November 20, 2013

CSL 201 Data Structures Mid-Semester Exam minutes

COS 226 Algorithms and Data Structures Fall Midterm

Friday Four Square! 4:15PM, Outside Gates

CS 112 Final May 8, 2008 (Lightly edited for 2012 Practice) Name: BU ID: Instructions

Introduction to C++ with content from

CS , Fall 2001 Exam 2

University of Illinois at Urbana-Champaign Department of Computer Science. Final Examination

Section 1: True / False (2 points each, 30 pts total)

Bits, Bytes and Integers

CSE 303 Midterm Exam

COMP 250 Midterm #2 March 11 th 2013

University of Illinois at Urbana-Champaign Department of Computer Science. Second Examination

Introduction to Algorithms October 12, 2005 Massachusetts Institute of Technology Professors Erik D. Demaine and Charles E. Leiserson Quiz 1.

ECE 2030D Computer Engineering Spring problems, 5 pages Exam Two 8 March 2012

CSE413 Midterm. Question Max Points Total 100

ECE 2035 Programming HW/SW Systems Fall problems, 5 pages Exam Three 28 November 2012

ECE 2035 A Programming HW/SW Systems Spring problems, 5 pages Exam Three 13 April Your Name (please print clearly)

ECE 2035 Programming HW/SW Systems Fall problems, 4 pages Exam Three 14 November Your Name (please PRINT clearly) Signed.

CPSC 121 Some Sample Questions for the Final Exam Tuesday, April 15, 2014, 8:30AM

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

University of Illinois at Urbana-Champaign Department of Computer Science. Final Examination

Final CSE 131B Spring 2004

Introduction to Computer Systems. Exam 2. April 10, Notes and calculators are permitted, but not computers.

ECE264 Spring 2013 Final Exam, April 30, 2013

Read this before starting!

Need Backtracking! L 0 A 1 B 2 K 3 P 4 C 5 I 6 Z 7 S 8 A 9 N 10 X 11 A 12 N 13 X 14 P 15

Computer Science Foundation Exam

CSE 30 Fall 2013 Final Exam

Transcription:

Exam 3 15-122 Principles of Imperative Computation, Summer 2011 William Lovas June 24, 2011 Name: Andrew ID: Instructions This exam is closed-book with one double-sided sheet of notes permitted. You have 80 minutes to complete the exam. There are 4 problems. Read each problem carefully before attempting to solve it. Do not spend too much time on any one problem. Consider if you might want to skip a problem on a first pass and return to it later. Consider writing out programs or diagrams on scratch paper first. And most importantly, DON T PANIC! Generic Integers Data Tries BDDs Prob 1 Prob 2 Prob 3 Prob 4 Total Score Max 30 40 40 40 150 Grader 1

1 Integers (30 points) Task 1 (15 pts). For each of the following expressions in C, indicate whether they are always true. If not, give a counterexample and state whether the counterexample is guaranteed to be false or undefined in C. You should not assume any particular size for ints and state your counterexamples in terms of INT_MAX, INT_MIN, and UINT_MAX. Assume we are in the scope of declarations int x =...; unsigned int u =...; so x and u are properly initialized to arbitrary values. We have filled in one row for you. C expression Always true? If no, counterexample False or undefined x + 1 == 1 + x no x == INT_MAX undefined x <= 0 -x < 0 x >= 0 -x > 0 u + 1 == 1 + u u + 1 > u u - 1 == -(1 - u) Task 2 (10 pts). Write a C function safe_to_add which returns true if adding the two arguments would be well-defined and false otherwise. bool safe_to_add(int x, int y) { } 2

Task 3 (5 pts). The following function attempts to build an int out of two shorts by putting the bit pattern of the first short next to the bit pattern of the second short. It makes the following assumptions about the C implementation: sizeof(int) == 4, sizeof(short) == 2, bytes are 8 bits, and all signed integer types are represented using two s complement. Even under these assumptions, the function has a bug. Explain briefly both the problem and the fix. int build_int(short s1, short s2) { } int res = 0; res = res (int) s1; res = res << 16; res = res (int) s2; return res; 3

2 Generic Data (40 points) A pointer to data of unknown type may be represented in C using the type void *, but any such pointer must be cast appropriately before it can be used. Task 1 (5 pts). Suppose we declare a generic pointer void *p; Draw a picture demonstrating the difference between ((char *) p) + 1 and ((int *) p) + 1. 4

Consider binary search trees implemented with generic elements. struct tree { struct tree *left; void *data; struct tree *right; /*... other fields... */ } typedef struct tree * tree; An in-order traversal of a binary tree is one that first visits all elements in the left subtree of a node, then visits the node itself, and finally visits all elements in the right subtree of a node. Task 2 (10 pts). Complete the traverse function which performs an in-order traversal on a binary search tree containing generic data. The second argument, visit, should be a pointer to a function taking generic data and returning nothing. void traverse(tree T, visit ) { } 5

Suppose we wish to store a roster of students in a binary search tree. struct student { char *name; int id; char *major; }; typedef struct student * student; Task 3 (5 pts). Write a print_student function suitable for passing to traverse (i.e., of the appropriate type for a visit function) that prints a student in the following format: Name: William Lovas, ID: 12345, Major: Computer Science Task 4 (5 pts). Suppose our roster is stored in a tree: tree roster =...; Show how to call traverse to print all of the students in the roster using your print_student function. 6

Now suppose we wish to store pointers to ints in a binary search tree. Task 5 (5 pts). Write a print_intptr function suitable for passing to traverse (i.e., of the appropriate type for a visit function) that prints the pointed-to integer. Task 6 (10 pts). Suppose we have an array A of n distinct integers. Using traverse and print_intptr, write a function that prints the integers in sorted order without sorting the array A itself. You may also assume the following tree functions: tree itree_new(); /* construct a new BST storing int pointers */ tree itree_insert(tree T, void *data); /* insert data into T, returning a new tree */ void itree_free(tree T); /* free the storage associated with tree T */ void print_in_order(int *A, int n) { } 7

3 Tries (40 points) Tries are an efficient data structure for storing sets of strings. We saw in class the ternary search trie, a data structure combining ideas from ordinary multi-way tries and binary search trees. Task 1 (15 pts). Construct the ternary search trie (TST) that results from inserting the following strings into an initially empty TST in order: malt, milk, mall, mill, apple, app, malted You only need to show the final trie, but if you show intermediate steps we may more easily assign partial credit. 8

Task 2 (10 pts). Describe an algorithm for checking whether any string with a given prefix is stored in a trie. For example, given the trie from Task 1, it should return true for the prefix ma, but false for the prefix mu. You do not need to write code though you may if you wish but your description should be precise enough that it would be clear how to write the code. Task 3 (5 pts). What is the asymptotic complexity of your algorithm? Explain briefly. 9

Task 4 (10 pts). Describe how you would extend your algorithm above to collect all strings that begin with a certain prefix (e.g., to perform UNIX-style tab completion or predictive text input). 10

4 Binary Decision Diagrams (40 points) An important operation in the construction of digital circuits is the nor operation, 1 which inputs booleans x 1 and x 2 and outputs (x 1 x 2 ). We write nor(x 1, x 2 ). In C, this might be defined as bool nor(bool x1, bool x2) { return!(x1 x2); } Task 1 (15 pts). Construct an ordered binary decision tree (BDT, i.e., without sharing) for the formula nor(x 1, nor(x 2, x 3 )), where the variables are tested in the order x 1, x 2, x 3. x 1 1 x 2 x 2 1 1 x 3 1 x 3 1 x 3 1 x 3 1 1 The first embedded system, the Apollo Guidance Computer, was built exclusively out of NOR gates. 11

Task 2 (10 pts). Ordered binary decision diagrams (OBDDs) are like binary decision trees that may exploit sharing. State the two conditions required for a BDD to be fully reduced (i.e., to be an ROBDD), explaining each briefly. 1. 2. Task 3 (15 pts). Convert your binary decision tree from Task 1 into a fully-reduced ROBDD, where the variables are tested in the same order. You only need to show the final ROBDD, but if you show intermediate steps we may more easily assign partial credit. 12