29
Hong Kong University of Science and Technology COMP104: Programming Fundamentals and Methodology Fall 2004, Lecture Section 1, 2, 3 Final Examination Friday, Dec.17, 2004 08:30 – 11:30AM Student Name: key Lecture Section: Student ID: Lab Section/TA Name: Instructions: 1. This is a closed-book, closed-notes examination, calculator is also not allowed. 2. Check that you have all 21 pages (including this cover page). 3. Write your name , student ID , lecture section , lab section and TA name on this page. 4. Please circle your answer . 5. Answer all questions in the space provided. Rough work should be done on the back pages. Questi on Topic Score 1 Strings /10 2 File I/O /5 3 Pointers /9 4 S imple L inked L ist /10 5 A lgorithms on L inked L ist /14 6 D oubly L inked L ist /18 7 Classes /10 8 Dynamic C lasses/ ADT /24 Total /100 /10 1

Hong Kong University of Science and Technologyhorner/comp104/exams/final04.doc  · Web view, 1 for each digit): 42 c) Complete the following program to print the user’s input word

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Hong Kong University of Science and Technologyhorner/comp104/exams/final04.doc  · Web view, 1 for each digit): 42 c) Complete the following program to print the user’s input word

Hong Kong University of Science and TechnologyCOMP104: Programming Fundamentals and Methodology

Fall 2004, Lecture Section 1, 2, 3Final Examination

Friday, Dec.17, 2004 08:30 – 11:30AM

Student Name: key Lecture Section:

Student ID: Lab Section/TA Name:

Instructions:

1. This is a closed-book, closed-notes examination, calculator is also not allowed.2. Check that you have all 21 pages (including this cover page).3. Write your name, student ID, lecture section, lab section and TA name on this page.4. Please circle your answer.5. Answer all questions in the space provided. Rough work should be done on the back

pages.

Question Topic Score1 Strings /102 File I/O /53 Pointers /94 S imple L inked L ist /105 A lgorithms on L inked L ist /146 D oubly L inked L ist /187 Classes /108 Dynamic C lasses/ ADT /24

Total /100 /100

1

Page 2: Hong Kong University of Science and Technologyhorner/comp104/exams/final04.doc  · Web view, 1 for each digit): 42 c) Complete the following program to print the user’s input word

Question 1 : Strings ( 10 marks)

a) What is the output of the following program? (1 mark)

#include <iostream>#include <string>using namespace std;

void main(){ string x = "2"; string y = "3"; string z = "5";

if(x+y == z){ cout << "2 + 3 = 5" << endl; } else{ cout << "2 + 3 != 5" << endl; }}

Answer (1 mark): 2 + 3 != 5

b) What is the output of the following program? (2 marks)

#include <iostream>#include <string>using namespace std;

void main(){ string x = "4";

string y = "2";string sum = x + y;cout << sum << endl;

}

Answer (2 marks, 1 for each digit): 42

2

Page 3: Hong Kong University of Science and Technologyhorner/comp104/exams/final04.doc  · Web view, 1 for each digit): 42 c) Complete the following program to print the user’s input word

c) Complete the following program to print the user’s input word in reverse order and check if it is a palindrome. A word is a palindrome if its reversion is the same as itself. For example, the output of the program should look like this:

Enter a word to reverse: canoeYour word spelled backwards is: eonacIt is not a palindrome.

Or

Enter a word to reverse: kayakYour word spelled backwards is: kayakIt is a palindrome.

Also, it should be case sensitive, i.e., “kayak” is a palindrome, but “Kayak” is not. You can use predefined C++ string functions such as xxx.length() (this function returns the number of characters of the given string xxx). (7 marks)

void main(){ string inword, outword; bool isPalindrome;

// Get user input cout << "Enter a word to reverse: "; cin >> inword;

// copy user input into outword, this will make sure // outword is long enough to contain inword outword = inword; // START YOUR CODE HERE ----->

// <---- END YOUR CODE HERE // Output the reversed word and whether it's a palindrome cout << "Your word spelled backward is: " << outword << endl; if(isPalindrome) cout << "It is a palindrome" << endl; else cout << "It is not a palindrome" << endl;

}Answer (7 marks):

3

Page 4: Hong Kong University of Science and Technologyhorner/comp104/exams/final04.doc  · Web view, 1 for each digit): 42 c) Complete the following program to print the user’s input word

for(int i=0; i<inword.length(); i++) // 2 marks outword[inword.length() - i - 1] = inword[i]; // 3 marksif(outword == inword) // 2 mark isPalindrome = true;else isPalindrome = false;

