PROBLEM 1 : (Vocabulary: 8 points) For each of the words/phrases below, circle the denition that is the best description as it pertains in the context

Similar documents
"apple" "grape" "grape" "grape" "apple"

PROBLEM 1 : (Vocabulary: 8 points) For each of the words/phrases below, circle the denition that is the best description as it pertains in the context

cameron grace derek cameron

Before calling Prepend(list,13) After calling Prepend(list,13) After calling RemoveLast(list)

Test 2: CPS 103 Owen Astrachan November 19, 1993 Name: Honor code acknowledgement (signature) Problem 1 value 9 pts. grade Problem 2 12 pts. Problem 3

mypoly

"sort A" "sort B" "sort C" time (seconds) # of elements

Test 2: CPS Owen Astrachan. November 17, Name: Honor code acknowledgement (signature)

Test #2. Login: 2 PROBLEM 1 : (Balance (6points)) Insert the following elements into an AVL tree. Make sure you show the tree before and after each ro

PROBLEM 1 : (ghiiknnt abotu orsst (9 points)) 1. In the Anagram program at the beginning of the semester it was suggested that you use selection sort

PROBLEM 1 : (Short Answer: 13 points) Three points each, except for last two which are two points 1. Both bubble-sort and selection sort are O(n 2 ) s

Test 1: CPS 100. Owen Astrachan. October 11, 2000

class for simulating a die (object "rolled" to generate a random number) Dice(int sides) -- constructor, sides specifies number of "sides" for the die

Test #1. Login: Initials: 2 PROBLEM 1 : (The Sorted Link (12 points)) Using the following definition of a linked list node, write a function that inse

Test 1: CPS 100. Owen Astrachan. February 23, 2000

Test 1: CPS 08 Owen Astrachan February 17, 1995 Name: Honor code acknowledgement (signature) Problem 1 Problem 2 Problem 3 Problem 4 Problem 5 Extra T

Test 2: CPS 100. Owen Astrachan. November 15, 2000

PROBLEM 1 : (Vocabulary: 12 points) For each of the words/phrases below, circle the denition that is the best description as it pertains in the contex

Test 1: Compsci 100. Owen Astrachan. February 15, 2006

Test 2: CPS 100. Owen Astrachan. April 3, Name: Login: (1 pt)

Test 1: CPS 100. Owen Astrachan. October 1, 2004

PROBLEM 1 : (I'll huff and I'll puff and I'll... (10 pts)) The Huffman tree below is used for this problem. e s r o t Part A(4 points) Using this tree

Part I: Short Answer (12 questions, 65 points total)

Name: I. 20 II. 20 III. 20 IV. 40. CMSC 341 Section 01 Fall 2016 Data Structures Exam 1. Instructions: 1. This is a closed-book, closed-notes exam.

Computer Science Foundation Exam

Object Oriented Programming COP3330 / CGS5409

CS 216 Exam 1 Fall SOLUTION

Computer Science CS221 Test 2 Name. 1. Give a definition of the following terms, and include a brief example. a) Big Oh

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)

Computer Science Foundation Exam

PROBLEM 1 : (Trouble?: (14 pts)) Part A: (6 points) Consider the function Trouble below. int Trouble (const Vector<int> & numbers, int size) if (size

CSE 333 Midterm Exam Cinco de Mayo, 2017 (May 5) Name UW ID#

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

CS 311 Data Structures and Algorithms, Fall 2009 Midterm Exam Solutions. The Midterm Exam was give in class on Wednesday, October 21, 2009.

CS 2150 Exam 1, Spring 2018 Page 1 of 6 UVa userid:

CS212 - COMPUTATIONAL STRUCTURES AND ALGORITHMS

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

Dynamic Data Structures

CMSC 341 Lecture 10 Binary Search Trees

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

Definition of Stack. 5 Linked Structures. Stack ADT Operations. ADT Stack Operations. A stack is a LIFO last in, first out structure.

First Examination. CS 225 Data Structures and Software Principles Spring p-9p, Tuesday, February 19

CS 61B, Spring 1996 Midterm #1 Professor M. Clancy

ADT Sorted List Operations

CSC 210, Exam Two Section February 1999

2003 AP COMPUTER SCIENCE AB FREE-RESPONSE QUESTIONS

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

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

Outline. User-dened types Categories. Constructors. Constructors. 4. Classes. Concrete classes. Default constructor. Default constructor

Computer Science E-22 Practice Final Exam

CS 151. Linked Lists, Recursively Implemented. Wednesday, October 3, 12

Questions: ECE551 PRACTICE Final

CSE 143. Linked Lists. Linked Lists. Manipulating Nodes (1) Creating Nodes. Manipulating Nodes (3) Manipulating Nodes (2) CSE 143 1

CS 61B, Spring 1996 Midterm #1 Professor M. Clancy

And Even More and More C++ Fundamentals of Computer Science

Containers: Stack. Jordi Cortadella and Jordi Petit Department of Computer Science

Containers: Stack. The Stack ADT. The Stack ADT. The Stack ADT

PRACTICE MIDTERM EXAM #2

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

CSCE 2014 Final Exam Spring Version A

Computer Science 62. Bruce/Mawhorter Fall 16. Midterm Examination. October 5, Question Points Score TOTAL 52 SOLUTIONS. Your name (Please print)

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

ADTs Stack and Queue. Outline

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

Midterm Exam 2 CS 455, Spring 2011

Tutorial Letter 102/1/2012 Programming: Data Structures

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

COS 226 Algorithms and Data Structures Spring Midterm Exam

Test 2: Compsci 100. Owen Astrachan. April 2, 2008

ADT Unsorted List. Outline

What is a List? elements, with a linear relationship. first) has a unique predecessor, and. unique successor.

