25-Comp-A4 Program Design and Data Structures · December 2019
Nivaar worked solution (AI-drafted; not reviewed by a licensed engineer)
Paper format. 17-Comp-A4 Program Design and Data Structures, December 2019 — 3 hours, closed book, no calculator permitted. Nine questions, each of equal weight (Questions 1, 2, 7 and 8 are split 10+10; Questions 3–6 and 9 are 20 marks each), so 180 marks are printed in total. The cover page directs candidates to answer any six of the nine, and only the first six as they appear in the answer book are marked — so a complete paper is $6\times 20 = 120$ marks, which is the "total mark is out of 120" the paper's Note 6 states. Pseudocode or any high-level language (e.g. C or C++) is accepted, and the examiner's note states explicitly that marking emphasises the operation of the program, not syntactic details. All nine questions are answered below, because the whole set is the more useful revision resource. Answers are given in C or C++ as the question dictates; each is compilable as written, but a clear, correctly reasoned pseudocode answer would earn the same marks.
Reference texts for this subject.
The Computer Engineering citation list is built around architecture and networking texts (Patterson & Hennessy, Tanenbaum, Mano); this subject is programming and data structures, so the works above are cited instead.
Question text not reproduced: the examination questions are © Engineers and Geoscientists BC. Open the official past paper (linked at the top of this page) to read the question, then follow the worked solution below.
Given. No numeric data; a design requirement for a class that can be declared both with and without initial real/imaginary values, gives read/write access to each part, and supports $+,-,\times$ plus printing.
Find. A Complex class, split into
Complex.h (declaration) and Complex.cc
(implementation), whose arithmetic operators implement the standard complex
number formulas.
Approach. Store the real and imaginary parts as two
double members; provide a single constructor whose two default
arguments $(0,0)$ make it serve as both the no-argument declaration
Complex c; and the initialised one Complex c(3,4);
— which is exactly the "with and without initialization" requirement
— then accessor/mutator pairs for each part, overload +, -
and * as member functions returning a new Complex
by value, and overload << as a free function so
cout << c reads naturally.
// Complex.h
#ifndef COMPLEX_H
#define COMPLEX_H
#include <iostream>
class Complex {
public:
Complex(double re = 0.0, double im = 0.0);
double getReal() const;
double getImag() const;
void setReal(double re);
void setImag(double im);
Complex operator+(const Complex &other) const;
Complex operator-(const Complex &other) const;
Complex operator*(const Complex &other) const;
friend std::ostream &operator<<(std::ostream &os, const Complex &c);
private:
double real, imag;
};
#endif// Complex.cc
#include "Complex.h"
Complex::Complex(double re, double im) : real(re), imag(im) {}
double Complex::getReal() const { return real; }
double Complex::getImag() const { return imag; }
void Complex::setReal(double re) { real = re; }
void Complex::setImag(double im) { imag = im; }
Complex Complex::operator+(const Complex &o) const
{
return Complex(real + o.real, imag + o.imag);
}
Complex Complex::operator-(const Complex &o) const
{
return Complex(real - o.real, imag - o.imag);
}
Complex Complex::operator*(const Complex &o) const
{
return Complex(real * o.real - imag * o.imag,
real * o.imag + imag * o.real);
}
std::ostream &operator<<(std::ostream &os, const Complex &c)
{
os << c.real;
if (c.imag >= 0) os << " + " << c.imag << "i";
else os << " - " << -c.imag << "i";
return os;
}| Operation | $c_1=3+4i,\ c_2=1-2i$ | Result |
|---|---|---|
| $c_1+c_2$ | $(3{+}1)+(4{-}2)i$ | $4+2i$ |
| $c_1-c_2$ | $(3{-}1)+(4{+}2)i$ | $2+6i$ |
| $c_1\times c_2$ | $(3\cdot1{-}4\cdot(-2))+(3\cdot(-2){+}4\cdot1)i$ | $11-2i$ |