25-Comp-A4 Program Design and Data Structures · December 2014
Nivaar worked solution (AI-drafted; not reviewed by a licensed engineer)
Paper format. 98-Comp-A4 Program Design and Data Structures, December 2014 — 3 hours, closed book, no calculator permitted. Nine questions, each of equal weight (20 marks); 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, 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. Two file names supplied on standard input; a definition of similarity that ignores blanks, tabs and newlines entirely; an explicit warning that the files may differ in size; and a requirement to answer false at the earliest possible moment.
Find. A program that prints true when the two files agree character for character after all white space is removed, and false otherwise, stopping at the first disagreement.
Approach. Read both files as streams of significant characters, skipping white space in a helper, and compare one significant character from each per iteration; any mismatch — including a character against end of file — ends the comparison immediately.
isspace from
<ctype.h> already covers blanks, tabs and newlines, and also
carriage returns and form feeds, which is the behaviour wanted when files come
from different operating systems.a != b catches that case with
no extra code. The files agree only if both reach EOF on the same iteration.
#include <stdio.h>
#include <ctype.h>
/* Return the next character of f that is not white space, or EOF. */
static int next_significant(FILE *f)
{
int c;
while ((c = fgetc(f)) != EOF && isspace(c))
; /* blanks, tabs and newlines are skipped */
return c;
}
int main(void)
{
char name1[FILENAME_MAX], name2[FILENAME_MAX];
FILE *f1, *f2;
int similar = 1;
/* The two file names are read from standard input. */
if (scanf("%s %s", name1, name2) != 2) {
fprintf(stderr, "expected two file names\n");
return 2;
}
f1 = fopen(name1, "r");
f2 = fopen(name2, "r");
if (f1 == NULL || f2 == NULL) {
perror("fopen");
if (f1) fclose(f1);
if (f2) fclose(f2);
return 2;
}
for (;;) {
int a = next_significant(f1);
int b = next_significant(f2);
if (a != b) { /* first difference: stop at once */
similar = 0;
break;
}
if (a == EOF) /* both streams ended together */
break;
}
fclose(f1);
fclose(f2);
printf(similar ? "true\n" : "false\n");
return similar ? 0 : 1;
}helloworld; “hello world” against “hello
world!” is not, and is rejected when one stream yields
! and the other EOF; and an empty file is highly similar to a file
of nothing but white space, since both filter to the empty sequence. Overall
cost is $O(n)$ time and $O(1)$ space.| Case | File 1 | File 2 | Output |
|---|---|---|---|
| Same text, different line breaks | hello world | hello / world | true |
| Same text, different spacing and tabs | a b c | abc | true |
| Extra punctuation | hello world | hello world! | false |
| Empty against all white space | (empty) | spaces and newlines only | true |
| One a prefix of the other | abc | abcd | false |
| Time / space | $O(n)$ time, stopping at the first mismatch | $O(1)$ space | |