Do not write in this area A.1 A.2 A.3 A.4 A.5 A.6 B.1 B.2 B.3 TOTAL. Maximum possible points:

Similar documents
Do not write in this area TOTAL. Maximum possible points: 75

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

Lab 2: ADT Design & Implementation

Review: C++ Basic Concepts. Dr. Yingwu Zhu

Chapter 10 Introduction to Classes

Lecture 18 Tao Wang 1

C++ - Lesson 2 This is a function prototype. a' is a function that takes an integer array argument and returns an integer pointer.

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

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

CSCE 110 PROGRAMMING FUNDAMENTALS

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

Instantiation of Template class

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file?

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

IDE: Integrated Development Environment

CS32 Discussion Sec.on 1B Week 2. TA: Zhou Ren

Data Structures and Algorithms

Multiple choice questions. Answer on Scantron Form. 4 points each (100 points) Which is NOT a reasonable conclusion to this sentence:

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

QUIZ Friends class Y;

Polynomial and Rational Functions

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

CMSC 341 Lecture 7 Lists

CSE 333 Midterm Exam Sample Solution 5/10/13

PRACTICE MIDTERM EXAM #2

UNIT 8: SOLVING AND GRAPHING QUADRATICS. 8-1 Factoring to Solve Quadratic Equations. Solve each equation:

Introduction & Review

Chapter 17: Linked Lists

CPSC 260 Data Structures and Algorithms for Computer Engineers Linked Lists!

CS 376b Computer Vision

Ch. 12: Operator Overloading

Outline. A C++ Linked Structure Class A C++ Linked List C++ Linked Dynamic Memory Errors In-class work. 1 Chapter 11: C++ Linked Structures

EECE.2160: ECE Application Programming

Linked List using a Sentinel

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

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

Chapter 17: Linked Lists

CSC1322 Object-Oriented Programming Concepts

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

A Previous CS 310 Final Exam

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

CSE 333 Midterm Exam 5/10/13

Summer Math Assignments for Students Entering Algebra II

2.1 Quadraticsnts.notebook. September 10, 2018

Carleton University Department of Systems and Computer Engineering SYSC Foundations of Imperative Programming - Winter Lab 8 - Structures

C Programming, Autumn 2013, Exercises for the Second Week

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

See the CS 2704 notes on C++ Class Basics for more details and examples. Data Structures & OO Development I

Review. What is const member data? By what mechanism is const enforced? How do we initialize it? How do we initialize it?

C++_ MARKS 40 MIN

C++ 8. Constructors and Destructors

Summer Math Assignments for Students Entering Integrated Math

Print out this PDF double-sided, staple pages in order, and write your answers on these pages neatly.

3x 2 + 7x + 2. A 8-6 Factor. Step 1. Step 3 Step 4. Step 2. Step 1 Step 2 Step 3 Step 4

Linked Lists. Gaddis Ch. 17. CS 2308 :: Spring 2016 Molly O'Neil

EECE.3220: Data Structures Spring 2017

! A data structure representing a list. ! A series of dynamically allocated nodes. ! A separate pointer (the head) points to the first

Example Final Questions Instructions

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

CS 216 Exam 1 Fall SOLUTION

Doubly-Linked Lists

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

(5 2) Introduction to Classes in C++ Instructor - Andrew S. O Fallon CptS 122 (February 7, 2018) Washington State University

Assignment 3: Inheritance

CS31: Introduction to Computer Science I Spring 2011

EINDHOVEN UNIVERSITY OF TECHNOLOGY Department of Mathematics and Computer Science

Dynamic Data Structures

CSCI-1200 Data Structures Fall 2009 Lecture 25 Concurrency & Asynchronous Computing

Largest Online Community of VU Students

Student Name and ID CS 32, WINTER 2015, PRACTICE MIDTERM I.

! A data structure representing a list. ! A series of nodes chained together in sequence. ! A separate pointer (the head) points to the first

Assignment of Objects

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

CSCI 104 Templates. Mark Redekopp David Kempe

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

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)

Abstract Data Types (ADT) and C++ Classes

1. Introduction. 2. Deliverables

CS 115 Exam 3, Spring 2010

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:

