Download as pdf
Download as pdf
You are on page 1of 6
UNIVERSITY EXAMINATIONS. UNIVERSITEITSEKSAMENS ~— UNISA Tisxc COS1512 ‘October/November 2018 RCO1512 Introduction to Programming I! Duration 2 Hours 75. Marks EXAMINERS. FIRST DR MA SCHOEMAN SECOND MS A THOMAS Closed book examination ‘This examination question paper remains the property of the University of South Africa and may not be removed from the examination venue This paper consists of 6 pages and 6 questions Please make sure that you have all 6 pages with the 6 questions Instructions: ‘Answer all the questions Do all rough work in the answer book ‘The mark for each question 1s given in brackets next to the question Please answer the questions in the correct order If youwant to do a question later, leave enough space Number your answers and label your rough work clearly Marks are awarded for part of an answer, so do whatever you are able to in each question GOOD Luck’ TURN OVER cosisiaRco1st2 OcuiNov 2018 QUESTION 1 [4] 11 Explain the difference between char short_string{} = "123", char short_string() = ('2')'24,439) @) 12 What output would be produced from the following C++ code fragment (2) Vector myList (4), myLast {0} for(int 1 a4) { myList(2] = 2 * myList{: - 1], fH nyhist push_back (34); for (int 2 = 0, 1 < mybist saze(), itt) { cout<< myList[i]<<" "| } [QUESTION 2 (5 An anthmetic senes is a number of integers in ascending or descending order where the difference between any two adjacent terms 1s a constant, for example 2, 6, 10, 14, 18, 22, 26,30 1s an anthmetic series in ascending order of which 2 1s the first term, 22 1s the 6” term and the constant difference 1s 4 The following incomplete program uses recursion to calculate the nth term of an ascending arithmetic senes starting at 1, with a constant difference of 3, 1@ the series will be 1, 4, 7, 10, 13, 16, 19, etc Read the questions below and supply code for the lines indicated You do not have to wnte down the ‘complete program in your answer book Only write down the line number and the answer 1 finelude 2 3 using namespace std; 4 ant arith{ int term ) 5s 6 ant farst = 1, ant diff = 3, 7 @ af (term == 1) 9 return (1), 10. else a 12 4 13° ant main() 14. ( 15 ant term, answer, 16. cout << “Please key in the number of the term "<< endl, 17, cin >> term, 18. answer = arath(term), 19. cout << "the value of term" << term << "as " << answer , 20. 3 21 Make sure that the value for term 1s greater than 0, by using the assert statement Also remember to mclude the correct #include directive in line 2 @ cOsis12iRCO1512 ‘OcvNow 2018 22 Whats the base case? (1) 23 Determine what the general case should be and add the code in line 11 Hint the general case 1s the sum of the previous term plus the difference (2) [QUESTION 3 [4] 31 Consider the following code segment which is assumed to be embedded in a complete and correct C++ program and answer the questions that follow ant x7 ant yz ant #r = ex: ant ‘s = gy, te = 35, +5 = 38, te = ts, coutccx <<" "ecyccendl, couteerr to compare two Surtcase objects The overloaded companson operator> is implemented as a £raend function with two parameters to compare two suitcases This function returns true if the ratio for the first suitcase has a larger value than the ratio for the second suitcase, and false if not * An overloaded stream extraction operator>> (implemented as a frend function) so that it can be used to input values of type suitcase + An overloaded stream insertion operator<< (implemented as 2 friend function) to display the values of the member vanables of an object of the class Suatcase in the same format as shown in the expected output 41 Create the header fle sua tcase.h that contains the Sua tcase class specification ©) 42. Create the implementation of the class Suitcase including all the friendfunctions (11) 43° Complete the application program (man {)) below by aiting the number and writing down the missing statement(s) Be sure to use appropriate member functions in the required statements This program processes the file Suitcase.txt to determine the suitcase with the best volume/weight ratio (10) finclude <1ostream> 1 7/1 Include files needed using namespace std, ant _main() ‘ //2 Declare input file Snes rerrcrca/ fs Open fale for the suitcases and //check that file exists 4 //4.Instantiate two objects current_suitcase and 7/_ best_suatcase 5 //5.tntialise object best_suitcase by extracting the // faxst object from file Suatcases txt while ( 6 )//6.Bxtract an object current_surtcase from // f2le Surtcases txt ( af | 1 1//7.Compaze object current_surtcase with TURN OVER costs12Rc01512 Oct/Nov 2018 // object best_suatcase a //8. Update best_suitcase if necessary I 2 //9.Close fale cout sett (10s- fixed), cout.setf (10s: showpoint), cout.precision({2), ——_10 ____//10. pisplay the suitcase with the best ratio Feturn 0, Ht 44 ©) 441 White a statement to declare an array of fifty Suitcase objects a) 442 Use the following declaration suitcases = x, //assume x indicates the number of suitcases /{ that Refilwe considered and the array in 4 4 1 to wnte a program fragment to determine the average ratio of the suitcases Refilwe considered (4) int n QUESTION § t7] The following classes form part of a document management system There are two forms of documents, Nessages and Reminders The Messace document has a sender, a receiver and a body of text, while the Remincer document consists of a date and a body of text class Document ( public friend void Decryption( }, /idecrypts text body Document {const string & text ) , void Encryptor (int a), //encrypts text body void Eneryptor (strung k), //encrypts text body void display(} const protected int getsize() const, //returns size of body private string text_body, he class Message publac Document t public Message [const string & text, const string & s, const string & 0), voad display(} const, string sender, private string receiver, b 51 What is the difference between redefining a base class function and overloading a base class function? Give examples of each from the above code (4) TURN OVER 52 53 54 55 56 COS1512/RCO1512 OctNov 2018 Does the frend function of the Document class have any special access to members of the Message class? Explain (2) Implement the constructor for the Message class by invoking the base class constructor (3) The following code fragments result in compilation errors Identify and explain the probable cause of these errors (2) () inthe implementation of the display member function for the Message class vord Message’ display() const cout << sender << receiver << text_body, , (») in the following statements embedded in a complete main program Message Mi"This 1s a Message”, "karabo", "Jack"), Document D("This 1s a Document), M.senders D.getsize(, Redesign the interface of the Document: olass to prevent the errors produced in (5 4) from ‘occurring (2) White code for the Remznder class interface which ts derived from the class you defined in (5 5) Do not provide any implementation (Assume that the interface for the class Date 1s defined in Date.) 4) QUESTION 6 [10] Consider the following class class Storage ‘ public: Storage(int size), ant getTtemAtPosition(int pos), void storertem(int pos, int item), bool find (int item); private vector containers; oa The class Storage has the following operations 61 62 63 getIzemAt Position — retums the element at position, pos from contazners storeitem ~ stores element, tem at position pos in contazners find = retums true if element 1 tem was found in contaaners Whte the interface for a genen version of the Storage class so that it may be used to create storage for any type of tem Provide only the interface. 6) Implement the stozeitem() member function of the template class Storage 4) Provide a declaration for a Storage object intended to contain 10 objects of type of Car (1) e UNISA 2018

You might also like