25-Comp-A4 Program Design and Data Structures · December 2016
Nivaar worked solution (AI-drafted; not reviewed by a licensed engineer)
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.
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 template text file containing zero or more
occurrences of the literal marker #N#, and a recipient name typed
at the keyboard.
Find. A copy of the template, written to a second file,
with every occurrence of #N# replaced by the typed name.
Approach. Stream the template character by character;
whenever the next three characters match #N# exactly, write the
name instead and skip past all three marker characters, otherwise copy the
character through unchanged.
ungetc, so no separate buffering scheme is
needed) lets the marker be recognised without reading the whole file into
memory.
#include <stdio.h>
#include <string.h>
int main(void)
{
FILE *in, *out;
char name[80];
int c1, c2, c3;
in = fopen("letter_template.txt", "r");
out = fopen("letter_out.txt", "w");
printf("Enter recipient's first name: ");
fgets(name, sizeof name, stdin);
name[strcspn(name, "\n")] = '\0'; /* strip trailing newline */
c1 = fgetc(in);
while (c1 != EOF) {
if (c1 == '#') {
c2 = fgetc(in);
if (c2 == 'N') {
c3 = fgetc(in);
if (c3 == '#') {
fputs(name, out); /* marker matched: substitute */
c1 = fgetc(in);
continue;
}
fputc(c1, out); fputc(c2, out);
if (c3 != EOF) ungetc(c3, in);
c1 = fgetc(in);
continue;
}
fputc(c1, out);
if (c2 != EOF) ungetc(c2, in);
c1 = fgetc(in);
continue;
}
fputc(c1, out);
c1 = fgetc(in);
}
fclose(in);
fclose(out);
return 0;
}"Dear #N#, welcome #N#!" with name Chris streams
through unchanged up to the first #, matches the full
#N# marker there, substitutes Chris, continues
copying ", welcome " unchanged, matches the marker a second
time, and substitutes again — confirming the "may occur more than once"
requirement is met without any assumption that there is exactly one marker.
$$\boxed{\text{"Dear \#N\#, welcome \#N\#!"} \to \text{"Dear Chris, welcome Chris!"}}$$| Template | Output (name = "Chris") |
|---|---|
| Dear #N#, welcome #N#! | Dear Chris, welcome Chris! |