Algebra 1. Standard 11 Operations of Expressions. Categories Combining Expressions Multiply Expressions Multiple Operations Function Knowledge

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

Midterm 2. 7] Explain in your own words the concept of a handle class and how it s implemented in C++: What s wrong with this answer?

2. From General Form: y = ax 2 + bx + c # of x-intercepts determined by the, D =

Suppose we find the following function in a file: int Abc::xyz(int z) { return 2 * z + 1; }

Ch. 11: References & the Copy-Constructor. - continued -

Midterm Practice TA: Brian Choi Section Webpage:

CS 61B Data Structures and Programming Methodology. June David Sun

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

Lecture Notes CPSC 122 (Fall 2014) Today Quiz 7 Doubly Linked Lists (Unsorted) List ADT Assignments Program 8 and Reading 6 out S.

G205 Fundamentals of Computer Engineering. CLASS 3, Wed. Sept Stefano Basagni Fall 2004 M-W, 1:30pm-3:10pm

Homework 5. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

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

Quadratic Functions Dr. Laura J. Pyzdrowski

Why circular list? check whether a pointer current points to the last node. check for (current->link == first)

COP Programming Assignment #7

[CS302-Data Structures] Homework 2: Stacks

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

Jordan University of Science & Technology Department of Computer Science CS 211 Exam #1 (23/10/2010) -- Form A

CISC 2200 Data Structure Fall, C++ Review:3/3. 1 From last lecture:

Transcription:

Name: Student ID: Instructor: Borja Sotomayor Do not write in this area A.1 A.2 A.3 A.4 A.5 A.6 B.1 B.2 B.3 TOTAL Maximum possible points: 75 + 10 This homework has two parts: Part A (Polynomial ADT) and Part B (Doubly Linked List). You are only required to do part A if you are taking this class for credit. Page 1 of 7

