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

Similar documents
Linked List using a Sentinel

Data Structures (CS301) LAB

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

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

Spring 2008 Data Structures (CS301) LAB

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

Lists. linking nodes. constructors. chasing pointers. MCS 360 Lecture 11 Introduction to Data Structures Jan Verschelde, 17 September 2010.

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

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

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

Review Questions for Final Exam

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Midterm Exam 5 April 20, 2015

Example Final Questions Instructions

1) What is the output of the following program? What if we add virtual to the destructors? #include <iostream> using namespace std;

CSCE 110 PROGRAMMING FUNDAMENTALS

Data Structures and Algorithms

The following is a typical execution run of this program:

1 Short Answer (5 Points Each)

pointers & references

CA341 - Comparative Programming Languages

Practice test for midterm 1

Student Name and ID. Problem Maximal Possible Points Received Total Score 150

CSCE Practice Midterm. Data Types

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

CS 240 Data Structure Spring 2018 Exam I 03/01/2018

// Pointer to the first thing in the list

Midterm Exam. Sample Solutions

Assignment #1 (50 points; due 11:59 P.M.)

Module Operator Overloading and Type Conversion. Table of Contents

CSCE Practice Midterm. Data Types

CS 376b Computer Vision

Example Final Questions Instructions

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:

1 Short Answer (7 Points Each)

Ch. 17: Linked Lists. Introduction to Linked Lists

Ch 6. Functions. Example: function calls function

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)

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

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

Object oriented programming

C++ Programming. Classes, Constructors, Operator overloading (Continued) M1 Math Michail Lampis

(6) The specification of a name with its type in a program. (7) Some memory that holds a value of a given type.

CS 31 Discussion 1A, Week 8. Zengwen Yuan (zyuan [at] cs.ucla.edu) Humanities A65, Friday 10:00 11:50 a.m.

CMSC 330: Organization of Programming Languages. Lets, Tuples, Records

Exam 2. CSI 201: Computer Science 1 Fall 2016 Professors: Shaun Ramsey and Kyle Wilson. Question Points Score Total: 80

Introduction to Programming I COS1511 School of Computing Revision Notes

CS24 Week 3 Lecture 1

CpSc212 Goddard Notes Chapter 10. Linked Lists

CS 103: Introduction to Programming Fall Written Final Exam 12/11/16, 4:30PM 6:30PM

Introduction to the C programming language

Introduction to the C programming language

Review Questions for Final Exam KEY

Lab 2: Pointers. //declare a pointer variable ptr1 pointing to x. //change the value of x to 10 through ptr1

Module Contact: Dr Pierre Chardaire, CMP Copyright of the University of East Anglia Version 1

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

CS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015

COL 100. Minor 2 Exam - Practice

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

Multiple Choice Questions (20 questions * 5 points per question = 100 points)

CS31 Discussion. Jie(Jay) Wang Week8 Nov.18

CMSC 202 Midterm Exam 1 Fall 2015

CS 117 Programming II, Spring 2018 Dr. Ghriga. Midterm Exam Estimated Time: 2 hours. March 21, DUE DATE: March 28, 2018 at 12:00 PM

1. a) What #include statement do you put at the top of a program that does uses cin, cout or endl?

CS 115 Midterm 2A Solutions

Assignment 4: SkipList

Midterm Exam #2 Spring (1:00-3:00pm, Friday, March 15)

Due Date: See Blackboard

Inheritance, and Polymorphism.

CS31: Introduction to Computer Science I Spring 2011

Pointers: 2 Operators:

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list