Question 2: File I/O ( 5 marks)

Suppose file “test.dat” contains 3 integers separated by spaces. For example:

23 35 60

Write C++ code to open the file, read in 3 integers from the file, and display them on the screen in reverse order separated by spaces. For the above file, the output of your program should be:

60 35 23

#include <iostream>#include <fstream>using namespace std;

void main(void){ // START YOUR CODE HERE ----->

// <---- END YOUR CODE HERE}

Answer (5 marks):

int n1, n2, n3;ifstream fin; // 1 mark

4

Page 5: Hong Kong University of Science and Technologyhorner/comp104/exams/final04.doc  · Web view, 1 for each digit): 42 c) Complete the following program to print the user’s input word

fin.open(“test.dat”); // 1markfin >> n1; // 1 markfin >> n2 ;fin>> n3 ;

cout << n3 << “ ” << n2 << “ ” << n1 << endl ; // 1 mark

fin.close() ; // 1 markQ uestion 3: Pointers ( 9 marks)

a) Given the following lines of codes,

int ival = 1024;int ival2 = 2048;int* pi1 = &ival;int* pi2 = &ival2;int** pi3 = 0;

Are the following statements legal or illegal? (3 marks)

(1) pi2 = *pi1;

(2) ival = pi1;

(3) pi3 = &pi2;

Answer(3 marks, 1 for each):

(1) illegal(2) illegal(3) legal

5

Page 6: Hong Kong University of Science and Technologyhorner/comp104/exams/final04.doc  · Web view, 1 for each digit): 42 c) Complete the following program to print the user’s input word

b) What is the output of the following program? (6 marks)

#include <iostream>using namespace std;

int x,y;

void Fun1(int* a,int* b,int* c){*a=*b+x;*b=*c+y;

}

void Fun2(int a,int b,int c){a=b+x;b=c+y;

}

void Fun3(int& a,int& b,int& c){a=b+x;b=c+y;

}

void main(){

x=10,y=15;Fun1(&y,&x,&y);Cout << x << " " << y << endl;Fun2(y,x,y);cout<< x << " " << y << endl;Fun3(y,x,y);cout<< x << " " << y << endl;

}

Answer(6 marks, 1 each):

40 2040 20160 80

6

Page 7: Hong Kong University of Science and Technologyhorner/comp104/exams/final04.doc  · Web view, 1 for each digit): 42 c) Complete the following program to print the user’s input word

Question 4 : Simple Linked List ( 10 marks)

Identify and fix the bugs. Given the following definitions of Node and NodePtr:

struct Node {int data;Node* next;

};typedef Node* NodePtr;

The following function is supposed to delete multiple elements from an unsorted simple linked list which contains only positive integers. Given a key element k(a positive integer), the function searches and deletes all its “multiples” that are greater than itself. (i.e. nk, n>=2). The input parameter “Head” points to the first element of the linked list and “delkey” is the key element whose “multiples” are to be deleted. There are FIVE mistakes in the following implementation. Please identify and fix them all(you may have to add lines if needed).

1 void delMultiple (NodePtr Head, int delKey){2 NodePtr prev, cur = Head;3 while(cur!=NULL){4 if(cur->data > delKey && cur->data % delKey == 0){5 if(cur!=Head)6 Head = Head->next;7 else8 prev->next = cur;9 NodePtr tmp = cur;1011 delete tmp;12 }13 else{1415 cur = cur->next;16 }17 } // end of while18 }

7

Page 8: Hong Kong University of Science and Technologyhorner/comp104/exams/final04.doc  · Web view, 1 for each digit): 42 c) Complete the following program to print the user’s input word

Answer(10 marks, 2 each): (-2 for each additional mistake)

(1) line 1: should be pass by refernece(2) line 5: change to “if(cur==Head)”(3) line 8: change to “prev->next = cur-> next”(4) line 10: add “cur = cur->next”(5) line 14: add “prev = cur”

Question 5 : Algorithms on Linked List ( 14 marks)

a) Find the errors and correct them. (7 marks)

Given the following definitions and function prototypes:

struct Node{ int data;Node *next;

};typedef Node* NodePtr;

//return the pointer of the node having data=item//return NULL if item does not exist in the listNodePtr searchNode(NodePtr Head, int item){……

}

//insert item into linked list according to ascending ordervoid insertNode(NodePtr& Head, int item){……

}