CS 311 Data Structures and Algorithms, Spring 2009 Midterm Exam Solutions. The Midterm Exam was given in class on Wednesday, March 18, 2009.

Midterm Examination. Instructor: Gary Chan Date: Saturday, 23 October 2010 Time: 2:30pm 4:00pm Venue: LTC

Test 2: CPS 100. Owen Astrachan. November 10, 2004

CS 455 Midterm 2 Fall 2017 [Bono] Nov. 7, 2017

CS 455 Final Exam Spring 2015 [Bono] May 13, 2015

Data Structures. BSc in Computer Science University of New York, Tirana. Assoc. Prof. Marenglen Biba 1-1

MS OE. Quizzes. Name:

Computer Science Foundation Exam

You must include this cover sheet. Either type up the assignment using theory3.tex, or print out this PDF.

Problem 1: Get val soon! (15 marks)

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

Test 2: Compsci 100. Owen Astrachan. November 14, 2006

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

Computer Science E-119 Fall Problem Set 3. Due before lecture on Wednesday, October 31

Cpt S 122 Data Structures. Course Review FINAL. Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University

EECE.3220: Data Structures Spring 2017

CS171 Midterm Exam. October 29, Name:

Instantiation of Template class

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

! 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)

ICOM 4035 Data Structures. Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department

CMSC 202 Final May 19, Name: UserID: (Circle your section) Section: 101 Tuesday 11: Thursday 11:30

DUKE UNIVERSITY Department of Computer Science. Test 1: CompSci 100

CSC1322 Object-Oriented Programming Concepts

Data Structures And Algorithms

Name: Username: I. 10. Section: II. p p p III. p p p p Total 100. CMSC 202 Section 06 Fall 2015

Q1: /8 Q2: /30 Q3: /30 Q4: /32. Total: /100

C:\Temp\Templates. Download This PDF From The Web Site

C++ Addendum: Inheritance of Special Member Functions. Constructors Destructor Construction and Destruction Order Assignment Operator

Transcription:

Test 1: CPS 100 Owen Astrachan February 12, 1996 Name: Honor code acknowledgement (signature) Problem 1 Problem 2 Problem 3 Problem 4 Extra TOTAL: value 8 pts. 18 pts. 13 pts. 16 pts. 6 pts. 57 pts. grade This test has 8 pages, be sure your test has them all. Do NOT spend too much time on one question remember that this class lasts 50 minutes. 1

PROBLEM 1 : (Vocabulary: 8 points) For each of the words/phrases below, circle the denition that is the best description as it pertains in the context of computer science, programming, and C/C++. 1. copy constructor 2. this (a) A member function used to construct an object (an instance of a class) from another object. (b) A member function called when one object is is assigned to another using operator =. (c) A member function that serves as a template for all other constructors to eliminate duplicated code. (a) A pointer used in member functions to prevent local variables in the function from aliasing data members of the class. (b) A pointer used in member functions to refer to the object that is being aected by the member functions, e.g., the object on the left of the. when a member function is invoked on an object. (c) A pointer used in the destructor of a class to ensure that all private data members are deleted appropriately. 3. friend (a) A way of circumventing the use of const for constant objects when a member function isn't declared as a const function. (b) Away of circumventing the use of private for specic functions and classes to have access to the private section of another class. (c) A way of circumventing the use of templates so that a non-templated class can be implemented as a templated class. 4. QuickSort (a) A worst-case O(n log n) sort with good average case performance. (b) An average-case O(n log n) sort whose worst-case is O(n 2 ), but with good performance in practice. (c) A theoretically optimal sort whose performance in practice is not as good as selection sort for sorting large amounts of data PROBLEM 2 : (Linkin' Logs: 18 points) The following declaration for a Node is given, linked lists are implemented without dummy-header nodes. struct Node string info; Node * next; Node(string val, Node * ptr = 0) : info(val), next(ptr) ; // stores data in node // points to next node // constructor 2