ENJOY! Problem 2 What does the following code do, in a brief English sentence? int mystery(int k) { int i = 0; if (k < 0) return -1;

Unit 7. 'while' Loops

BITG 1113: Array (Part 1) LECTURE 8

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list

Local and Global Variables

! A data type for which: ! An ADT may be implemented using various. ! Examples:

CSCI 111 Midterm 2 Exam Spring Solutions 09.00am 09.50am, Wednesday, May 04, 2016

Sol. Sol. a. void remove_items_less_than(int arr[], int size, int value) #include <iostream> #include <ctime> using namespace std;

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

BITG 1233: Array (Part 1) LECTURE 8 (Sem 2, 17/18)

moretosearch = (location < length);

Other operators. Some times a simple comparison is not enough to determine if our criteria has been met.

Linked Lists 28/11/2018. Nodes with member functions. The need for a linked list class

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

PIC 10A. Final Review: Part I

CSCI 104 Memory Allocation. Mark Redekopp David Kempe

2. It is possible for a structure variable to be a member of another structure variable.

CS 115 Midterm 2 Solutions

C++ and OO. l C++ classes and OO. l More Examples. l HW2. C++ for C Programmers by Ira Pohl

LECTURE 03 LINKED LIST

Introduction Of Classes ( OOPS )

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

Use the template below and fill in the areas in Red to complete it.

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

Do not turn to the next page until the start of the exam.

Intermediate Programming, Spring 2017*

The University of Nottingham

1 Unit 8 'for' Loops

Transcription:

UCLA Computer Science Department TA: Kung-Hua Chang Student Name and ID CS 32, WINTER 2015, PRACTICE MIDTERM I. Problem # Maximal Possible Points Received 1.1 3 1.2 5 1.3 5 1.4 5 1.5 5 2 3 3.1 4 3.2 5 Total 35

Problem #1: Below is a definition of a LinkedList class and a test main program: #include <iostream> using namespace std; class LinkedList public: LinkedList(): head(nullptr) ~LinkedList(); void addtolist(int value); // add to the end of the linked list void reverse(); // Reverse the linked list void output(); private: struct Node int num; Node *next; ; Node *head; ; void LinkedList::output() Node *ptr = head; cout << "The elements in the list are: "; while(ptr!=nullptr) cout << ptr->num << ""; cout << endl; int main() LinkedList list; for(int i=1;i<=10;i++) list.addtolist(i); list.output(); list.reverse(); list.output();

Problem #1.1: Please complete the implementation of the destructor. LinkedList::~LinkedList() Node *temp; while(head!= nullptr) temp = head; head = head->next; delete temp; Problem #1.2: Please write an implementation of the addtolist member function. void LinkedList::addToList(int value) Node *nodetoadd = new Node; nodetoadd->num = value; nodetoadd->next = nullptr; if( head == nullptr) head = nodetoadd; else Node *ptr = head; while(ptr->next!= nullptr) ptr->next = nodetoadd;

Problem #1.3: Please complete the implementation of the reverse member function. void LinkedList::reverse() Node *nextnode = nullptr,*prevnode = nullptr, *current = head; while(current) // Hint: Only 4 lines of codes are needed inside the while loop nextnode = current->next; current->next = prevnode; prevnode = current; current = nextnode; head = prevnode; Problem #1.4: Suppose we add a new member function called findnthfromlast() to find the N-th node from the end of the list, where N being 1 means the last node, N being 2 the second-to-last, etc. Use the reverse member function to complete the implementation of findnthfromlast() member function. If the list has at least N nodes, then assign to the variable value the number that is stored in that node and return true; otherwise, leave the variable value unchanged and return false. Don't forget to call the reverse function to restore the linked list to its original state. bool LinkedList::findNthFromLast(int N, int &value) reverse(); Node *ptr = head; int n = 0; while(ptr!= nullptr) n++; if(n == N) value = ptr->num; reverse(); return true; reverse(); return false;

Problem #1.5: There is a more efficient way to solve problem #1.4 without using reverse() function. The idea is to scan first to count the total number of nodes in the linked list. Once we know how many nodes are in the linked list, the second time we scan the list, the Nth node from the end of list can be easily calculated. Please use this idea to implement this member function findnthfromlast(). bool LinkedList::findNthFromLast(int N, int &value) if( N <= 0 ) return false; Node *ptr = head; int i, M = 0; // M is the number of nodes in the linked list. while(ptr!= nullptr) M++; if( N > M ) return false; for( i=1, ptr = head; i < (M-N+1); i++) value = ptr->num; return true; // Your implementation should pass the following test program int main() int value = 999; LinkedList list; for(int i=1;i<=10;i++) list.addtolist(i); assert(! list.findnthfromlast(11,value) && value == 999); assert( list.findnthfromlast(1,value) && value == 10); assert( list.findnthfromlast(2,value) && value == 9); assert( list.findnthfromlast(10,value) && value == 1);

Problem #2: Please complete the missing blocks of codes below to make the program generate the following output: 1+2i 6+7i 6+7i 6+7i #include <iostream> using namespace std; class Complex private: double r,i; public: // Please complete the missing codes below. Complex(): r(0),i(0) Complex(int c_r,int c_i): r(c_r),i(c_i) ; void output() cout << r << "+" << i << "i" << endl; int main() Complex a,b; a = Complex(1,2); a.output(); b = Complex(6,7); b.output(); a = b; a.output(); b.output(); return 0;

Problem #3: The implementation below will CRASH because it s missing a copy constructor and an assignment operator. #include <iostream> using namespace std; class Triangle public: Triangle() p = new Point[3]; Triangle(int x1,int y1,int x2, int y2,int x3,int y3) p = new Point[3]; p[0].x = x1; p[0].y = y1; p[1].x = x2; p[1].y = y2; p[2].x = x3; p[2].y = y3; Triangle::~Triangle() delete [] p; private: struct Point int x,y; Point(int px=0,int py=0): x(px), y(py) ; Point *p; ; int main() Triangle *array[3]; array[0] = new Triangle(1,1,1,3,3,1); array[1] = new Triangle(2,2,2,6,6,2); array[2] = new Triangle(3,3,3,9,9,3); Triangle c2 = *array[0]; c2 = *array[1]; for(int i=0;i<3;i++) delete array[i];

Problem #3.1: Please write an implementation of the copy constructor. Triangle::Triangle(Triangle &t) p = new Point[3]; for(int i=0;i<3;i++) p[i] = t.p[i]; Problem #3.2: Please write an implementation of the assignment operator. Triangle& Triangle::operator=(const Triangle &t) if(&t == this) return *this; delete [] p; p = new Point[3]; for(int i=0;i<3;i++) p[i] = t.p[i]; return *this;