You are on page 1of 11

COS1512/104/1/2012

Tutorial Letter 104/1/2012


Introduction to Programming II

COS1512
Semester 1

School of Computing
Examination Tutorial Letter.

Bar code
CONTENTS

1 PURPOSE OF THIS LETTER ........................................................................................................ 3

2 TUTORIAL MATTER DISPATCHED TO DATE............................................................................. 3

3 EXAMINATION ADMISSION ......................................................................................................... 3

4 THE EXAMINATION ...................................................................................................................... 4

5 LAYOUT OF THE COS1512 EXAMINATION PAPER................................................................... 5

6 PREVIOUS EXAMINATION PAPER .............................................................................................. 6

7 MEMORANDUM WITH SELECTED ANSWERS ......................................................................... 11

Please note / important notes:

Please use the information in this tutorial letter to help you prepare for the exam.

Good luck!
COS1512 Team

2
COS1512/104

1 PURPOSE OF THIS LETTER

Dear student,

This Tutorial Letter contains information pertaining to the examination as well as an examination
paper and selected answers from the memorandum of a previous year. Please make sure you
read this tutorial letter at least once before the examination.

Many students found this module difficult to tackle. For this reason you may have spend more time
on this module particularly towards completing the assignments. Every effort you made to tackle
the subject will definitely come in handy in the examination.

2 TUTORIAL MATTER DISPATCHED TO DATE

If you have not received all of the above-mentioned tutorial matter, please contact our DESPATCH
DEPARTMENT on 011 471-3249/2248.

CD Prescribed software

COS1512/101/3/2012 First tutorial letter: General information, study programme, exam admission and
assignments
COS1512/102/1/2012 Invitation to Workshop

COS1512/201/1/2012 Solution to Assignment 1

COS1512/103/1/2012 Solution to Workshop Exercises

COS1512/202/1/2012 Solution to Assignment 2

COS1512/104/1/2012 This letter: Examination Tutorial Letter

Note that all the COS1512 tutorial letters will also be available on myUnisa.

3 EXAMINATION ADMISSION
The university will inform you whether you obtained admission to the examination shortly, and if
you have, the time and the examination centre. It is your responsibility to make sure of the correct
examination date, time and centre. If you need information regarding the examination date, time or
venue please contact UNISA’s Examination Department.

Examination admission will be based on whether or not you submitted an assignment before the
cut-off date specified by the Department of Education. Since this turns examination admission into

3
an administrative matter, all requests and queries relating to examination admission should be
directed to:

E-mail: exams@unisa.ac.za

SMS: 43584

Fax: 012 429 4150

4 THE EXAMINATION

Please note the following:

¾ The exam paper covers all the work outlined in Tutorial Letter 101 and the Study Guide
(Appendix F in Tutorial Letter 101) and specifically tests the outcomes specified in the Study
Guide. You need to use the prescribed book (Savitch 8th Edition or Savitch 7th edition) as
well as all the COS1512 Tutorial Letters in your exam preparation.

¾ Work through all your assignments and the tutorial letters providing solutions to the
assignments, as well as the solutions to the workshop again, even though you may have
achieved a good mark for the assignments. This is extremely important. We included
additional notes in the solutions that should assist you in understanding the work.

¾ Do not ignore assignment 3. Assignment 3 is self-assessment. You should first attempt it and
then work through the solution provided in Tutorial Letter 101. Students frequently fail the
COS1512 exam because they have not mastered the last part of the study material, which is
covered by assignment 3.

¾ The examination consists of a single 2-hour paper.

¾ COS1512 is a closed book examination meaning that you are not allowed to take any notes
into the examination room.

¾ No calculators are allowed for the COS1512 examination.

¾ The COS1512 exam does not include multiple choice questions.

¾ It is a written examination. C++ programs and parts of C++ programs must be written in your
examination answer book. Computers are not used during the examination. You may answer
in pencil.

¾ It is important to practice by doing the examination paper below, by writing the answers down
on paper and only once you have completed it, comparing your answers with the
memorandum. You can also, once you have written the answers down on paper, type it into a
C++ program and test it yourself. We do not provide the complete memorandum, since it is
important that you attempt the questions by yourself.

4
COS1512/104

¾ Since the semester system requires 2-hour papers, the total for the May 2012 examination
paper is 75 marks. Therefore some questions may cover more than one learning outcome or
topic.

When writing the exam, it is important that you use the available time well. Determine the amount
of time that you should spend on a certain section or question by allowing more or less one-and-a
half minutes for each mark. A question with a total of 10 marks should therefore not take longer
than 15 minutes to answer. If you follow this time scheme, you will have 12 minutes for the
following:

• To read the exam paper calmly and properly before you start answering the questions.

• To read the answers again after completing the exam paper.

• To complete the questions that you struggled with.

