25-Comp-A4 Program Design and Data Structures · December 2017
Nivaar worked solution (AI-drafted; not reviewed by a licensed engineer)
Paper format. 98-Comp-A4 Program Design and Data Structures, December 2017 — 3 hours, closed book, no calculator permitted. Nine questions of equal weight (20 marks each: 1 and 7 split as (a) 10 + (b) 10, 8 split as (a) 15 + (b) 5); 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.
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. Starting integer $n_0>0$; rule $n_{k+1}=3n_k+1$ (odd) or $n_{k+1}=n_k/2$ (even); worked example $n_0=10\Rightarrow$ 6 steps, largest value 16.
Find. A program reporting the step count to reach the repeating ground state $4,2,1,4,2,1,\ldots$ and the largest value visited.
Approach. Apply the rule repeatedly, counting one step per application and tracking the running maximum, stopping the first time the current value reaches 1 (the ground state $4,2,1$ then repeats forever from there, so reaching 1 marks the end of the "climbing" part of the sequence — this matches the worked example exactly).
#include <stdio.h>
int main(void)
{
long n, largest, steps = 0;
printf("Enter a starting number: ");
scanf("%ld", &n);
largest = n;
while (n != 1) {
if (n % 2 == 1)
n = 3 * n + 1;
else
n = n / 2;
steps++;
if (n > largest) largest = n;
}
printf("Steps to reach the ground state: %ld\n", steps);
printf("Largest number reached: %ld\n", largest);
return 0;
}
| Starting number | Steps to ground state | Largest value reached |
|---|---|---|
| 10 | 6 | 16 |
| 1 (already at ground state) | 0 | 1 |
| 27 (illustrative, long-running case) | 111 | 9232 |