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) · Last updated
Notes on this paper
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.
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.
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.
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;
}
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
Input
Self-describing?
Reason
2020
Yes
pos0=2 (two 0's), pos1=0 (no 1's), pos2=2 (two 2's), pos3=0 (no 3's)