25-Comp-A4 Program Design and Data Structures · December 2018
Question 1 of 9: Programming — Sphere Buoyancy and a Subsystem Trade Study
Nivaar worked solution (AI-drafted; not reviewed by a licensed engineer) · Last updated
Notes on this paper
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.
Reference texts for this subject.
Cormen, Leiserson, Rivest & Stein, Introduction to Algorithms,
4th ed. — tree walks (ch. 12), stacks and linear-time scans (ch. 10, 2),
asymptotic analysis (ch. 3).
Weiss, Data Structures and Algorithm Analysis in C, 2nd ed. —
arrays and dynamic 2-D allocation (ch. 1), linked lists (ch. 3), stacks
(ch. 3.3), binary trees (ch. 4).
Deitel & Deitel, C++ How to Program, 10th ed. — class
design, templates and the Rule of Three (ch. 9–12), file streams
(ch. 14).
Kernighan & Ritchie, The C Programming Language, 2nd ed.
— character/file I/O idioms (ch. 1, 7), pointers and structures
(ch. 5–6).
Stroustrup, The C++ Programming Language, 4th ed. — value
semantics, const-correctness and templates (ch. 3, 16–18).
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 1: Programming — Sphere Buoyancy and a Subsystem Trade Study (20 marks: (a) 10, (b) 10)
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.
(a) Sink or float? (10 marks)
Write the buoyant force directly from the given formulas.
$$F_b = V\gamma = \frac{4}{3}\pi r^3 \times 62.4$$
There is no iteration or search here — both quantities are computed by
one closed-form expression each, so the program is a straight-line
calculation followed by one comparison.
#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;
}
Trace a sphere that floats. For $r=1\ \text{ft}$,
$$V = \frac{4}{3}\pi(1)^3 = 4.18879\ \text{ft}^3, \qquad
F_b = 4.18879 \times 62.4 = 261.38\ \text{lb}$$
$$\boxed{F_b(r{=}1) = 261.38\ \text{lb}}$$
so a 150 lb sphere of this radius floats, since $261.38 \ge 150$.
Trace a sphere that sinks. For $r=0.5\ \text{ft}$,
$$V = \frac{4}{3}\pi(0.5)^3 = 0.52360\ \text{ft}^3, \qquad
F_b = 0.52360 \times 62.4 = 32.67\ \text{lb}$$
$$\boxed{F_b(r{=}0.5) = 32.67\ \text{lb}}$$
so a 50 lb sphere of this smaller radius sinks, since $32.67 < 50$: the same
material at a smaller radius displaces far less water (volume scales as
$r^3$), and a modest weight is then enough to overcome it.
(b) Cheapest, most reliable, and best-value car (10 marks)
Reliability block diagram: the four subsystems sit in series, so their failure rates (reciprocal MTBFs) add. Costs are in dollars, MTBF in years.
Recognise the series structure. The failure of any
subsystem dooms the car, so under the constant-hazard model failure
rates add:
$$\lambda_{total} = \sum_i \lambda_i = \sum_i \frac{1}{MTBF_i}$$
Four subsystems × two variants each gives $N = 2^4 = 16$ whole cars
— small enough that exhaustive enumeration is not just acceptable, it
is the correct engineering answer.
Define the three figures of merit and enumerate.
$$C=\sum_i c_{i,v_i}, \qquad MTBF=\Big(\sum_i \tfrac{1}{m_{i,v_i}}\Big)^{-1},
\qquad R = C\sum_i\tfrac{1}{m_{i,v_i}}$$
$R$ (cost per failure-free year) is computed as $C$ times the rate sum rather
than $C/MTBF$, avoiding a division.
#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;
}
Cheapest car takes every subsystem's cheaper variant
independently (cost is a plain sum, hence separable): disc, wankle, oil,
standard.
$$\boxed{C_{\min} = 15.00+1067.00+320.00+40.00 = 1442.00\ \text{dollars}}$$
with MTBF $=1.0244$ years.
Most reliable car takes every subsystem's largest MTBF
(the rate sum is likewise separable): drum, conventional, air, standard.
$$\frac{1}{MTBF}=\frac{1}{5}+\frac{1}{6}+\frac{1}{9}+\frac{1}{4}
=\frac{36+30+20+45}{180}=\frac{131}{180}$$
$$\boxed{MTBF_{\max}=\frac{180}{131}=1.3740\ \text{years}}$$
at a cost of $2,345.00.
Best-value car is neither of the above, because $R$
couples cost and reliability and does not separate — it must genuinely
be searched. The winner is drum, wankle, oil, standard:
$$R=\frac{1452.00}{1.0797}$$
$$\boxed{R_{\min}=1344.83\ \text{dollars per failure-free year}}$$
For just $10.00 more than the cheapest car, drum brakes buy
enough extra life to beat it on value.