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. For (a), the specific weight of water $\gamma=62.4\ \text{lb/ft}^3$ and the volume law $V=(4/3)\pi r^3$ for a sphere of radius $r$; the program itself reads the weight $W$ (lb) and radius $r$ (ft). For (b), four subsystems in series, each in two variants, with the manufacturing cost and MTBF of every variant.
Find. (a) Whether a given sphere sinks or floats in water. (b) The cheapest, most reliable, and best-value whole-car configuration out of all combinations of subsystem variants.
Approach. (a) Compute the buoyant force directly from the volume formula and compare it against the input weight. (b) Enumerate all $2^4=16$ combinations with four nested loops, since the design space is tiny; track the three running winners (cheapest cost, longest MTBF, best cost per failure-free year) in one pass.
#include <stdio.h>
#include <math.h>
#define GAMMA_WATER 62.4 /* lb/ft^3, specific weight of water */
int main(void)
{
double weight, radius, volume, buoyant_force;
printf("Enter the weight (lb) and radius (ft) of the sphere: ");
if (scanf("%lf %lf", &weight, &radius) != 2 || weight < 0.0 || radius < 0.0) {
printf("Invalid input.\n");
return 1;
}
volume = (4.0 / 3.0) * M_PI * pow(radius, 3);
buoyant_force = volume * GAMMA_WATER;
if (buoyant_force >= weight)
printf("The sphere will float (buoyant force = %.2f lb).\n", buoyant_force);
else
printf("The sphere will sink (buoyant force = %.2f lb).\n", buoyant_force);
return 0;
}#include <stdio.h>
#define NSUB 4
#define NOPT 2
int main(void)
{
const char *name[NSUB][NOPT] = {
{"disc", "drum"},
{"wankle", "conventional"},
{"air", "oil"},
{"computer", "standard"}
};
const double mtbf[NSUB][NOPT] = {{4, 5}, {3, 6}, {9, 7}, {2, 4}};
const double cost[NSUB][NOPT] = {{ 15.00, 25.00},
{1067.00, 1850.00},
{ 430.00, 320.00},
{ 130.00, 40.00}};
int b, e, s, l;
double cheapC = -1.0, longM = -1.0, bestR = -1.0;
for (b = 0; b < NOPT; b++)
for (e = 0; e < NOPT; e++)
for (s = 0; s < NOPT; s++)
for (l = 0; l < NOPT; l++) {
int pick[NSUB] = {b, e, s, l};
double c = 0.0, inv = 0.0;
int k;
for (k = 0; k < NSUB; k++) {
c += cost[k][pick[k]];
inv += 1.0 / mtbf[k][pick[k]];
}
double m = 1.0 / inv, perYear = c * inv;
if (cheapC < 0.0 || c < cheapC) cheapC = c;
if (longM < 0.0 || m > longM ) longM = m;
if (bestR < 0.0 || perYear < bestR) bestR = perYear;
}
printf("cheapest : %.2f\n", cheapC);
printf("longest MTBF : %.4f years\n", longM);
printf("best value : %.2f per failure-free year\n", bestR);
return 0;
}| Part | Quantity | Result |
|---|---|---|
| (a) | Buoyant force, $r=1$ ft | 261.38 lb → a 150 lb sphere floats |
| (a) | Buoyant force, $r=0.5$ ft | 32.67 lb → a 50 lb sphere sinks |
| (b) | 1. Cheapest | disc/wankle/oil/standard, $1,442.00 |
| (b) | 2. Longest MTBF | drum/conventional/air/standard, 1.3740 yr |
| (b) | 3. Lowest cost per failure-free year | drum/wankle/oil/standard, $1,344.83/yr |