Computer Science 1B 2021 Exam Memo - CSC01B1-2021-EXAM-MEMO
Computer Science 1B 2021 Exam Memo - CSC01B1-2021-EXAM-MEMO
MODULE CSC01B1
Introduction to data structures (C++)
CAMPUS APK
/2…
COMPUTER SCIENCE 1B CSC01B1 -2-
QUESTION 1
Use UML to model the following scenario: A Weather Predictor has a real valued
computer time budget and a volume (made up of three integers) which are set
when the predictor is created. In addition, it has a three-dimensional collection
of Air Cells whose life cycle it manages. Each Air Cell contains a set of integer
x, y, and z values which indicate its position within a three-dimensional space as
well as real values indicating the temperature, pressure, and humidity within that
cell. These values need to be accessible and modifiable. Both the Weather
Predictor and Air Cells have a simulate behaviour (the predictor delegates this
to its cells). There are two kinds of predictor namely Long Term and Short Term
which override the simulate behaviour.
1.1 (5)
A class often has multiple constructors which differ in terms of their parameter lists
(in number, order, and type of parameters). What is this an example of?
1.2
A B C D E
inheritance composition aggregation overloading abstracting
Assume that A has a B. Whenever an A is instantiated so too is B. Whenever A is
destroyed so too is B. What kind of relationship is this?
1.3
A B C D E
composition aggregation inheritance polymorphism generics
Assume that class A is an abstract base class and that B derives from A. B is also
an abstract base class given that the following code compiles:
1.4 A a = new B;
True False 無1
1
The Sino-Japanese ideogram Wu/Mu in this case represents a question which is flawed.
COMPUTER SCIENCE 1B CSC01B1 -3-
QUESTION 2
Complete the following code in which a generic Q2Cube class acts as a container for
three-dimensional data of type T
a) template
b) public
c) <T>
d) const
2.1 e) Q2Cube<T>& (10)
f) T
g) ***
h) !=
i) freeCube()
j) *this or objRHS
The copyCube() function assumes that the current object is already the same size as the
object that is being copied. If this is not the case, then an error condition has been
encountered. Provide code which defines a simple two class hierarchy consisting of a base
exception type and a specific exception type for this kind of error.
2.2 (2)
class Error {};
class NotEqualToError : public Error {};
Write code to protect the following using exception handling assuming that the class has
been updated to raise the exception types defined in the previous sub-question.
2.3 (3)
try
COMPUTER SCIENCE 1B CSC01B1 -4-
{
//
} catch(NotEqualToError& e)
{
cerr << “!= error” << endl;
} catch (Error& e)
{
cerr << “Error” << endl;
} catch (…)
{
cerr << “???” << endl;
}
Consider the following node-based data structure for the remaining sub-questions:
Provide code for a templated node structure compatible with the depicted data-structure.
template <typename T>
struct Node
{
2.4 (5)
T data;
Node<T>* next;
};
Provide code for inserting a node containing the value ‘E’ at the end of the data-structure
Node<char>* nodeNew = new Node<char>;
nodeNew->data = ‘E’;
2.5 (5)
nodeNew->next = nullptr;
_tail->next = nodeNew;
_tail = nodeNew;
Provide code for deleting the entire data structure
Node<T>* nodeCurrent = _head;
while(nodeCurrent != nullptr)
{
2.6 (5)
Node<T>* n = nodeCurrent;
nodeCurrent = nodeCurrent->next;
delete n;
}
[30]
COMPUTER SCIENCE 1B CSC01B1 -5-
QUESTION 3
Write the necessary C++ code for the following statements, and answer the remaining questions.
Unless otherwise indicated you may assume that the necessary header files are included. Most of the
marks in this section are awarded for the file handling operations.
The text file numeric-parameters.txt is made up of lines in the following format:
The data represents the input parameters for a set of virtual simulations. Each line
represents the data for running one instance of the simulation and the location where its
results should be saved. There can be an arbitrary number of lines and they can vary in
length. Each value in a line is separated by a space character and has the following
meaning:
• SIM-NAME: The name of the simulation as a string
• VERSION: A positive integer
• P0 … PN: A set of integer parameters for this particular instance of the simulation.
• OUT-FILE: The name of the file the results of the simulation should be saved to.
• EOL: Represents the end of line characters for the operating system
The names of the simulation and the output file are limited to letters, underscored,
numbers, and full stops. In addition, they may not exceed 32 characters each.
Write code which reads these values from the file and uses the system() function to run
3.1 the simulation with its parameter set. The name of the simulation executable is its sim- (5)
name followed by its version number followed by .exe.
ifstream f(“numeric-parameters.txt”);
string strLine = “”;
while(getline(f, strLine))
{
stringstream ss(strLine);
string strEX = “”;
ss >> srEXE;
strVer = “”;
ss >> strVer;
strEXE += strVer + “.exe ”;
string strP = “”;
while(ss >> strP)
{
strEXE += “strP “;
}
system(strEXE.c_str());
}
f.close()
Define a structure with alignment packing/padding disabled to represent the data for one
run of a simulation.
#pragma pack(push)
#pragma pack(1)
struct SimRun
3.2 { (5)
char name[32];
int version;
int params[16];
};
#pragma pack(pop)
Assume that the file simulation-parameters.dat is a pre-existing binary file of
homogenous records of the type defined in question 3.2. Write code for a function which
3.3 (5)
reads the last record from the end of the file.
fstream f(“simulation-parameters.dat, ios::binary | ios::in);
COMPUTER SCIENCE 1B CSC01B1 -6-
f.seekg(0, ios::end);
SimRun recRun;
f.read(reinterpret_cast<char*>(&recRun), sizeof(SimRun));
f.close();
Assume that the file simulation-parameters.dat is a pre-existing binary file of
homogenous records of the type defined in question 3.2. Write code for a function which
determines if there are an even or odd number of records in the file.
fstream f(“simulation-parameters.dat, ios::binary | ios::in);
3.4 (5)
f.seekg(0, ios::end);
int intBytes = f.tellg();
bool isEven = (intBytes / sizeof(SimRun) % 2) == 0;
f.close();
Analyse the provided code asymptotically using Big-O notation. State any assumptions
made.
3.5 (10)
Assumptions (4)
Derivation (5)
Result (1)
.
[30]
QUESTION 4
Your software expects a dynamic array of integer values to be monotonically increasing (that
is each subsequent element is either the same as the previous or larger, never smaller). You
will need to create a DLL to contain this such a
bool isIncreasing(int* aryData, int intSize) function in a dynamically linked
library called monotone-increase.dll
Write code for the header file of such a DLL
#ifndef EXAMPLE_DLL_H (1)
#define EXAMPLE_DLL_H (1)
#endif (1)
Write code for the .cpp implementation file of the DLL including the
implementation of the isIncreasing function
4.2 • Normal library function (5)
• Loop through array from 1 up to n – 1
o Compare a[i] with a[i – 1]
COMPUTER SCIENCE 1B CSC01B1 -7-
o Check if < or ==
• Return
You have code for a program which uses this functionality in a file called
process-main.cpp. Write a BAT file which compiles an implicitly linked
executable called process.exe which expects to link to the DLL which will be
4.3 stored in the lib folder (5)
g++ process-main.cpp -o process.exe -lmonotone-increase -L\lib