The following code segment is supposed to return the union of two simple linked lists. Assume the two functions, searchNode() and insertNode(), work properly, identify errors in the code and fix them.

1. void UnionLists(NodePtr Head1, NodePtr Head2){2. NodePtr Union, Cur;3. Union = Head1;4. Cur = Head2;5. while(Cur != NULL){6. if(searchNode(Union, Cur->data)) 7. insertNode(Union, Cur->data);8. Cur = Cur->next;

8

Page 9: Hong Kong University of Science and Technologyhorner/comp104/exams/final04.doc  · Web view, 1 for each digit): 42 c) Complete the following program to print the user’s input word

9. }10. return Head2;11. }Answer(7 marks):

a) Line 1: ‘void’ should be ‘NodePtr’ // 1 markb) Line 6 should be: if(searchNode(Union, Cur->data)==NULL) // 2 marks

c) Line 10: ‘Head2’ should be ‘Union’ // 2 marks

(-1 or more for adding other errors)

9

Page 10: Hong Kong University of Science and Technologyhorner/comp104/exams/final04.doc  · Web view, 1 for each digit): 42 c) Complete the following program to print the user’s input word

b) Given the following definition of an element in a linked-list:

struct Mylist{ int data; Mylist* next; };typedef Mylist* MylistPtr;

Write a function, findTotal(), which takes a simple linked list and returns the sum of all linked-list data. (7 marks)

Answer(7 marks):

int findTotal(MylistPtr head){ // 1 markint total = 0; // 1 markwhile(head!=NULL){ // 1 mark

total += head->data; // 2 markshead = head->next; // 1 mark

}return total; //1 mark

}

(-1 or more for adding other errors)

10

Page 11: Hong Kong University of Science and Technologyhorner/comp104/exams/final04.doc  · Web view, 1 for each digit): 42 c) Complete the following program to print the user’s input word

Question 6 : Doubly Linked List ( 18 marks)

We will use the following definitions throughout this question: struct Node {

int data;Node* next; Node* prev;

};typedef Node* NodePtr;

a) Write a print function for a doubly linked list with dummy head node such that all the elements are printed out in the REVERSE order. For example, your function should print out “30 20 10” for the following doubly linked list: (4 marks)

Void print(NodePtr Head){// please add your code here

}Answer(4 marks):

void print(NodePtr Head){

11

10 20 30

Head

Page 12: Hong Kong University of Science and Technologyhorner/comp104/exams/final04.doc  · Web view, 1 for each digit): 42 c) Complete the following program to print the user’s input word

NodePtr Cur = Head->prev; // 1 markwhile (Cur!=Head){ // 1 mark

cout << Cur->data << “ “; // 1 markCur = Cur->prev; // 1 mark

}cout << endl;

}b) Write a function to add a node at the end of a doubly linked list with dummy head node.(7 marks)

void insertEnd(NodePtr& Head, int item){// please add your code here

}

Answer(7 marks, 1 for each line):

void insertEnd(NodePtr& Head, int item){

NodePtr New,Cur; New = new Node;

12

Page 13: Hong Kong University of Science and Technologyhorner/comp104/exams/final04.doc  · Web view, 1 for each digit): 42 c) Complete the following program to print the user’s input word

New->data = item;

New->next = Head;New->prev = Head->prev;Head->prev = New;New->prev-next = New;

}

c) Somebody has built a partially doubly linked list(Figure 1) by using only the first two member variables of the node, “data” and the “next” pointer, while leaving the “prev” pointer unused. In Figure 1, Head is a pointer pointing to the dummy head.

Now write a function to convert the above partially doubly linked list into a doubly linked list. It should take the Head pointer of a partially doubly linked list(as shown in Figure 1), and return the head of the doubly linked list(as shown in Figure 2). Use what we have already got, DO NOT allocate any new nodes. (7 marks)

NodePtr convertToDoubly(NodePtr Head){// please add your code here. // DO NOT build the doubly linked list from scratch.

Answer(7 marks):

NodePtr convertToDoubly(NodePtr Head){

NodePtr ptr1 = Head; // 1 markNodePtr ptr2 = Head->next; // 1 mark]

13

10 20 30

HeadFigure 1

10 20 30

HeadFigure 2

Page 14: Hong Kong University of Science and Technologyhorner/comp104/exams/final04.doc  · Web view, 1 for each digit): 42 c) Complete the following program to print the user’s input word

do{ptr2->prev = ptr1; // 1 markptr1 = ptr1->next; // 1 markptr2 = ptr2->next; // 1 mark

}while(ptr1!=Head); // 1 markreturn Head; // 1 mark

}