It is advisable to read the exam paper properly before you start answering the questions. Initially
answer those questions that are easy for you leaving enough space for difficult questions or
questions that may require more time. In this way, you will have sufficient time to tackle the difficult
questions. Make sure you have completed all the answers before handing over the examination
answer book to the invigilator.

5 LAYOUT OF THE COS1512 EXAMINATION PAPER

This section provides an estimate of the marks allocated to the different main sections of the work
covered in this module. You need to know all the work as outlined in Section 4 of this letter,
including the work covered in COS1511.

File processing 5

C strings 5

Pointers/dynamic arrays 5

Recursion 5

Classes including inheritance 45

Templates and vectors 10

5
6 PREVIOUS EXAMINATION PAPER
QUESTION 1 [5]
1.1 Consider the following declaration: (1)
char s[6];
What is the maximum string length that can be stored in s? Explain your answer.

1.2 What would be the expected output from the following? (2)
char sms[5] = "Gr8";
int index = 0;
while(sms[index] != '\0')
{
sms[index] = 'x';
index++;
}
cout<<sms;

1.3 Write C++ statements to accomplish the following: (2)


• Declare a vector secretlist to store objects of type int - do not specify the size of
secretlist.
• Store the following values into secretlist: 56 and 28.
• Reset the first element of secretList to 12.

QUESTION 2 [5]
2.1. Consider the following declarations:
int * px;
int * py;
int x = 10;
int y = 20;

2.1.1 Provide C++ statements so that pointer variables px and py point to variables x and y respectively. (2)

2.1.2 Write a C++ code segment to recreate the pointer structure given below. (1)

2.2. Give an example of a dynamic variable. (1)

2.3 Discuss one advantage to using dynamic arrays. (1)

6
COS1512/104

QUESTION 3 [35]

Define a class Booking that represents one booking for a performance at a venue. This class has four member
variables:

• show, a string that holds the name of the show or performance


• nrTickets, an integer value that indicates the number of tickets booked
• seats, a string that holds seats booked (either “standing” or the seat numbers, e.g. “A6-A10”)
• customer, a string containing the name of the person who made the booking

In addition, the class should have the following member functions:

• A default constructor that initializes show, seats and customer respectively to an empty string.
nrTickets should be initialized to 0.
• An overloaded constructor that accepts a new booking and sets show, seats, customer and
nrTickets to specified values.
• A destructor that does not perform any action.
• An overloaded equality operator== to compare two bookings. The == operator is implemented as a
friend function with the following prototype:
bool operator==(const Booking & booking1, const Booking & booking2)
This function returns true if booking1and booking2 has been made for the same show and customer; and
false if not.
• An overloaded operator+ for class Booking so that the following are feasible in the main program: a =
b + c; where a, b, and c are all Booking objects. The overloaded operator+ should add the values of
the nrTickets member variables. It should also concatenate the value of the seats member variable of
the second operand to the value of the seats member variable of the first operand. The show and
customer member variables should be the same as for the first operand. The overloaded operator+
should return a Booking object. The prototype is as follows:
Booking &operator+ (const Booking & b1, const Booking & b2);
The overloaded operator+ will allow a customer to buy more tickets and book the seats for the tickets
under the same name.
• A member function calcFee() to determine the amount charged for the tickets. Use the following
prototype:
float calcFee();
The fee for “standing” tickets is R100.00 per ticket. All other tickets cost R200.00 per ticket.
• An overloaded extraction operator >> (implemented as a friend function) so that it can be used to
input values of type Booking.
• An overloaded insertion operator << (implemented as a friend function) that displays all the member
variables of a Booking object.

You should attempt the solutions as follows:

3.1 Create the header file Booking.h that contains the Booking class specification. (8)

3.2 Create the implementation of the class Booking including all the friend functions. (17)

3.3 Complete the application program (main()) below by citing the number and writing down the missing
statement. This program obtains the detail for a booking from the user and creates an object to represent the
booking. It then reads in all existing bookings from a file Bookings.dat and checks for a previous booking for
the customer for the same show. If a previous booking has been made, the +operator is used to update the new

7
booking to include the previous booking. The booking is then displayed as well as the amount due. Be sure to
use appropriate member functions in the required statements. (10)

#include <iostream>
#include <iomanip>
1 //1.Include files needed
using namespace std;

