25-Comp-A4 Program Design and Data Structures · December 2018
Nivaar worked solution (AI-drafted; not reviewed by a licensed engineer)
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.
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. 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.
#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;
}| 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) |
| 1210 | Yes | pos0=1 (one 0), pos1=2 (two 1's), pos2=1 (one 2), pos3=0 (no 3's) |
| 3211000 | Yes | 7-digit family member: pos0=3, pos1=2, pos2=1, pos3…6=1,0,0,0 |
| 1234 | No | pos0 claims one 0, but 1234 has zero 0's |