Question 7: Classes(10 marks)

Consider the following class and answer questions.

#include <iostream>#include <string>using namespace std;

class Currency {private: string code; double amount;public: Currency(); Currency(double amt, string c = "HKD"); string getCode() const { return code; }; double getAmount() const { return amount; }; void add (double amt) { amount += amt; }; Currency exchange(string newCode, double rate); void print();};

Currency::Currency() { code = "HKD"; amount = 0;}

Currency::Currency(double _amount, string _code) { code = _code; amount = _amount;}

void Currency::print()const { cout << code << " " << amount;}

a) What is wrong with the following code? (2 marks)

14

Page 15: Hong Kong University of Science and Technologyhorner/comp104/exams/final04.doc  · Web view, 1 for each digit): 42 c) Complete the following program to print the user’s input word

int main () { Currency x, y; x.add (3.9); if (x.code == y.code) cout << "Same code!" << endl; else cout << "Different code." << endl; return 0;}

Answer: (2 marks)Cannot access the private data directly

b) What is the output of the following program? (3 marks)

int main () { Currency a, b(16.8); Currency c(100.5, "EUR"); a.add (2.3); b.print(); cout << endl; c.print(); cout << endl; a.print(); return 0;}

Answer: (3 marks, 1 for each) HKD 16.8 EUR 100.5 HKD 2.3

c) The member function “exchange” is used to buy a foreign currency under a specified exchange rate using the amount of money stored in an object. For example:

Currency dollar = Currency(10, "USD"); Currency cad = dollar.exchange("CAD", 1.1969);

Then cad.amount == 11.969 and cad.code == "CAD". Write the implementation of the member function "exchange". The data in the original object should not be modified by this function. (5 marks)

15

Page 16: Hong Kong University of Science and Technologyhorner/comp104/exams/final04.doc  · Web view, 1 for each digit): 42 c) Complete the following program to print the user’s input word

Answer: (5 marks)Currency Currency::exchange(string newCode, double rate) { // 1 mark // should not modify any data members; otherwise, zero markdouble new_amount = amount * rate; // 1 markCurrency r(newCode, new_amount); // 2 marks, 1 for right constructor, 1

// for passing correct values return r; // 1 mark}

Question 8 : Dynamic C lasses /ADT ( 24 marks)

A class OrderList will implement an ordered integer list (in increasing order) using linked list (singly linked without dummy head). The class is defined as follows:

struct Node { int data;Node* next;

};

typedef Node* Nodeptr;

class OrderList { public:

// default constructorOrderList();// copy constructor

OrderList(const OrderList& list);// destructor~OrderList();// return true if the list is emptybool empty() const;// add an integer into the ordered listvoid addElement(int item); // delete the integer from the ordered listvoid delElement(int item);// return the length of the listint length() const;// print all the element in the listvoid print() const;// merge two sorted lists into one sorted listOrderList merge(const OrderList& list) const;

private:

16

Page 17: Hong Kong University of Science and Technologyhorner/comp104/exams/final04.doc  · Web view, 1 for each digit): 42 c) Complete the following program to print the user’s input word

Nodeptr head;};

a) Implement the member function empty(). (1 mark)

bool OrderList::empty() const {

}

Answer: (1 mark) return (head = = NULL);

It’s also fine with return (length()==0);

b) Implement the destructor of this class. (3 marks)

OrderList::~OrderList (){

}

17

Page 18: Hong Kong University of Science and Technologyhorner/comp104/exams/final04.doc  · Web view, 1 for each digit): 42 c) Complete the following program to print the user’s input word

Answer: ( 3 marks)

Nodeptr cur;while(head!=NULL){ // 1 mark

cur = head; // 0.5 pt head = head->next; // 0.5 ptdelete cur; // 1 mark

}

c) Complete the implementation of the member function addElement(int item) that adds the given element to the list in increasing order (6 marks):

void OrderList::addElement(int item){Nodeptr newp, cur, pre;

pre = NULL;cur = head;

// first, find the position cur and prewhile( (cur != NULL) && (item>cur->data)){

pre = cur;cur = cur->next;

}

if ((cur == NULL) || (item != (cur->data))) {// second, if the element is not already in the list

// create and insert the element right before cur

18

Page 19: Hong Kong University of Science and Technologyhorner/comp104/exams/final04.doc  · Web view, 1 for each digit): 42 c) Complete the following program to print the user’s input word

} // end of if} // end of function

