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

"apple" "grape" "grape" "grape" "apple"

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

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

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

cameron grace derek cameron

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

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.

mypoly

EECE.3220: Data Structures Spring 2017

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

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

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

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

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

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

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)

Data Structure (CS301)

Object Oriented Programming COP3330 / CGS5409

Programming Assignment 2

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

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

CS 216 Exam 1 Fall SOLUTION

CS24 Week 4 Lecture 2

Largest Online Community of VU Students

CS171 Midterm Exam. October 29, Name:

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

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

CSC1322 Object-Oriented Programming Concepts

Classes and Objects. Class scope: - private members are only accessible by the class methods.

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

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

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

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

Fast Introduction to Object Oriented Programming and C++

Dynamic Data Structures

Module 9. Templates & STL

Chapter 11. Abstract Data Types and Encapsulation Concepts

The Compositional C++ Language. Denition. Abstract. This document gives a concise denition of the syntax and semantics

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

Chapter 3: Data Abstraction

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

Largest Online Community of VU Students

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

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

ADT: Design & Implementation

ADT Sorted List Operations

Lab 2: ADT Design & Implementation

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

Data Structures and Algorithms

Containers and the Standard Template Library (STL)

Lecture 7. Log into Linux New documents posted to course webpage

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Overview of today s lecture. Quick recap of previous C lectures. Introduction to C programming, lecture 2. Abstract data type - Stack example

CSE030 Fall 2012 Final Exam Friday, December 14, PM

Computer Science Foundation Exam

CS201 Some Important Definitions

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

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

Object Oriented Paradigm

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

(8 1) Container Classes & Class Templates D & D Chapter 18. Instructor - Andrew S. O Fallon CptS 122 (October 8, 2018) Washington State University

PROGRAMMING IN C++ (Regulation 2008) Answer ALL questions PART A (10 2 = 20 Marks) PART B (5 16 = 80 Marks) function? (8)

CMPT 117: Tutorial 1. Craig Thompson. 12 January 2009

Purpose of Review. Review some basic C++ Familiarize us with Weiss s style Introduce specific constructs useful for implementing data structures

Most of this PDF is editable. You can either type your answers in the red boxes/lines, or you can write them neatly by hand.

Today s lecture. CS 314 fall 01 C++ 1, page 1

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

Programming Languages 2nd edition Tucker and Noonan"

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

Preface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS

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

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 12: Classes and Data Abstraction

Outline. Computer Science 331. Information Hiding. What This Lecture is About. Data Structures, Abstract Data Types, and Their Implementations

CMa simple C Abstract Machine

Suppose that the following is from a correct C++ program:

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

Topic 7: Algebraic Data Types

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

Chapter 13 Object Oriented Programming. Copyright 2006 The McGraw-Hill Companies, Inc.

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

CSCE 110 PROGRAMMING FUNDAMENTALS

1 Short Answer (10 Points Each)

Implementing Abstractions

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

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

15-122: Principles of Imperative Computation, Spring Due: Thursday, March 10, 2016 by 22:00

ADT Unsorted List. Outline

C++: Const Function Overloading Constructors and Destructors Enumerations Assertions

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

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

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

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

CS 10, Fall 2015, Professor Prasad Jayanti

CSE 143 SAMPLE MIDTERM

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

UNIVERSITY REGULATIONS

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am

Computer Science Foundation Exam

Transcription:

Test 1: CPS 100 Owen Astrachan October 5, 1994 Name: Honor code acknowledgement (signature) Problem 1 Problem 2 Problem 3 Problem 4 Problem 5 TOTAL: value 8 pts. 6 pts. 14 pts. 10 pts. 14 pts. 52 pts. grade 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. template (a) A program used as a pattern on which similar programs are designed and implemented. (b) A mechanism in C++ for parameterizing a class by atype, e.g., allowing a general stack class to be able to represent a stack ofint and a stack of double. (c) A mechanism that permits operators to be overloaded in a user-dened class, e.g., so that += can mean something for a class like BigInt. 2. destructor (a) A member function that is automatically invoked when a variable of a user-dened type is initially dened. (b) A member function that is automatically invoked when a variable of a user-dened type goes out of scope. (c) A bug in a program that slowly stops the operating system from working. 3. O-notation (a) A notation used to express relationships between dierent objects in an object-oriented program. (b) A notation used to express resource requirements of an algorithm that provides an asymptotic upper bound on the resource. (c) A notation used by the professor in this course to describe his own programs. 4. Queue (a) A last-in, rst-out data structure. (b) A rst-in, rst-out data structure. (c) A letter in the alphabet. PROBLEM 2 : (Postx: 6 points) What is the value of the postx expression: 8 2 * 4 5 + - 11 * Write a postx expression equivalent to 7 + (12 + 3) * 4. Note that this expression has the value 67. How many comparisons are needed to nd an element in an array of four million elements using binary search? [answers within 10 will be considered correct]. 2

