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) Six standard cord ratings {3.0, 6.0, 7.0, 10.0, 13.0, 15.0} A and the rule linking cord length to the amperage headroom required. (b) The nine-digit-plus-check-character ISBN-10 structure and its positional-weighted-sum check formula.
Find. (a) A program that reads an appliance amperage and cord length and prints the correct cord rating, or refuses lengths over 100 ft. (b) A program that reads a 10-character ISBN and reports whether its check character is correct.
Approach. (a) First find the smallest listed rating that is ≥ the appliance's amperage (the "same or higher" rule for ≤50 ft), then, only for the 50–100 ft band, step to the next rating above that minimum. (b) Compute the weighted digit sum directly from the array positions and compare the derived check symbol against the tenth character.
#include <stdio.h>
static const double RATINGS[] = {3.0, 6.0, 7.0, 10.0, 13.0, 15.0};
#define N_RATINGS 6
int main(void)
{
double amperage, length;
int i, min_idx;
printf("Enter appliance amperage: ");
scanf("%lf", &erage);
printf("Enter cord length in feet: ");
scanf("%lf", &length);
if (length > 100.0) {
printf("No cord of length greater than 100 feet should be used.\n");
return 0;
}
/* smallest rating that is >= the appliance's amperage */
min_idx = -1;
for (i = 0; i < N_RATINGS; i++) {
if (RATINGS[i] >= amperage) { min_idx = i; break; }
}
if (min_idx == -1) {
printf("No cord rating is high enough for this appliance.\n");
return 0;
}
if (length <= 50.0) {
printf("Use a cord rated at %.0f amperes or more.\n", RATINGS[min_idx]);
} else { /* 50 < length <= 100 */
if (min_idx + 1 >= N_RATINGS) {
printf("No cord rating is high enough for this length.\n");
} else {
printf("Use a cord rated at %.0f amperes or more.\n", RATINGS[min_idx + 1]);
}
}
return 0;
}#include <stdio.h>
#include <ctype.h>
int main(void)
{
char raw[32];
int digits[9], n = 0;
char check = 0;
int i, sum = 0, remainder;
char computed;
printf("Enter ISBN: ");
fgets(raw, sizeof raw, stdin);
for (i = 0; raw[i] != '\0' && raw[i] != '\n'; i++) {
if (raw[i] == '-') continue;
if (n < 9 && isdigit((unsigned char)raw[i])) {
digits[n++] = raw[i] - '0';
} else if (n == 9) {
check = raw[i]; /* tenth character: digit or 'X' */
}
}
if (n != 9 || check == 0) {
printf("Invalid ISBN: wrong length.\n");
return 0;
}
for (i = 0; i < 9; i++) sum += (i + 1) * digits[i];
remainder = sum % 11;
computed = (remainder == 10) ? 'X' : (char)('0' + remainder);
if (computed == check)
printf("%s is a valid ISBN.\n", raw);
else
printf("%s is NOT a valid ISBN (expected check character %c).\n", raw, computed);
return 0;
}| Case | Result |
|---|---|
| Amperage 5.8 A, length 60 ft | Use a cord rated at 7 A or more |
| Amperage 5.8 A, length 40 ft | Use a cord rated at 6 A or more |
| Amperage 15.0 A, length 60 ft | No rating high enough (top tier already selected) |
| ISBN 0-670-82162-4, weighted sum | 158 |
| ISBN 0-670-82162-4, check character | 4 (valid) |