NivaarExam PrepOfficial exam papers ↗

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

Question 1 of 9: Programming — Extension-Cord Selection and ISBN Validation

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 1: Programming — Extension-Cord Selection and ISBN Validation (20 marks: (a) 10, (b) 10)

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. (a) Six standard cord ratings {3.0, 6.0, 7.0, 10.0, 13.0, 15.0} A and the rule linking cord length to the amperage headroom required. (b) The nine-digit-plus-check-character ISBN-10 structure and its positional-weighted-sum check formula.

Find. (a) A program that reads an appliance amperage and cord length and prints the correct cord rating, or refuses lengths over 100 ft. (b) A program that reads a 10-character ISBN and reports whether its check character is correct.

Approach. (a) First find the smallest listed rating that is ≥ the appliance's amperage (the "same or higher" rule for ≤50 ft), then, only for the 50–100 ft band, step to the next rating above that minimum. (b) Compute the weighted digit sum directly from the array positions and compare the derived check symbol against the tenth character.

(a) Extension-cord selection (10 marks)

  1. Separate "which rating suffices" from "which tier the length demands." The minimum sufficient rating is always the smallest listed value ≥ the appliance amperage — that alone answers a ≤50 ft run. A 50–100 ft run then needs one tier of headroom above that minimum, and anything over 100 ft is refused outright regardless of amperage.
  2. Write the program.
    #include <stdio.h>
    
    static const double RATINGS[] = {3.0, 6.0, 7.0, 10.0, 13.0, 15.0};
    #define N_RATINGS 6
    
    int main(void)
    {
        double amperage, length;
        int i, min_idx;
    
        printf("Enter appliance amperage: ");
        scanf("%lf", &amperage);
        printf("Enter cord length in feet: ");
        scanf("%lf", &length);
    
        if (length > 100.0) {
            printf("No cord of length greater than 100 feet should be used.\n");
            return 0;
        }
    
        /* smallest rating that is >= the appliance's amperage */
        min_idx = -1;
        for (i = 0; i < N_RATINGS; i++) {
            if (RATINGS[i] >= amperage) { min_idx = i; break; }
        }
        if (min_idx == -1) {
            printf("No cord rating is high enough for this appliance.\n");
            return 0;
        }
    
        if (length <= 50.0) {
            printf("Use a cord rated at %.0f amperes or more.\n", RATINGS[min_idx]);
        } else {                                   /* 50 < length <= 100 */
            if (min_idx + 1 >= N_RATINGS) {
                printf("No cord rating is high enough for this length.\n");
            } else {
                printf("Use a cord rated at %.0f amperes or more.\n", RATINGS[min_idx + 1]);
            }
        }
        return 0;
    }
  3. Confirm the worked example and the boundary cases. For amperage 5.8 and length 60: the minimum sufficient rating is 6 A (smallest ≥5.8), and 60 falls in the 50–100 band, so the program steps up one tier to 7 A — matching “Use a cord rated at 7 amperes or more.” exactly. At the same amperage but length 40 (≤50 ft) the answer stays at 6 A; at 13.0 A/60 ft the next tier above 13 is 15; at 15.0 A/60 ft there is no tier above the top rating, so the program reports that no rating is high enough — the closed-form table lookup has no gaps to patch. $$\boxed{\text{select}(5.8\text{ A},\,60\text{ ft})=7\text{ A}}$$

(b) ISBN check-character validation (10 marks)

  1. Read the 9 leading digits and the check character separately. The dashes are formatting only; strip them while scanning, keep the first nine numeric digits in an array, and keep the tenth (possibly 'X') character on its own for comparison.
  2. Write the program.
    #include <stdio.h>
    #include <ctype.h>
    
    int main(void)
    {
        char raw[32];
        int digits[9], n = 0;
        char check = 0;
        int i, sum = 0, remainder;
        char computed;
    
        printf("Enter ISBN: ");
        fgets(raw, sizeof raw, stdin);
    
        for (i = 0; raw[i] != '\0' && raw[i] != '\n'; i++) {
            if (raw[i] == '-') continue;
            if (n < 9 && isdigit((unsigned char)raw[i])) {
                digits[n++] = raw[i] - '0';
            } else if (n == 9) {
                check = raw[i];                 /* tenth character: digit or 'X' */
            }
        }
        if (n != 9 || check == 0) {
            printf("Invalid ISBN: wrong length.\n");
            return 0;
        }
    
        for (i = 0; i < 9; i++) sum += (i + 1) * digits[i];
        remainder = sum % 11;
        computed = (remainder == 10) ? 'X' : (char)('0' + remainder);
    
        if (computed == check)
            printf("%s is a valid ISBN.\n", raw);
        else
            printf("%s is NOT a valid ISBN (expected check character %c).\n", raw, computed);
        return 0;
    }
  3. Confirm against the worked example. For 0-670-82162-4 the digit array is {0,6,7,0,8,2,1,6,2}; the weighted sum is $1(0)+2(6)+3(7)+4(0)+5(8)+6(2)+7(1)+8(6)+9(2)=158$, matching the question's own stated sum, and $158 \bmod 11 = 4$, matching the printed check character. $$\boxed{\text{sum}=158,\ \ \text{check}=4}$$
Question 1 — results
CaseResult
Amperage 5.8 A, length 60 ftUse a cord rated at 7 A or more
Amperage 5.8 A, length 40 ftUse a cord rated at 6 A or more
Amperage 15.0 A, length 60 ftNo rating high enough (top tier already selected)
ISBN 0-670-82162-4, weighted sum158
ISBN 0-670-82162-4, check character4 (valid)
← Paper overview