25-Comp-A4 Program Design and Data Structures · December 2018
Nivaar worked solution (AI-drafted; not reviewed by a licensed engineer)
Paper format. 17-Comp-A4 Program Design and Data Structures, December 2018 — 3 hours, closed book, no calculator permitted. Nine questions, each of equal weight (Question 1 is split 10+10; Questions 2–9 are 20 marks each); candidates answer any six, and only the first six as they appear in the answer book are marked, so the paper is marked out of 120. 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, read one character at a time, that may contain round, square and curly brackets interleaved with ordinary text.
Find. Whether every opening bracket is closed, in the reverse order it was opened, before end of file.
Approach. Scan the file once; push every opening bracket onto a stack, and on every closing bracket pop the stack and confirm the popped symbol is the matching opener — any mismatch, or a closer with an empty stack, means unbalanced. At end of file the stack must itself be empty (no unmatched openers left over).
#include <stdio.h>
#define MAXLEN 10000
int is_balanced(FILE *fp)
{
char stack[MAXLEN];
int top = -1, c;
while ((c = fgetc(fp)) != EOF) {
if (c == '(' || c == '[' || c == '{') {
stack[++top] = (char) c;
} else if (c == ')' || c == ']' || c == '}') {
if (top < 0) return 0; /* closer, nothing open */
char open = stack[top--];
if ((c == ')' && open != '(') ||
(c == ']' && open != '[') ||
(c == '}' && open != '{'))
return 0; /* wrong bracket type */
}
/* any other character is ignored */
}
return top == -1; /* nothing left unclosed */
}
int main(void)
{
char filename[256];
FILE *fp;
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;
}
printf(is_balanced(fp) ? "All parentheses are balanced.\n"
: "Parentheses are NOT balanced.\n");
fclose(fp);
return 0;
}([]) pushes
( then [, pops [ to match
], pops ( to match ) — stack
ends empty.
$$\boxed{\text{is\_balanced}(\texttt{"([])"}) = \text{true}}$$
([)] pushes ( then [, then hits
): the top of the stack is [, not (,
so the type check fails immediately.
$$\boxed{\text{is\_balanced}(\texttt{"([)]"}) = \text{false}}$$| Input | Result | Reason |
|---|---|---|
([]) | Balanced | every closer matches the most recently opened bracket, stack empties |
([)] | Not balanced | ) arrives while [ is on top — type mismatch |