COS110 Tutorial 4-8August2011-MEMO

COS110 Tutorial 4-8August2011-MEMO - 1 COS110 Tutorial 4...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 COS110 Tutorial 4 MEMO-MEMO-MEMO Date: 10 August 2011 Time: 50 minutes Marks: 40 Student Surname and Initials:__________________________________________________ Student number:_______________________ Employee number:_____________________ Question 1 (string list ADT) [20 marks] Study the class specification given below and answer the questions that follow: class StringList { public: StringList(int size); ~StringList(void); void addString(const string &line, int position); string getString(int position); int getSize(void); int find(const string &line); private: string *list; int listSize; bool isValid(int position); void growList(int newSize); }; Write code for the following member functions: a. the constructor (1 marks) Solution: StringList::StringList(int size) { list = new string[size]; listSize = size; } 2 b. growLis t. Increases the size of the string list object. ( 3 marks) Solution: void StringList::growList(int newSize) { string *newList = new string[newSize]; (1 mark) for (int index = 0; index < listSize; index++) newList[index] = list[index]; (1 mark) listSize = newSize; delete [] list; list = newList; newList = 0; (1 mark) } c. addString . This function stores a string at the specified position. If the position value is larger than the current size, the list is grown to an appropriate size before the new string is added. (4 marks) Solution: void StringList::addString(const string &line, int position) { if ((position >= 0) && (position < listSize)) list[position] = line; (1 mark) else if (position >= listSize) { growList(position+1); list[position] = line; (2 mark) } else if (position < 0) cout << "list index out of bounds" << endl; (1 mark) } d. find. Returns the position in the string list of the string that is passed as an argument. If the string is not found, a value of -1 is returned. (4 marks) Solution: int StringList::find(const string &line) { bool found = false; int pos = 0; while ((pos < listSize) && !found) while ((pos < listSize) && !...
View Full Document

This note was uploaded on 02/27/2012 for the course IT 101 taught by Professor Unsure during the Spring '11 term at University of South Africa.

Page1 / 7

COS110 Tutorial 4-8August2011-MEMO - 1 COS110 Tutorial 4...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online