COS132 ST2 Final 1 PDF
COS132 ST2 Final 1 PDF
Examiners
Internal: Dr Linda Marshall, Dr Patricia Lutu, Mr Emilio Singh, Ms Ntombi Banda, Mr Pula Rammoko, Ms Tari
Mautsa and Mr Theo Naidu
Instructions
1. Read the question paper carefully and answer all the questions.
2. The assessment opportunity comprises of 7 questions on 7 pages.
3. 2 hours have been allocated for you to complete this paper. An additional 30 min have been allocated for you to
download the paper and upload your answer document. The paper is divided into 2 sections, a practical section
(Section A) and a theoretical section (Section B).
• You should not spend more than 50 min on Section A
• Section B is scheduled for 70 min of the 2 hours.
4. This paper is take home and is subject to the University of Pretoria Integrity statement provided below.
• You are allowed to consult any literature.
• You are not allowed to discuss the questions with anyone.
5. If you have any queries when writing the paper, post them in good time on the ClickUP Discussion Board under the
appropriate thread, by replying to the thread, in the Semester Test 2 Forum. Note, this forum is moderated and
therefore your post will not be available for viewing to the COS132 class until is has been moderated.
6. An upload slot, per Activity, will be open on assignments.cs.up.ac.za for questions in Section A, that is from
Question 1 to 3, from 11:30 to 13:30.
• Note that file names are case sensitive. Failure to name the files as specified will result in no marks being
awarded.
• Each question (activity) is self contained and all of the code you will require to complete it will be provided on
the appropriate question slot.
• Each question will require you submit a separate archive to an individual upload slot. That is, each separate
question will require its own answer archive upload.
• Make sure your name and student number is clearly visible in the comments of the programs (.cpp files)
you upload for Section A
1
7. Write your answers for Section B in a separate document (eg. using a word processor) and submit the document in
PDF format. An upload slot will be open on the module ClickUP page under the Semester Test 2 - Question
paper and upload menu option for questions from Section B for the duration of the examination (11:30 to 13:30)
and then for an additional 30 min to give enough time to download this paper and upload your PDF. No late
submissions will be accepted.
• Please make sure your name and student number is clearly visible in the document you upload.
• Additionally, provide an e-mail address and a phone number on your paper where you can be contacted, should
there be any problems, in your Section B document.
8. Marks per question
Question: 1 2 3 4 5 6 7 Total
Marks: 5 10 15 15 10 12 20 87
Integrity statement:
The University of Pretoria commits itself to produce academic work of integrity. I affirm that I am aware of
and have read the Rules and Policies of the University, more specifically the Disciplinary Procedure and the Tests
and Examinations Rules, which prohibit any unethical, dishonest or improper conduct during tests, assignments,
examinations and/or any other forms of assessment. I am aware that no student or any other person may assist
or attempt to assist another student, or obtain help, or attempt to obtain help from another student or any other
person during tests, assessments, assignments, examinations and/or any other forms of assessment.
Section A - Practical
1. Activity 1 (5)
For this practical activity of the semester test, you are required to write a function that computes Euclidean
distance. The function takes an array of inputs, of size 4, where the first two elements of that array, are the
first x and y coordinates of the first point and similarly for the last two elements. The output of the function
is a double value, representing the distance between the two points.
Your Euclidean function should be implemented a file called distance.cpp. In addition to containing your
function, this file should contain the main function for your code. When run, the program should read from a
file called d1.txt. The file will have just one line consisting of 4 double values separated by commas, such as
1.1,2.2,3.3,4.4
The first two values are for the first point, and the second two values are for the second point.
Once calculated, your code should just output the distance with a newline at the end.
Requirements
You will have a maximum of 5 uploads for this activity. You should include a blank d1.txt, distance.cpp and
are encouraged to use the math, iostream, sstream, string and fstream libraries and to make use of functions.
Remember to include your makefile.
2. Activity 2 (10)
For this practical activity of the semester test, you are required to write code, map.cpp, that reads in a file
and stores it in a 2D array. The file is named m1.txt and consists of 10 rows. Each row comprises of 10
columns containing a single character. Your code needs to read this file into the 2D array and then print this
information to the screen. Each row of output should have a newline at the end of the row. Note, there are no
delimiters that separating the characters in each row.
Requirements
You will have a maximum of 5 uploads for this activity. You should include a blank m1.txt, map.cpp and
are encouraged to use the math, iostream, sstream, string and fstream libraries and to make use of functions.
Remember to include your makefile.
3. Activity 3 (15)
For this practical activity of the semester test, you are going to implement the delivery system as specified in
the scenario document. You will create a file called system.cpp. This file will contain code that does the
following:
Page 2
1. Read in and store the map in m1.txt into a 2D array
2. Process each of the deliveries specified in delivery.txt.
(a) For each delivery, calculate the profit potential of that delivery
(b) Output each delivery’s profit potential, and viability, with a newline at the end in the format specified
in the scenario document
Requirements
You will have a maximum of 5 uploads for this activity. You should include a blank, m1.txt and delivery.txt,
your system.cpp and are encouraged to use the math, iostream, sstream, string and fstream libraries and to
make use of functions. Remember your makefile.
Section B - Theory
4. Short questions
(a) For each of the questions that follow, choose the most appropriate option. (8)
i. Which one of the following is a correct function header for a function that computes and returns the
square root of a float value?
A. void squareRoot( float value)
B. float squareRoot( int value)
C. int squareRoot( float value)
D. float squareRoot( float value)
ii. Which of the following statements about arrays are true? (1) An array may contain elements of
different data types. (2) A constant array must have an initialisation list. (3) When an array is
passed as an argument to a function, the function has access to the original array.
A. (1), (2) and (3) are true.
B. (2) and (3) are true.
C. (1) and (2) are true.
D. Only (3) is true.
iii. Given the following array declaration:
const int SIZE = 5; int numbers[SIZE] = 10, 15, 20, 25, 30 ;
Which of the following statements will display all the values in the array?
A. f o r ( int i n d e x = 1 ; i n d e x <= SIZE ; i n d e x++)
c o u t << numbers [ i n d e x ] << ’ \ t ’ ;
iv. Which of the following is a correct declaration for a 2-dimensional array with 5 rows and 10 columns?
A. int table[5][10];
B. int table [10][5];
C. int table[5], [10];
D. int table[10, 5];
v. Given the following declarations:
f l o a t num1 = 1 0 . 5 , num2 = 5 . 2 , temp ;
Which of the following statements will correctly swap the values of num1 and num2;
A.
Page 3
num1 = num2 ;
num2 = num1 ;
B.
num2 = num1 ;
num1 = num2 ;
C.
temp = num1 ;
num1 = num2 ;
num2 = temp ;
D.
temp = num2 ;
num2 = temp ;
num1 = num2 ;
vi. Study the program statements below and answer the question that follows.
int num = 1 2 5 ;
int ∗numPtr ;
numPtr = &num ;
c o u t << numPtr << ’ \ t ’ << ∗numPtr ;
What will be displayed on the screen?
A. memory address of variable numPtr, followed by a tab, followed by 125
B. memory address of variable num, followed by a tab, followed by 125
C. value of num in hexadecimal followed by a tab followed by 125
D. memory address of variable numPtr followed by a tab followed by NULL
vii. Given the following statements:
const int SIZE = 5 ;
int v a l u e s [ SIZE ] = { 1 0 , 2 0 , 3 0 , 4 0 , 50 } ;
f o r ( i n d e x = 0 ; i n d e x < SIZE ; i n d e x++)
c o u t << v a l u e s [ i n d e x ] << ’ \ t ’ ;
Which of the following cout statements will produce the same output as the cout statement above?
A. c o u t << ∗ ( v a l u e s + i n d e x ) << ’ \ t ’ ;
C. c o u t << ∗ v a l u e s [ i n d e x ] << ’ \ t ’ ;
Page 4
B. &a r r a y P t r [ i n d e x ] = index ∗ 1 0 . 0 ;
C. ∗ a r r a y P t r [ i n d e x ] = index ∗ 1 0 . 0 ;
D. ∗ a r r a y P t r + i n d e x = index ∗ 1 0 . 0 ;
(b) What is the reason for undefined behaviour/errors that will occur when the following functions are called.
i. int ∗ m u l t i p l y ( int n ) (2)
{
int Value = n ∗ 3 ;
int ∗q = &Value ;
return q ;
} ( 2 marks )
(c) Indicate if the following overloaded functions are valid or invalid. (3)
i. int getMinimumValue ( int , int ) ;
int getMinimumValue ( int , int , int ) ;
5. Arrays
Use the code below to answer the questions that follow.
s t r i n g s t r e e t m a p [ 1 0 ] = { ”O” , ”−” , ”−” , ” ∗ ” , ” ∗ ” , ” ∗ ” } ;
int ∗ a l l o c a t e ( int n S i z e )
{
int ∗ p t r = new int [ n S i z e ] ;
return p t r ;
}
int main ( )
{
int ∗ a r r a y = a l l o c a t e ( 2 5 ) ;
// do s t u f f w i t h a r r a y
Page 5
//
//
// d e a l l o c a t e memory h e r e ! ! !
return 0 ;
}
ii. The 10-by-10 maps very soon become too limiting for the delivery service company. They decide to (6)
include two integers at the beginning of each map file to indicate the number of rows and columns
used to define the map in the map file. The typical 10-by-10 map file (d1.txt) from the scenario will
change to the following:
10 10
#O########
#**------#
#**--**--#
#----**--#
O--------#
#***--**-#
#***--**-O
#***--**-#
#--------#
########O#
Write the main program that reads the map into an appropriately sized, rows-by-columns array and
then frees the memory assigned to the array.
6. Functions
(a) The delivery company needs to consider the cost of petrol used in delivering packages. The value of the
petrol used is determined by the price of petrol (per litre), delivery car fuel consumption (in litres/100km),
and travel distance (in km). The company has taken a great liking to a particular car brand. Therefore,
the great majority of its fleet consists of that car brand which has a fuel consumption of 8.2 litres/100km.
i. Write the prototype for the function computePetrolCost that estimates and returns the value of the (2)
petrol used in delivering a package. The function prototype should appropriately reflect any default
values.
ii. Implement the computePetrolCost function such that it returns the value of the petrol used in a (2)
delivery. The petrol cost is the product of the three input parameters. Make sure to apply any
necessary value conversions.
(b) Suppose the delivery system has a function that sends an email to a driver to instruct them to fulfil a (6)
delivery order, as defined below.
void sendOrderToDriver ( s t r i n g o r d e r , s t r i n g d r i v e r E m a i l )
{
sendEmail ( d r i v e r E m a i l , o r d e r D e t a i l s ) ;
}
Modify the sendOrderToDriver function such that it keeps count of the number of order instructions
sent. Add a parameter called shouldReset that makes it possible to reset the count to 0 (for example, at
the start of a new day). The function should return the count.
(c) Overload the Euclidean distance function you implemented in Question 1 to accept four integer values as (2)
input. The first two integer parameters should correspond to the x and y coordinates of the first point, and
the last two integer values should correspond to the x and y coordinates of the second point, respectively.
The formula for the Euclidean distance (Equation (3) in the Scenario and repeated below) is:
p
d(p, q) = (q1 − p1 )2 + (q2 − p2 )2
Page 6
(c) Consider the following array definition which shows an array of profit margins for some of the different
deliveries.
double p r o f i t m a r g i n s [ ] = { 0 . 4 , 2 . 5 , 5 . 0 , 1 0 . 0 , 1 5 . 0 , 2 0 . 5 }
i. Write a function getLargest(double numbers[], int size), which accepts the array profitmargins (5)
and size as its arguments. The function getLargest should return the largest number stored in the
array.
ii. In the function getLargest, the contents of the array can be easily modified. Write a function header (1)
for the function getLargest such that the contents of the array passed as an argument cannot be
modified.
iii. Suppose the delivery company wants to order the profit margins for the different delivery destinations (4)
starting with deliveries with the lowest profit margin. The incomplete code below implements an
efficient bubble sort algorithm that can be used to sort an array of profit margins in ascending order
where size is a parameter that holds the size of the array that can be used to store the profit margins
and the parameter arr holds the address of the array passed as an argument. The missing expressions
and/or instructions are numbered [i] to [iv].
In a bubble sort, after the first pass, the largest number is guaranteed, to be the highest-indexed
element of the array; after the second pass, the two highest numbers are “in place”, and so on. Instead
of making five comparisons on every pass (as will be the case with the array profitmargins[]), the
bubble sort implemented here should make four comparisons on the second pass, three on the third
pass, and so on.
void b u b b l e S o r t ( double a r r [ ] , int s i z e )
{
f o r ( int k = 0 ; −−[ i ]−−; k++)
{
f o r ( int j = 0 ; −−[ i i ]−− ; −−[ i i i ]−−)
{
if −−[ i v ]−−
{
double temp = a r r [ j + 1 ] ;
arr [ j + 1] = arr [ j ] ;
a r r [ j ] = temp ;
}
}
}
}
Write ONLY the missing code that should be placed where each of the numbers appear in the above
listing
(d) Write a function findcharacter(const char array[][COLS], int n, char item) that locates a given (6)
character, for example a health check point, in the map. The function should search only the nth row
(e.g. for the second row, a value of 2 will be passed) of the 2D array that is stored in the delivery map.
The function locates the character (item) the delivery system wishes to find and if the searched character
appears in the nth row the function should return the column index of where the character was found else
return -1 if searched item is not found.
Page 7