25-Comp-A4 Program Design and Data Structures · December 2019
Nivaar worked solution (AI-drafted; not reviewed by a licensed engineer)
Paper format. 17-Comp-A4 Program Design and Data Structures, December 2019 — 3 hours, closed book, no calculator permitted. Nine questions, each of equal weight (Questions 1, 2, 7 and 8 are split 10+10; Questions 3–6 and 9 are 20 marks each), so 180 marks are printed in total. The cover page directs candidates to answer any six of the nine, and only the first six as they appear in the answer book are marked — so a complete paper is $6\times 20 = 120$ marks, which is the "total mark is out of 120" the paper's Note 6 states. 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 text file whose lines hold at most 256 characters each, and whose word boundaries are exactly four delimiter kinds: blank, comma, period, and line start/end.
Find. The total number of lines, and the number of words in each individual line.
Approach. Read the file one line at a time; for each line, scan character by character maintaining an "in a word" flag that starts a new word count whenever a non-delimiter follows a delimiter (or the start of line), so each line's word count falls out of a single forward scan with no need to store the words themselves.
#include <stdio.h>
#define MAXLINE 258 /* 256 chars + '\n' + '\0', per the stated line bound */
int is_delim(int c) { return c == ' ' || c == ',' || c == '.'; }
int count_words(const char *line)
{
int count = 0, in_word = 0;
for (; *line != '\0' && *line != '\n'; line++) {
if (is_delim((unsigned char) *line)) {
in_word = 0;
} else if (!in_word) {
in_word = 1;
count++;
}
}
return count;
}
int main(void)
{
char filename[256], line[MAXLINE];
FILE *fp;
long nlines = 0;
printf("Enter file name: ");
if (scanf("%255s", filename) != 1) return 1;
fp = fopen(filename, "r");
if (fp == NULL) { printf("Could not open %s\n", filename); return 1; }
while (fgets(line, MAXLINE, fp) != NULL) {
nlines++;
printf("Line %ld: %d word(s)\n", nlines, count_words(line));
}
printf("Total lines: %ld\n", nlines);
fclose(fp);
return 0;
}"Hello, world. This is fun.", "One more line here",
"Last one.". Line 1 has words Hello / world / This / is / fun
(the comma and period act as delimiters exactly like a blank, so "Hello,"
splits into "Hello" plus a delimiter, not a 6-character token) — 5
words. Line 2 has 4 words (no punctuation, blanks only). Line 3 has 2 words
(Last, one).
$$\boxed{\text{lines}=3,\ \text{words per line}=(5,4,2)}$$| Line | Text | Word count |
|---|---|---|
| 1 | "Hello, world. This is fun." | 5 |
| 2 | "One more line here" | 4 |
| 3 | "Last one." | 2 |
| Total lines | 3 | |