mypoly

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"

cameron grace derek cameron

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)

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

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

CIS Fall Data Structures Midterm exam 10/16/2012

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.

CS 216 Exam 1 Fall SOLUTION

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

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

Largest Online Community of VU Students

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

Midterm Exam 2 CS 455, Spring 2011

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

CMSC 341 Lecture 10 Binary Search Trees

Questions: ECE551 PRACTICE Final

Computer Science Foundation Exam

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

CS212 - COMPUTATIONAL STRUCTURES AND ALGORITHMS

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

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

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

CSE 143 SAMPLE MIDTERM

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

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

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

(b) int count = 0; int i = 1; while (i<m) { for (int j=i; j<n; j++) { count = count + 1; i = i + 1; O(M + N 2 ) (c) int count = 0; int i,j,k; for (i=1

Largest Online Community of VU Students

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

CS 455 Final Exam Fall 2012 [Bono] Dec. 17, 2012

LAB 5, THE HIDDEN DELIGHTS OF LINKED LISTS

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

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

list<t>::const_iterator it1 = lst.begin(); // points to first element list<t>::const_iterator it2 = lst.begin(); // points to second element it2++;

CSC1322 Object-Oriented Programming Concepts

Computer Science Foundation Exam

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

Data Structure (CS301)

Common Misunderstandings from Exam 1 Material

CS 455 Final Exam Fall 2015 [Bono] Dec. 15, 2015

Dynamic Data Structures

Review for Test 1 (Chapter 1-5)

Data Structures And Algorithms

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

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

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

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

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

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

Largest Online Community of VU Students

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

class Polynomial { public: Polynomial(const string& N = "no name", const vector<int>& C = vector<int>());... };

Object-Oriented Principles and Practice / C++

CSIS 10B Lab 2 Bags and Stacks

C++ Templates. David Camp

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

ADT Unsorted List. Outline

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

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

CS 61B Summer 2005 (Porter) Midterm 2 July 21, SOLUTIONS. Do not open until told to begin

Extra Credit: write mystrlen1 as a single function without the second parameter int mystrlen2(char* str)

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

! Determine if a number is odd or even. ! Determine if a number/character is in a range. ! Assign a category based on ranges (wind speed)

Object Oriented Programming COP3330 / CGS5409

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

1 Recursion. 2 Recursive Algorithms. 2.1 Example: The Dictionary Search Problem. CSci 235 Software Design and Analysis II Introduction to Recursion

Priority Queues. 1 Introduction. 2 Naïve Implementations. CSci 335 Software Design and Analysis III Chapter 6 Priority Queues. Prof.

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

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

Exam B Informatik D-MATH/D-PHYS :15 14:55

Practice Midterm Exam #1

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

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

Exam A Informatik D-MATH/D-PHYS :15 14:55

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

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

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

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

Chapter 12 - Templates

15-122: Principles of Imperative Computation, Fall 2015

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

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

Name CPTR246 Spring '17 (100 total points) Exam 3

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

SOLUTIONS (INCOMPLETE, UNCHECKED)

2003 AP COMPUTER SCIENCE AB FREE-RESPONSE QUESTIONS

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

Computer Science Foundation Exam

EECE.3220: Data Structures Spring 2017

PRACTICE MIDTERM EXAM #2

ADT Sorted List Operations

Linked List Practice Questions

Midterm Exam (REGULAR SECTION)

CMSC 341 Lecture 6 Templates, Stacks & Queues. Based on slides by Shawn Lupoli & Katherine Gibson at UMBC

CSCE 2014 Final Exam Spring Version A

Ch. 18: ADTs: Stacks and Queues. Abstract Data Type

Prelim 1 SOLUTION. CS 2110, September 29, 2016, 7:30 PM Total Question Name Loop invariants. Recursion OO Short answer

CS250 Final Review Questions

! Operators such as =, +, <, can be defined to. ! The function names are operator followed by the. ! Otherwise they are like normal member functions:

Transcription:

CPS 100 Exam 1 Fall 1996 Dr. Rodger PROBLEM 1 : (Hidden in the Stack: (12 pts)) The Stack class discussed in lecture (StackAr.h) is given on the Exam 1 handout. PART A (6 pts): Write the function Max which returns the maximum value in a stack of integers. Max assumes that the stack is not empty and it does not change the stack inanyway. The function Max is NOT a part of the Stack class, but rather part of a user's program. Complete Max below the following header. int Max(const Stack<int> & numbers) // precondition: numbers is not empty // postcondition: returns the maxmimum value on the stack numbers PART B (6 pts): Write the member function Max which returns the maximum value in a stack. Max assumes that the stack is not empty and it does not change the stack in any way. This time, the function Max is a part of the Stack class. Assume assignment and comparison operators are dened for whatever type the Stack is used with. For example, if a stack named scores of type int is declared, then scores.max() returns the maximum score on the stack. If a stack named names of type string is declared, then names.max() returns the maximum name (the last name in alphabetical order). Complete Max below the following header. template <class Etype> Etype Stack<Etype>::Max() // precondition: stack is not empty // postcondition: returns maximum value on the stack PROBLEM 2 : (Analysis: (18 pts)) 1. (2 pts) What is the worst case running time (big-oh, O(?)) of the following code segment? int k, j, h; int sum = 0; for (k = 1; k <= N; k++) for (j = 1; j <= P; j++) for (h = 1; h <= N; h++) sum = sum + h; 1

2. (2 pts) What is the worst case running time (big-oh, O(?)) of the following code segment? int k, j; int sum = 0; for (k = 1; k < N; k++) for (j = k; j < N; j++) sum = sum + j; 3. (10 pts) Assume you are given a pointer to a singly linked list of elements in sorted order from smallest to largest. The pointer to the linked list points to the smallest element. The list may contain duplicate items. For example, the list below contains 8 elements. list 3 3 3 6 9 11 11 15 Consider the most ecient implementation of each of the following operations, given only a pointer to the front of the list. For example, nd the maximum would be implemented as a function with the only parameter a pointer to the linked list. DO NOT modify the data structure. What is the worst case running time (big-oh, O(?)) for each operation if the list contains N elements? (a) Find the maximum element Running time: (b) Find the minimum element Running time: (c) Is X an element in the list Running time: (d) Print the element that occurs most often Running time: (e) List the elements from largest to smallest Running time: 4. (4 pts) Consider the following silly Mystery function but don't attempt to decipher what it computes. int Mystery (int N) // precondition: N>=0 int tot = 0; int j, h; for (j=1; j<=n; j++) tot += Mystery(N-1); h = N; while (h>0) tot += j; h = h/2; return tot; 2

Write a recurrence relation describing the running time of Mystery. DO NOT solve the recurrence or compute the big-oh, just give the recurrence relation. PROBLEM 3 : (Get a Job: (18 pts)) This problem refers to a linked list of companies, for which each company contains the names of students they plan to interview. Refer to the Exam 1 Handout for an example gure and the denition of CNode and SNode (note both nodes have constructors). Part A (5 pts) Write the function IsMember whose header is given below. IsMember returns true if the student name is a member of a linked list of students. For example, on the Exam 1 handout, where interview is the pointer to the data structure, IsMember(interview->rst, "Hendren") returns true since Hendren is in the IBM list of students, IsMember(interview->next->rst, "") returns true since is in the GE list of students, and IsMember(interview->rst, "Farlow") returns false, since Farlow is not in the IBM list of students. Complete IsMember below the following header. bool IsMember(SNode * list, string name) // postcondition: returns true if name is a member of the list, // otherwise returns false Part B (5 pts) Write the function PrintCompanies whose header is given below. PrintCompanies prints the name of companies (on one line) the particular student is interviewing with. Refering to the data structure on the Exam 1 handout where interview is the pointer to the data structure, PrintCompanies(interview, "Hendren") prints IBM Glaxo PrintCompanies(interview, "") prints IBM Microsoft SAS You may call the function IsMember that you wrote in PART A. Assume IsMember works correctly regardless of what you wrote for PART A. Complete PrintCompanies below the following header. void PrintCompanies (CNode * list, string student) // postcondition: Prints a list of companies on one line that // student is interviewing with. 3

Part C (8 pts) Write the function CopyList whose header is given below. CopyList is given a list of companies and the name of two companies, company1 and company2, and makes a copy of company1's list with the new company name company2. Company2's list is inserted into the list of companies to the immediate right of company 1.The students in company2's list do not have to be in the same order as they were in company1's list. Refering to the rst data structure on the Exam 1 handout, CopyList(interview, "GE", "Intel") might modify this structure to look like the second data structure. Note that the Intel list is a copy of the GE list; however, the students do not have to appear in the same order. Complete CopyList below the following header. void CopyList(Cnode * list, string company1, string company2) // precondition: company1 is a company name in list // postcondition: makes a copy of company1's student list and inserts the // list to the immediate right of company1 in list with the new // company name company2. The students in company2's list do not // have to be in the same order as they are in company1's list. PROBLEM 4 : (Poly want another: (30 pts)) This problem refers to the linked list implementation of the Polynomial class. Refer to the Exam 1 Handout for the denitions of Node and the Polynomial class. NOTE that the implementation contains two private data items mypoly and mynumnodes. You must use this implementation (and these variables) in the questions that follow. You may assume that Nodes in the linked list are in sorted order from largest degree to smallest degree, and mypoly points to the Node of highest degree. You may also assume (except for Part F) that there is only one Node per degree and no nodes for terms with a zero coecient, unless the Polynomial is equal to 0, in which case there is one Node with coecient 0. Part A (5 pts) Write the member function LowestDegree whose header is given below. LowestDegree returns the smallest degree of the terms in the polynomial. If Polynomial A was 3:2x 8 +5:2x 7 +5:4x 3, then A would be represented by the linked list shown below and A.LowestDegree() would return 3 since x 3 is the lowest degree term. If Polynomial B was 3:7x 5 +3:2x 2 +1:4x x +6:8, then B would be represented by a linked list of 4 nodes and B.LowestDegree() would return 0 since 6:8 is the lowest degree term (which is the same thing as 6:8x 0 ). mypoly 3.2 8 5.2 7 5.4 3 Complete LowestDegree below the following header. int Polynomial::LowestDegree() 4

// postcondition: returns the degree of the smallest term Part B (5 pts) Write the member function Swap whose header is given below. Swap exchanges the values of two polynomials. For example, if Polynomial A was 3:2x 8 +5:2x 7 +5:4x 3, and Polynomial B was 3:7x 5 +3:2x 2 + 1:4x x +6:8, then A.Swap(B) would change Polynomial A to be 3:7x 5 +3:2x 2 +1:4x x +6:8 (B's value) and Polynomial B to be 3:2x 8 +5:2x 7 +5:4x 3 (A's value). Note that B.Swap(A) would accomplish the same thing. Complete Swap below the following header. void Polynomial::Swap(Polynomial & Poly) // postcondition: Swap the values of two Polynomials Part C (4 pts) Suppose that you did not write a copy constructor for the Polynomial class. would happen if you executed the statements shown below. Explain what Polynomial A(3.2, 4.8, 1.2); Polynomial B(A); Part D (4 pts) Consider the following code segment. void Process(Polynomial & P); void DoIt(Polynomial Q); main () Polynomial Poly(3.2, 4.5, 2.1); Polynomial B(Poly); Polynomial C = B; Polynomial D; D = Poly; Process(C); DoIt(D); 5

Assume that a copy constructor and an \=" operator are written for the Polynomial class. For each line in the main function above, indicate to the right whether a constructor, copy constructor, or none is invoked. Part E (4 pts) Suppose that you did not write a destructor for the Polynomial class. Explain 1) when a destructor is invoked, and 2) what would happen in the Polynomial class. Part F (8 pts) Write the member function Compress whose header is given below. For this problem only, assume that the implementor has been lazy in some of the member functions. Terms are sorted by degree, however there may beseveral terms with the same degree. Compress compresses Nodes with the same degree into one Node. For example, suppose Polynomial A was 3:2x 8 +5:2x 7 +5:4x 3. It might be represented by the rst polynomial shown below (there are three nodes for the x 8 term whose coe sum is 3.2). Compress() would rst compress nodes of like degree resulting in the second polynomial shown below. The last statement in Compress() should call RemoveZeros() to remove the Nodes with coe zero from the list. You may assume the function RemoveZeros() already exists (you do not have to write it) and that it removes Nodes with coecient 0 unless there is just one node in the list. mypoly 1.1 8 0.8 8 1.3 8 5.2 7 2.1 3 3.3 3 1.8 2-1.8 2 mypoly 3.2 8 5.2 7 5.4 3 0.0 2 Complete Compress below the following header. void Polynomial::Compress() // postcondition: Compresses Nodes with the same degree CPS 100 Exam 1 Handout Fall 1996 Dr. Rodger PROBLEM 1: StackAr.h template <class Etype> class Stack public: Stack( ); // construct empty stack ~Stack( ); // destructor const Stack & operator=( const Stack & Rhs ); // assign Rhs to self 6

// copy constructor works ok void Print() const; void Push( const Etype & X ); void Pop( ); const Etype & Top( ) const; bool IsEmpty( ) const; bool IsFull( ) const; void MakeEmpty( ); // print stack to cout // push X onto top of stack // pop top element // return top element (NO pop) // return true if empty, else false // return true if full, else false // empty stack (no elements) int NumPushes(); int Size(); // returns # of elements pushed // returns # of elements in stack ; private: int mymaxsize; int mytop; int mypushes; Vector<Etype> myarray; // largest possible size of stack // index of top item // number of items pushed (may not be // current size of stack) 7

PROBLEM 3: SNode, CNode, and interview list struct SNode string name; SNode * next; ; SNode (string nm, SNode * nx = NULL) name = nm; next = nx struct CNode string name; CNode * next; SNode * first; ; CNode (string nm, CNode * nx = NULL, SNode * fr = NULL) name = nm; next = nx; first = fr; Parts A and B interview IBM GE Microsoft Glaxo SAS Starling Manchen Preston Yeung Hendren Hendren Howard Bartoli Geer Farlow Huang Starling Part C After calling CopyList(interview, "GE", "Intel") on the above structure. interview IBM GE Intel Microsoft Glaxo SAS Starling Manchen Preston Yeung Hendren Hendren Manchen Howard Bartoli Geer Farlow Huang Starling 8

PROBLEM 4 struct Node double coeff; int degree; Node * next; ; // member functions // // Polynomial(double, double, double); // Create a polynomial of degree 3 or less, has default values // if fewer than 3 arguments are sent. // ~Polynomial(); // destructor // int Degree(); // return the coefficient of the highest term with nonzero degree class Polynomial public: Polynomial(double, double, double); ~Polynomial(); int Degree(); // not all Member functions shown // constructor with defaults // destructor // return degree of polynomial private: Node * mypoly; int mynumnodes; ; // pointer to linked list // number of nodes in polynomial 9