NivaarExam PrepOfficial exam papers ↗

25-Comp-A4 Program Design and Data Structures · December 2016

Question 3 of 9: Programming — VLSI Wire Intersection Detection

Nivaar worked solution (AI-drafted; not reviewed by a licensed engineer)

Notes on this paper

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.

  • Cormen, Leiserson, Rivest & Stein, Introduction to Algorithms, 4th ed. — tree traversals and BSTs (ch. 12), sorting (ch. 2, 7), asymptotic analysis (ch. 3).
  • Weiss, Data Structures and Algorithm Analysis in C, 2nd ed. — linked lists and queues (ch. 3), binary search trees (ch. 4).
  • Deitel & Deitel, C++ How to Program, 10th ed. — class design and templates (ch. 9–12), file streams (ch. 14), operator overloading (ch. 11).
  • Kernighan & Ritchie, The C Programming Language, 2nd ed. — character/array I/O idioms (ch. 1, 7), pointers, structures and linked lists (ch. 5–6).
  • Stroustrup, The C++ Programming Language, 4th ed. — class templates and value semantics (ch. 3, 25–27).

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 3: Programming — VLSI Wire Intersection Detection (20 marks)

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.

  1. Read all 20 wires into two arrays, split by orientation. Following the hint, classify each wire as it is read ($y_1=y_2 \Rightarrow$ horizontal, $x_1=x_2 \Rightarrow$ vertical) into a horizontal-wire array and a vertical-wire array, normalising each into (fixed coordinate, span-low, span-high) so the intersection test needs no further min/max work.
  2. Write the program.
    #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;
    }
  3. Confirm against the question's own two example wires. The paper's (6,8:3,8) horizontal wire spans $x\in[3,6]$ at $y=8$; its (2,4:2,6) vertical wire is at $x=2$, spanning $y\in[4,6]$. Since $x=2$ does not fall in $[3,6]$, the program correctly reports no intersection for that pair — a vertical wire at $x=5$ over the same $y$-span, by contrast, does fall in $[3,6]$ and at $y=8$ (outside $[4,6]$) still would not cross, while one spanning $y\in[6,10]$ would. $$\boxed{\text{intersects}\big((6,8{:}3,8),\,(2,4{:}2,6)\big)=\text{false}}$$
Question 3 — results
PairIntersect?
(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]