Part A: Polynomial ADT In this part you will implement a Polynomial ADT (Abstract Data Type) using C++ classes. In particular, we will only consider second-order polynomials: ax 2 bx c The class declaration is the following (poly.h in the homework files): class Polynomial { private: /* Member variables */ int a, b, c; /* Static member variable */ static int numpolynomials; public: /* Constructors */ Polynomial(); Polynomial(int a, int b, int c); Polynomial(const Polynomial &p); // Copy constructor /* Destructor */ ~Polynomial(); /* Member functions */ bool hasrealsolution(); double getrealsolution1(); double getrealsolution2(); string str(); /* Overloaded operators */ Polynomial& operator=(const Polynomial &p); Polynomial operator+(const Polynomial &p); bool operator==(const Polynomial &p); int operator()(int x); /* Friends */ }; /* Static member function */ static int getnumpolynomials(); A poly.cpp file is provided that includes a partial implementation of the constructors, and the implementation of the static getnumpolynomials() function. Page 2 of 7

To test your implementation, a main.cpp is provided in the homework files. Running this program with a correct implementation should yield the following: Number of polynomials is 5 p2 and p3 are the same. Good! p2 and p5 are the same. Good! p2 and p4 are not the same. Good! p2 has real solutions x1=3, x2=-5 p4 has no real solutions. p2 is 2x^2 + 4x - 30 p2+p3 is 4x^2 + 8x - 60 p2 is 2x^2 + 4x - 30 p5 is 4x^2 + 8x - 60 p2(3) = 0 p2(-5) = 0 p2(0) = -30 Number of polynomials is 4 Exercise 1 <<5 points>> A partial implementation of these constructors is provided: Polynomial(); Polynomial(int a, int b, int c); However, these constructors do not modify the static numpolynomials member variable (which keeps a count of the number of Polynomial instances created). Modify the constructors so they will correctly change the value of numpolynomials, and implement the destructor: ~Polynomial(); Also, you must make sure that the static numpolynomials member variable is correctly initialized. Exercise 2 <<5 points>> Implement the copy constructor: Polynomial(const Polynomial &p); Page 3 of 7

Exercise 3 <<10 points>> Implement the following member functions: bool hasrealsolution(); double getrealsolution1(); double getrealsolution2(); In these functions, you will consider the polynomial as a quadratic equation (P(x)=0). hasrealsolution returns true if the equation has a real solution (or two), and false otherwise. getrealsolution1 and getrealsolution2 assume that a real solution exists, and return each of the real solutions (if the equation has a unique solution, they return the same value). If you only vaguely remember the quadratic formula, you can get up to speed here: http://en.wikipedia.org/wiki/quadratic_equation Extra credit (10 points): Modify these functions to consider all possible solutions (two complex solutions, one single real solution, two real solutions) and all possible error conditions (e.g. what if a=0? What if a=b=0? What if a=b=c=0?). You should not do this by adding more functions ( getcomplexsolution1,...) but by writing a single function that returns an error code and two complex numbers (using the ComplexNumber ADT seen in class). Exercise 4 <<10 points>> Overload the following operators: Polynomial& operator=(const Polynomial &p); Polynomial operator+(const Polynomial &p); bool operator==(const Polynomial &p); Exercise 5 <<10 points>> Overload the function call operator: int operator()(int x); Note: We have not discussed the function call operator in class. You will have to read about it on your own. Page 4 of 7

You must overload the function call operator in such a way that using the parentheses operator on a Polynomial object will return the value of that polynomial when x is equal to the integer value supplied as a parameter. For example: Polynomial p(1,2,3); cout << p(2); // Prints out 11 (Why? ---> 1*2^2 + 2*2 + 3 = 11) Exercise 6 <<5 points>> Implement the following function: string str(); This function returns a string representation of the polynomial. You must make sure that you print out the polynomial correctly. Hint: Careful with terms that have a negative coefficient and terms that have a zero coefficient. Page 5 of 7

Part B: Doubly-Linked List NOTE: You only need to do this exercise if you are taking CMSC 15200 for credit. You will implement a doubly linked list (as described in class). DoubleList Head 7 43 16 34 The structure and function declarations are the following (dlist.h in the homework files): struct ListNode { int data; ListNode *next; ListNode *prev; }; struct DList { ListNode *head; }; void createlist(dlist &l); ListNode* first(dlist &l); void inserthead(dlist &l, int data); void insertafter(listnode* node, int data); void printdata(dlist &l); bool find(dlist &l, int data); void deletehead(dlist &l); void deleteafter(listnode* node); void deletedata(dlist &l, int data); void deletelist(dlist &l); // New functions /* Inserts a new node with provided data before the specified node */ void insertbefore(dlist &l, ListNode* node, int data); /* Deletes the specified node */ void deletenode(dlist &l, ListNode* node); Page 6 of 7

Don't reinvent the wheel. Reuse as much code as possible from the list implementation seen in class! In fact, you should only tweak the existing functions to make sure that the prev pointer always has a valid value, and then implement the new insertbefore and deletenode functions. Note that, although the book describes how to implement a doubly-linked list, you cannot take code directly from the book. This exercise also evaluates your ability to read code (both from the book and the provided list implementation) and adapt it to your own needs. To test your list implementation, a main_double.cpp is provided in the homework files. Running this program with a correct doubly linked list implementation should yield the following: 5 4 3 2 1 5 4 3 2 1 5 1 4 3 2 1 5 4 3 2 1 1 0 1 4 3 2 1 5 4 3 2 1 1 3 2 1 5 4 3 2 1 1 2 1 5 4 3 2 1 42 1 2 1 5 4 3 2 1 42 37 1 2 1 5 4 3 2 1 37 1 2 1 5 4 3 2 1 37 1 2 1 4 3 2 1 37 2 4 3 2 List is empty! Note: Debugging programs that use data structures is no easy matter, and you are likely to encounter run-time errors due to dangling pointers. One good strategy is to draw the data structure on paper, and for each operation see how the next and prev pointers are affected. Don't forget to consider special cases (e.g. What happens if I try to delete the first node? ) Also, if your program crashes unexpectedly (the hallmark of a dangling pointer), the Eclipse debugger can come in handy to pinpoint the dangling pointer (the debugger will pause execution at the exact line that caused the crash). Note 2: In your code, make sure you explicitly point out (with comments) what code you had to add/modify to turn the list into a doubly linked list. The point weights for this exercise are the following: <<10 points>> for correctly updating the prev pointer, <<10 points>> for implementing insertbefore, <<10 points>> for implementing deletenode. Page 7 of 7