PROBLEM 3 : (Stacks: 12 points) Part of the header le for the stack class we discussed in class is shown below (this is for a stack ofint.) const int maxsize = 400; typedef int ElementType; class Stack public: Stack (); // constructor Stack (const Stack &); // copy constructor ~Stack(); // destructor int IsEmpty () const; // returns 1 if empty, else 0 ElementType Top () const; // returns top element int Push(ElementType); // push, return 1 if success int Pop(); // pop, return 1 if success Stack & operator = (const Stack &); private: int tos; // top of stack ElementType * elements; // array implementation ; Part A 8points You are to write the body of the NON-member function StackSize below. StackSize returns the number of elements stored in the parameter s. Note that s is passed as a const-reference parameter which means that the member function Pop CANNOT be used with s (since this would alter s). You can, however, dene variables/objects of type Stack. int StackSize(const Stack & s) // postcondition: returns # of elements stored in stack s 3

Part B 3points Write the body of operator < below. In writing the function you may call the function StackSize from part A. Assume that StackSize works as specied, regardless of what you wrote for part A. Less-than is dened for stacks a and b so that a<bif the number of elements in stack a is smaller than the number of elements in stack b. int operator < (const Stack & a, const Stack & b) // postcondition: returns 1 if stack a is "smaller" than stack b. Part C 3points In the denition of the class Stack given above an array is used to store the elements of the stack. The array is accessible via the private data eld elements. The body of the constructor Stack is shown below. Stack::Stack() // postcondition: empty stack initialized elements = new ElementType [maxsize]; tos = 0; Explain how this constructor could be changed so that a statement like Stack s(100); could be used to dene a stack of 100 elements, but the denition Stack st; would still be legal and dene a stack of 400 elements. 4

Bonus question (2 points): Who invented the language C++? 5

PROBLEM 4 : (Double-link 10 points) In a doubly-linked list, each node maintains pointers to the previous node and to the next node. For example, the diagram below shows a doubly-linked list of 4 nodes. 5 7 11 16 Doubly-linked lists are implemented using the struct below. struct Node int info; Node * next; Node * previous; Node(int datum, Node * before = 0, Node * after = 0); ; Node::Node(int datum, Node * before, Node * after) // postcondition: construct list node from given params info = datum; previous = before; next = after; Part A Assume that doubly-linked lists are maintained in sorted order and do NOT have header nodes. Write the function Insert below that inserts a new node containing val into list so that list remains in sorted order. void Insert(Node * & list, int val) // precondition: list is sorted, list = a1, a2,..., an // postcondition: list is sorted, list contains val and all nodes a1,...,an // 6

7

PROBLEM 5 : (Classied 14 points) A class that implements lists of strings uses the denition below. The denitions below support adding elements only to the front of a List. The underlying representation of a List uses a linked list with a header node. typedef String Key; struct Node Key info; Node * next; Node(const Key & key, Node * follow = 0) info = key; next = follow; ; class List public: List(); List(const List & list); ~List(); int Size() const; void AddFront(const Key & key); int Includes(const Key & key); private: Node * first; ; // constructor, makes empty list // copy constructor // destructor // returns # of elements in list // add key to front of list // returns 1 if key in list // points to first node in list The use of a header node means that a list ("apple","pear", "orange","banana") is represented by an object diagrammed below. A constructor initializes an empty list consisting of just a header node whose next eld has value 0. first apple pear orange banana List::List() // postcondition: list with 0 elements created first = new Node("",0); // make header node 8

Part A: 6points Assume that the operator += will be overloaded so that it adds a new node to the end of a List. That is the eect of animals += "zebra" is to add a new item to the end of the List animal. The prototype for this operator is given below, you are to write the code implementing the operator. List & List::operator += (const Key & key) // precondition: *this represents a1, a2,..., an // postcondition: *this represents a1, a2,..., an, key 9

Part B: 8points Assume that the function Remove whose header and specication are given below is provided. You do NOT need to write this function, you can call the function in code you write. void Remove(Node * & list, const String & word) // postcondition: all nodes of list containing word have been removed // other nodes remain in original order A new member function Uniquify is to be implemented that removes all duplicate keys from a list. For example, if a list fruit is represented by ("apple","pear", "apple", "orange", "apple", "pear", "banana") then after the call fruit.uniquify(), fruit should represent the list ("apple","pear", "orange", "banana") Write member function Uniquify and describe, using O-notation, the complexity of the code you write to remove all duplicates from a list that initially contains n elements. 10