Part A (4 points) The function CountWord is intended to return the number of occurrences of word in the linked-list pointed to by rst. The function always returns 1 more than the correct answer. Describe how toxcountword so that it works as intended. int CountWord(Node * first, const string & word) // postcondition: returns # of occurrences of word in list pointed // to by first int count = 0; if (first == 0) return 1; else if (first->info == word) count++; return count + CountWord(first->next); Part B (6 points) Write the function Copy whose header is given below; Copy returns a pointer to a newly constructed linked list that is a copy of the list pointed to by the parameter rst. (hint: it's easier to do this recursively than iteratively) Node * Copy(Node * first) // postcondition: returns a copy of the list pointed to by first 3

Part C (8 points) In this problem assume that lists are implemented using dummy/header nodes. Write the function MoveToFront that nds the rst node containing word and unlinks this node from the list, making it the rst node of the list. For example, if list represents ("bear","ape","cat","dog"), the call MoveToFront(list,"cat") makes list represent ("cat","bear","ape","dog"). void MoveToFront(Node * list, const string & word) // precondition: list has a dummy/header node // postcondition: first occurrence of word in list has been moved // to the front of list as the first node 4

PROBLEM 3 : (Jerry's house in Philadelphia: 13 points) Part A(3 points) What is the value of the postx expression: 13 8 6 2 * + 7 - + Part B (8 points) A Stack class can be implemented using a vector, a linked list, or using other ways of storing the data in the stack. Using only the member functions IsEmpty(), MakeEmpty(), Top(), Pop(), and Push(), write the assignment operator whose header is given below (the header le for the stack class in the book is provided at the end of the test). The general method you should use for s = rhs is: make the stack being assigned to empty make a reversed-duplicate of the stack rhs by popping all the elements of rhs onto a temporary stack pop the elements from the temporary stack to the stack being assigned to You must also complete the while loop of the function ReverseStack so that it works as intended. template <class Etype> ReverseStack(const Stack<Etype> & s, Stack<Etype> & copy) // precondition: s is a1, a2,..., an (a1 on top of stack) // postcondition: copy is an,..., a2, a1 (an on top of stack) Stack<Etype> temp(s); // copy of s to pop copy.makeempty(); // make sure copy is empty // loop until temp is empty, popping all elements onto copy template <class Etype> Stack<Etype>::operator Stack<Etype> temp; MakeEmpty(); = (const Stack<Etype> & rhs) // temporary stack // make myself empty // copy elements from rhs onto temp (reversed), then back to myself // so that I'm a copy of rhs [note: use ReverseStack] 5

Part C (2 points) What is the complexity (using big-oh) of the assignment operator described in Part B for assigning a stack ofnelements, briey justify your answer. PROBLEM 4 : (Sequences: 16 points) A member function Length for the sequence class could be implemented simply as int Sequence::Length() // postcondition: returns number of elements in sequence return mysize; However, if you don't have access to the class you cannot add a new member function. Part A (5 points) Write a non-member function Length that returns the number of elements in the sequence s. The header les for the Sequence and SequenceIterator classes are provided at the end of the test. int Length(const Sequence & s) // postcondition: returns number of elements in s Part B (8 points) For two sequences s and t by denition s<tif the sequences have the same length and each element ofsis less than the corresponding element oft.for example s t s<t (1,3,5) (2,4,7,8) false, not the same length (1,3,5) (2,4,7) true (1,3,5) (2,4,5) false, 5 isn't less than 5 (continued) 6

Write the function operator < whose header is given below. In writing the function assume that Length works as specied, regardless of what you wrote in part A. bool operator < (const Sequence & lhs, const Sequence & rhs) // postcondition: returns true if lhs < rhs, otherwise returns false Part C (3 points) Write the function that implements operator >, dened to be consistent with the denition of operator <. The body of operator > MUST be only one statement. In writing the function, assume that operator < works as specied, regardless of what you wrote in Part B. bool operator > (const Sequence & lhs, const Sequence & rhs) // postcondition: returns true if lhs > rhs, otherwise returns false 7

PROBLEM 5 : (Extra Credit: 6 points) A linked list can be sorted using quicksort using the code below. Write the function Partition so that all nodes in the list pointed to by the rst parameter are moved into two other lists (the second and third parameters). If the rst node of the rst parameter list has info eld value X, then all nodes whose info elds are less than or equal to X should be moved to the list pointed to by the second parameter and all nodes whose info elds are greater than X should be moved to the list pointed to by the third parameter. When Partition is done, the rst parameter should be an empty list. (Nodes are implemented as they are in Problem 2, the info eld is a string.) void QuickSort(Node * & list) // postcondition: list is sorted if (list!= 0) Node * first = 0; Node * second = 0; Partition(list,first,second); QuickSort(first); QuickSort(second); Node * temp = first; if (temp!= 0) while (temp->next!= 0) temp = temp->next; temp->next = second; list = first; else list = second; // partition into two lists // sort each sublist // find end of first sublist // and attach it to second // attach to second 8