25-Comp-A4 Program Design and Data Structures · December 2016
Nivaar worked solution (AI-drafted; not reviewed by a licensed engineer)
Paper format. 98-Comp-A4 Program Design and Data Structures, December 2016 — 3 hours, closed book, no calculator permitted. Nine questions of equal weight (20 marks each: 1, 2 and 9 split as (a) 10 + (b) 10); candidates answer any six, so a complete paper is 120 marks. Pseudocode or any high-level language 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 (or corrected where the printed paper itself has a slip), 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. 20 wires, each purely horizontal ($y_1=y_2$) or purely vertical ($x_1=x_2$), given as endpoint pairs.
Find. Every pair of wires that intersects, printed as their coordinate pairs.
Approach. Classify each wire once by orientation. Two wires of the same orientation are parallel and run-track spacing in real layouts keeps them from touching, so this check only tests each horizontal/vertical pair: a horizontal wire at $y=y_h$ spanning $[x_{\min},x_{\max}]$ crosses a vertical wire at $x=x_v$ spanning $[y_{\min},y_{\max}]$ exactly when $x_v$ falls inside the horizontal wire's $x$-span and $y_h$ falls inside the vertical wire's $y$-span.
#include <stdio.h>
typedef struct { double fixed, lo, hi; } Seg; /* horizontal: fixed=y; vertical: fixed=x */
#define MAXW 20
int main(void)
{
Seg h[MAXW], v[MAXW];
int nh = 0, nv = 0, i, j;
double x1, y1, x2, y2;
for (i = 0; i < MAXW; i++) {
scanf("%lf %lf %lf %lf", &x1, &y1, &x2, &y2);
if (y1 == y2) { /* horizontal */
h[nh].fixed = y1;
h[nh].lo = (x1 < x2) ? x1 : x2;
h[nh].hi = (x1 < x2) ? x2 : x1;
nh++;
} else { /* vertical */
v[nv].fixed = x1;
v[nv].lo = (y1 < y2) ? y1 : y2;
v[nv].hi = (y1 < y2) ? y2 : y1;
nv++;
}
}
for (i = 0; i < nh; i++) {
for (j = 0; j < nv; j++) {
if (v[j].fixed >= h[i].lo && v[j].fixed <= h[i].hi &&
h[i].fixed >= v[j].lo && h[i].fixed <= v[j].hi) {
printf("Horizontal (y=%.1f, x %.1f..%.1f) intersects "
"Vertical (x=%.1f, y %.1f..%.1f)\n",
h[i].fixed, h[i].lo, h[i].hi,
v[j].fixed, v[j].lo, v[j].hi);
}
}
}
return 0;
}| Pair | Intersect? |
|---|---|
| (6,8:3,8) & (2,4:2,6) | No — vertical wire's x=2 is outside horizontal's [3,6] span |
| (6,8:3,8) & (5,6:5,10) | Yes — x=5 in [3,6] and y=8 in [6,10] |