UNIVERSITY OF ESSEX
Undergraduate Examinations 2020
C++ PROGRAMMING
Time allowed: TWO hours (exam time) + ONE hour to allow for submission time (total THREE hours)
Copyright By PowCoder代写 加微信 powcoder
(Please see your exam timetable or check on FASER for the deadline to upload your answers)
The times shown on your timetable are in British Summer Time (BST) (GMT+1). Please check online for a conversion to your local time if you will be undertaking your assessment outside the United Kingdom
Candidates are permitted to use:
Calculator – Casio FX-83GT PLUS/X or Casio FX-85GT PLUS/X only
Candidates must answer ALL questions.
The paper consists of FOUR questions.
The questions are NOT of equal weight.
The percentages shown in brackets provide an indication of the proportion of the total marks for the PAPER which will be allocated.
If you have a query with the content of this exam paper please use the revision FAQ Forum on the module’s Moodle page. Your academic will be available to answer any queries in real-time.
If you have a technical problem with FASER, or any other query, please go to Exams Website to find contact details of the teams that can help you.
Please note that the time allocated for this assessment includes time for you to download this question paper and answer paper and to upload your answers to FASER.
Please allow at least 30 minutes at the end of your exam time to upload your work. Once you have completed the assessment do not leave it to the last minute to upload.
Please save your work throughout the examination to avoid losing your work.
Please do not communicate with any other candidate in any way during this assessment. Your response must be your own work. Procedures are in place to detect plagiarism and collusion.
CE221-5-AU
CE221-5-AU 2
Question 1
(a) Rewrite the following C++ class in two files Complex.h and Complex.cpp such that [9%] all of the members of the class are declared in the header file but the complete member function definitions are written in the .cpp file. Use comments to indicate clearly which
file is which. Appropriate header files should be included.
class Complex
{ private:
double real, imag;
Complex(double r, double i)
{ real = r;
imag = i; }
Complex& operator+=(const Complex &c)
{ real += c.real;
imag += c.imag;
return *this;
friend ostream &operator<<(ostream&, const Complex&);
(b) Rewrite the constructor for the Complex class using an initialiser list. [2%]
(c) Show how it is possible to modify the header file so that initialisations of the form [2%] Complex c1(3.5,2.5) and Complex c2(4.5)would both be permitted. (The
latter should initialise the imag member to be 0.0.) You must not introduce a second constructor.
(d) Provide an implementation of the operator<< function, which should generate output [5%] of the form 3.5+4.4i, where the first number is the value of the real member and the
second number is the value of the imag member.
(e) Briefly explain why the operator<< function could not be written as a member of the [2%] class.
(f) Add to the class an operator+ function to allow expressions of the form c1+c2 to be [7%] used, where c1 and c2 are expressions of type Complex. Its implementation should
make use of the += operator to add the value of c2 to a copy of c1, and then return the
copy. Show the modifications needed to both the header file and the .cpp file.
Question 2
while (*q = *p)
{ cout << *q << ' ';
cout << endl;
// note: = not ==
(c) bool b1 = 0, b2 = 20;
cout << b1 << ';' << b2 << endl;
3 CE221-5-AU
State what would be output by each of the following C++ program fragments; you should assume that each fragment is inside a function body within a complete program and that all appropriate header files have been included.
(a) int i = 0, j = 5, a[] = { 1, 2, 3, 4, 5, 6 }; [4%] while (i<4)
cout << a[i++] << ', ' << a[--j] << endl;
(b) int x[] = {3, 2, 1, 0, 4 }, y[5] = { 0 }; [4%] int *p = x, *q = y ;
(d) vector
v.push_back(3);
v.push_back(4);
sort(v.rbegin(), v.rend());
cout << v[2] << ' ' << v[1] << ' ' << v[0] << endl;
(e) list
ll.push_front(2);
ll.push_back(3);
ll.push_front(1);
list
for (it = ++ll.rbegin(); it != ll.rend(); it++)
cout << *it << ' ';
cout << endl;
(f) map
age[“Bart”] = 15;
age[“Marge”] = 45;
age[“Lisa”] = 12;
map
for (it = age.begin(); it->second<20; it++)
cout << it->first << ' ' << it->second << endl;
CE221-5-AU 4
Question 3
Explain the difference between the use of the stack and the heap for storage of data [5%] and how this relates to the lifetime of data items.
(ii) Explain how the contents of the stack and the heap would be affected by the [2%] following declaration. (You should assume that it occurs inside a function definition.)
int *ip = new int[20];
(b) Explain what is meant by a copy constructor and an assignment operator for a class and [10%] describe in what circumstances they are invoked. Show how each of these would be
declared for a class C. (It is not necessary to provide function bodies.)
(c) If the following program fragment was run the copy constructor for the class C would be invoked three times and the assignment operator once. State precisely each of the four situations in which they would be used, indicating clearly which is being invoked.
// the following function declarations are assumed
// C() // a no-argument constructor
// C getinput(istream &i);
// int f1(const C&);
// void f2(C);
int x = f1(getinput(cin));
Question 4
The following is the contents of a header file for a class to store information about lecturers.
#ifndef _LECT_H_
#define _LECT_H_
using namespace std;
#include
#include
class Lecturer
{ private:
string name, department, office;
Lecturer(string name, string dept, string off);
bool operator<(const Lecturer &other) const;
void print(ostream &str) const;
5 CE221-5-AU
(a) Write a .cpp file for this class which should include complete definitions for the constructor and operator< and print functions. The < operator should compare lecturers using their names; the print function should output all of the details of an individual lecturer in fixed-width fields on a single line (terminated by a newline character).
The answers to parts (b), (c) and (d) should be written as part of a single main function. You should assume that a function Lecturer inputLect(istream &str); has been written to input details of a lecturer and return a lecturer object containing those details.
(b) Use a loop to make calls to inputLect to input details of 25 lecturers from the keyboard and store the results of the calls in a vector, additionally maintaining a count of the number of lecturers from each department in a map. (The STL vector and map classes should be used for the list and map.)
(c) Sort the vector by name and print its contents one item per line (using the print member function).
(d) Print the names of all departments with at least 5 lecturers, along with their numbers of lecturers, one per line.
CE221-5-AU 6
END OF PAPER CE221-5-AU
Once you have completed your answers, please upload them to FASER
http://faser.essex.ac.uk.
Remember to add your REGISTRATION NUMBER onto ALL documents that you upload.
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com