Answer: (6 marks)newp = new Node; // 1 mark for creationnewp->data = item;

// if don’t treat special case, 3 marksIf (pre == NULL){ // 1 mark

newp->next = head; // 1 markhead = newp; // 1 mark

} else {pre->next = newp; // 1 marknew->next = cur; // 1 mark

}

d) Implement the copy constructor, which takes a list and makes a copy of it. You can use addElement() method as defined in the previous question. (4 marks)

OrderList::OrderList(const OrderList& list){

}

Answers: (4 marks) head = NULL; // 0.5 mark

Nodeptr cur = list.head; // 0.5 markwhile(cur != NULL) { // 1 mark addElement(cur->data); // 1 mark cur = cur->next; // 1 mark

}

19

Page 20: Hong Kong University of Science and Technologyhorner/comp104/exams/final04.doc  · Web view, 1 for each digit): 42 c) Complete the following program to print the user’s input word

e) The method merge is supposed to merge the two ordered lists into one ordered list. For example, there is an ordered integer list, list1, and another list, list2, list1.merge(list2) will return the merged list. Both list1 and list2 will not be changed after merging.

Write the implementation of this member function. You can use the member function void addElement(int data) that has been already defined. (8 marks)

OrderList OrderList::merge(const OrderList& list2) const{

}

20

Page 21: Hong Kong University of Science and Technologyhorner/comp104/exams/final04.doc  · Web view, 1 for each digit): 42 c) Complete the following program to print the user’s input word

Answer: (8 marks)

Solution 1:OrderList OrderList::merge(const OrderList& list2) const{

Nodeptr Cur1, Cur2;Cur1 = head;Cur2 = list2.head; // proper declaration/initialization, 1 mark

OrderList result; // creation of ‘result’ 1 mark

// totally 2 marks for the first part of resultwhile((cur2 != NULL) { // 1 mark

result.addElement(Cur2->data); // 0.5 markCur2 = Cur2->next; // 0.5 mark

}

// or OrderList result(list2);

// totally 3 marks for the second partwhile((cur1 != NULL) { // 1 mark

result.addElement(Cur1->data); // 1 markCur1 = Cur1->next; // 1 mark

}

return result; // 1 mark}

Solution 2: this solution totalizing 11 marks is more difficult than the first one, but more efficient. We will try to give as many as marks we can up to 8.

use ‘addEnd’ instead of addElement is correct (even better), but not ‘addHead’

OrderList OrderList::merge(const OrderList& list2) const{

Nodeptr Cur1, Cur2;Cur1 = head;Cur2 = list2.head; // 1 mark

// the algorithm consists of 2 major parts.

OrderList result; // 1 mark

// first, we examine two lists element by element.

21

Page 22: Hong Kong University of Science and Technologyhorner/comp104/exams/final04.doc  · Web view, 1 for each digit): 42 c) Complete the following program to print the user’s input word

// Each time, we compare the beginning elements of the two lists, // take the smaller one, add it to the resulting list, // and move forward for the following elements.

while ((Cur1 != NULL) && (Cur2 != NULL)){ // condition: 1 marksif (Cur1->data > Cur2->data){ // 1 mark

result.addElement(Cur2->data); // 0.5 markCur2 = Cur2->next; // 0.5 mark

}else { result.addElement(Cur1->data); // 0.5 mark

Cur1 = Cur1->next; // 0.5 mark}

}

// second, // there may still be elements left over in one of the lists,// we add these remaining elements in the final result.

while (Cur1 != NULL) { // 1 markresult.addElement(Cur1->data); // 0.5 markCur1 = Cur1->next; // 0.5 mark

}while (Cur2 != NULL) { // 1 mark

result.addElement(Cur2->data); // 0.5 markCur2 = Cur2->next; // 0.5 mark

}

return result; // 1 mark}

f) Explain the meaning of two const in the prototype of the function merge. (2 marks)

OrderList OrderList::merge(const OrderList& list2) const

(1) (1 mark) the first const before OrderList& :

22

Page 23: Hong Kong University of Science and Technologyhorner/comp104/exams/final04.doc  · Web view, 1 for each digit): 42 c) Complete the following program to print the user’s input word

(2) (1 mark) the second const at the end of the line after the closing parenthesis:

Answer: (2 marks, 1 for each)

(1) The const before OrderIntList& is to ensure object list1 (or calling object) won’ be altered.

(2) The const at last of the function merge is to ensure the object itself does not change.

23