NivaarExam PrepOfficial exam papers ↗

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

Question 2 of 9: Programming — Self-Describing Integers

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

Notes on this paper

Paper format. 17-Comp-A4 Program Design and Data Structures, December 2018 — 3 hours, closed book, no calculator permitted. Nine questions, each of equal weight (Question 1 is split 10+10; Questions 2–9 are 20 marks each); candidates answer any six, and only the first six as they appear in the answer book are marked, so the paper is marked out of 120. 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.

  • Cormen, Leiserson, Rivest & Stein, Introduction to Algorithms, 4th ed. — tree walks (ch. 12), stacks and linear-time scans (ch. 10, 2), asymptotic analysis (ch. 3).
  • Weiss, Data Structures and Algorithm Analysis in C, 2nd ed. — arrays and dynamic 2-D allocation (ch. 1), linked lists (ch. 3), stacks (ch. 3.3), binary trees (ch. 4).
  • Deitel & Deitel, C++ How to Program, 10th ed. — class design, templates and the Rule of Three (ch. 9–12), file streams (ch. 14).
  • Kernighan & Ritchie, The C Programming Language, 2nd ed. — character/file I/O idioms (ch. 1, 7), pointers and structures (ch. 5–6).
  • Stroustrup, The C++ Programming Language, 4th ed. — value semantics, const-correctness and templates (ch. 3, 16–18).

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 2: Programming — Self-Describing Integers (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. A positive integer, entered as a sequence of decimal digits (positions labelled 0 to $n-1$ left to right).

Find. Whether, for every position $p$, the digit stored there equals the count of how many array entries equal $p$.

Approach. Read the number as a string into a digit array (the hint), then for every position count occurrences of that position's own index value across the whole array and compare against the stored digit; any single mismatch rejects the number.

  1. State the check as one condition per position. $$\text{self-describing} \iff \forall p\in\{0,\dots,n-1\}:\ \text{digit}[p] = \big|\{\,i : \text{digit}[i]=p\,\}\big|$$ This is a closed condition on the array against itself — no external data is needed, only two nested scans.
    #include <stdio.h>
    #include <string.h>
    
    #define MAXLEN 16
    
    int is_self_describing(int digits[], int len)
    {
        int pos, i, count;
        for (pos = 0; pos < len; pos++) {
            count = 0;
            for (i = 0; i < len; i++)
                if (digits[i] == pos) count++;
            if (count != digits[pos])
                return 0;                 /* fails at this position */
        }
        return 1;
    }
    
    int main(void)
    {
        char buf[MAXLEN + 1];
        int digits[MAXLEN], len, i;
    
        printf("Enter a positive integer: ");
        if (scanf("%16s", buf) != 1) return 1;
    
        len = (int) strlen(buf);
        for (i = 0; i < len; i++)
            digits[i] = buf[i] - '0';
    
        if (is_self_describing(digits, len))
            printf("%s is self-describing.\n", buf);
        else
            printf("%s is NOT self-describing.\n", buf);
        return 0;
    }
  2. Trace the paper's own examples. For 2020 (positions 0,1,2,3 hold digits 2,0,2,0): position 0's value 2 must equal the count of 0's among the digits — digits are 2,0,2,0, and there are indeed two 0's; position 1's value 0 must equal the count of 1's — there are none; position 2's value 2 must equal the count of 2's — there are two; position 3's value 0 must equal the count of 3's — there are none. All four positions check out. $$\boxed{\text{is\_self\_describing}(2020) = \text{true}}$$ 1210 and 3211000 verify the same way, and a number such as 1234 fails immediately at position 0: it claims one 0, but 1234 contains none.
Question 2 — results
InputSelf-describing?Reason
2020Yespos0=2 (two 0's), pos1=0 (no 1's), pos2=2 (two 2's), pos3=0 (no 3's)
1210Yespos0=1 (one 0), pos1=2 (two 1's), pos2=1 (one 2), pos3=0 (no 3's)
3211000Yes7-digit family member: pos0=3, pos1=2, pos2=1, pos3…6=1,0,0,0
1234Nopos0 claims one 0, but 1234 has zero 0's