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) A line of text up to 256 characters, read repeatedly until an empty line. (b) An $n\times n$ integer array with entries in $[1,99]$, stated to contain exactly one saddle point; the worked $6\times 6$ example above.
Find. (a) Each entered line printed in reverse character order. (b) The value and (row, column) location of the array's saddle point.
Approach. (a) Read each line into a fixed char array with
fgets, strip the trailing newline, then print it back
index-by-index from the last character to the first. (b) For every row find
its minimum and remember which column it sits in; a row's minimum is a
saddle point exactly when it is also the largest value anywhere in that
column, so check each row-minimum against a linear scan of its own column.
#include <stdio.h>
#include <string.h>
#define MAXLEN 257 /* 256 chars + '\0' */
int main(void)
{
char line[MAXLEN];
int len, i;
printf("enter a line of text: ");
while (fgets(line, MAXLEN, stdin) != NULL) {
len = (int) strlen(line);
if (len > 0 && line[len - 1] == '\n') line[--len] = '\0';
if (len == 0) break; /* empty line -> stop */
printf("the reversed line is: ");
for (i = len - 1; i >= 0; i--)
putchar(line[i]);
putchar('\n');
printf("enter a line of text: ");
}
return 0;
}Computer programming is fun. $\to$
.nuf si gnimmargorp retupmoC; CN Tower $\to$
rewoT NC; hello $\to$ olleh —
all three match the sample session exactly.#include <stdio.h>
#define MAXN 20
void find_saddle(int a[MAXN][MAXN], int n)
{
int r, c, j, i, min_val, min_col, is_col_max;
for (r = 0; r < n; r++) {
min_val = a[r][0];
min_col = 0;
for (j = 1; j < n; j++)
if (a[r][j] < min_val) { min_val = a[r][j]; min_col = j; }
is_col_max = 1;
for (i = 0; i < n; i++)
if (a[i][min_col] > min_val) { is_col_max = 0; break; }
if (is_col_max) {
printf("Saddle point: value=%d at row=%d, col=%d\n",
min_val, r + 1, min_col + 1); /* 1-indexed, as in the paper */
return;
}
}
printf("No saddle point found.\n");
}| Part | Input | Result |
|---|---|---|
| (a) | "Computer programming is fun." | ".nuf si gnimmargorp retupmoC" |
| (a) | "CN Tower" | "rewoT NC" |
| (a) | "hello" | "olleh" |
| (b) | 6×6 array above | Saddle point $A(3,3)=4$ |