int main()
{
2 //2.Declare input file

3 //3.Open the file Bookings.dat and check


// that the file exists

//Add a booking
string bShow, bSeats, bCustomer;
int bTickets;
cout << "Enter show name: ";
cin >> bShow;
cout << "Enter seats requested: ";
cin >> bSeats;
cout << "Enter number of tickests requested: ";
cin >> bTickets;
cout << "Enter customer name: ";
cin >> bCustomer;

4 //4.Instantiate object newBooking for the


// new booking

//check for existing booking


Booking aBooking;
bool found = false;

while (!found && 5 ) //5. Read in a Booking object


//from the input file into object aBooking
{
if 6 //6.Compare new booking with aBooking
found = true;
}

//if previous booking has been made, update the new booking
if (found)
7 //7.Update new booking

cout.setf(ios::showpoint);
cout.setf(ios::fixed);
cout << setprecision(2);
cout << "Booking confirmed:" << endl;
8 //8.Display the new booking
//9. Calculate and display the amount due for the new booking
cout << endl << "Amount due: R" << 9 << endl;

10 //10. Close input file

return 0;
}

8
COS1512/104

QUESTION 4 [15]
Consider the following class:

class Loan
{
public:
Loan();
Loan(double amt, double rate, int term);
double get_loanAmt()const;
double get_intRate() const;
double get_termOfLoan()const;
void setAmt(double amt);
void setRate(double rate);
void setTerm(int term) ;
void output(ostream & out) const;
double calculate_interest() const;
protected:
double loanAmt;
double intRate;
int termOfLoan;
};

4.1 Define a new class CarLoan that inherits the functionality of the class Loan. Class CarLoan has additional
member variables:

• string carModel (the model of the car)


• int modelYear (the year of the car)
Moreover class CarLoan has member functions, setModel() and setYear() to reset member variables
carModel and modelYear respectively to values specified by parameters. CarLoan should override
functions calculate_interest() and output() of class Loan. Provide only the interface of class
CarLoan in terms of a header file. The header file should contain compiler directives to prevent multiple
definitions. Assume that the interface of class Loan is contained in file called Loan.h. (7)

4.2 Implement the overloaded constructor for the class CarLoan by invoking the base class constructor. (4)

4.3 Consider the following:


double CarLoan::calculate_interest()const
{
return (2*loanAmt * intRate * termOfLoan);
}

Suppose the access of member variables of class Loan were changed to private. Explain why the
redefinition in CarLoan of calculate_interest would result in compilation errors. Show how you
would fix these errors. (3)

4.4 Discuss one advantage of using inheritance in question 4.1. (1)

QUESTION 5 [10]

Consider the following class Database that maintains a database of customer names.

9
class Database
{
public:
Database();
void insert(string n);
void swap(int pos1, int pos2);
int count();
private:
vector<string> myData;
};

The class Database has the following operations:

insert – adds a string to the database

swap – swaps the strings at positions pos1 and pos2, i.e the string at position pos1 of myData is moved to
position pos2, and vice-versa.

count - returns the number of strings in the database

5.1. Write a template version of the Database class. In other words, redesign the Database interface so that it
may be used to create a database of any type of data. For example, Database that contains objects of type
Student, Car or Person. Provide only the interface. (5)

5.2. Provide the implementation of function swap from the template class Database. (4)

5.3. Provide a declaration for a Database object intended to contain of objects of type of Student. (1)

QUESTION 6 [5]

Function iterativeTriangle() below accepts as a parameter a nonnegative integer and generates a triangular
pattern of stars. For example, if the nonnegative number is 4, then the following pattern is generated:

****
***
**
*

void iterativeTriangle(int x)
{
if (x == 0)
return;
else
{
for (int i = 0; i < x; i++){
for(int j = x; j > i; j--){
cout<<"*";
}
cout<<endl;
}

6.1 If we rewrite function iterativeTriangle() as a recursive function recursiveTriangle(), what


would be the base case? (1)

6.2 Why do we have to include a base case in a recursive function? (1)


10
COS1512/104

6.3 What is the purpose of the general case in a recursive function? (1)

6.4 Write down the function header only for function recursiveTriangle(). (2)

UNISA 2011

7 MEMORANDUM WITH SELECTED ANSWERS

Question 1: [5]

1.1 A string of size 5, there must be one space left for the null character. (1)

1.2 XXX99 (2)

1.3 vector<int> secretList; ½


secretList.push_back(56);
secretList.push_back(28); 9
secretList[0] = 12; ½ (2 )

Question 2 [5]

2.1.1 px = &x; 9
py = &y; 9 (2)

2.1.2 px = py; 9 (1)

2.2 int *p = new int; 9 (1)

2.3 There is no preset upper limit on the number of items a dynamic array can contain - This is an
advantage in situations where a reasonable value for the size of the array is not known at the time it
is declared.9 (1)

Question 6 [5]

6.1 if (x == 0) return; 9 (1)

6.2 The purpose of the base case is to ensure that the recursion will stop eventually. If there is no base case, the
recursion will either not take place at all or become infinite. 9 (1)

6.3 The purpose of the general case is to generate the recursive calls which should eventually result in a call that
causes the base case to execute and end the recursion. 9 (1)

6.4 void recursiveTriangle(int x) 99 (2)

UNISA 2012

11