25-Comp-A4 Program Design and Data Structures · December 2014
Nivaar worked solution (AI-drafted; not reviewed by a licensed engineer)
Paper format. 98-Comp-A4 Program Design and Data Structures, December 2014 — 3 hours, closed book, no calculator permitted. Nine questions, each of equal weight (20 marks); 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, 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. Four subsystems in series, each available in two variants, with the manufacturing cost and mean time between failure of every variant, and the series MTBF relation quoted in the question.
| Subsystem | Variant | MTBF (years) | Cost |
|---|---|---|---|
| brakes | disc | 4 | $15.00 |
| drum | 5 | $25.00 | |
| engine | wankle | 3 | $1,067.00 |
| conventional | 6 | $1,850.00 | |
| suspension | air | 9 | $430.00 |
| oil | 7 | $320.00 | |
| electrical | computer | 2 | $130.00 |
| standard | 4 | $40.00 |
Find. A program that enumerates every whole car and reports three winners: the cheapest, the one with the longest MTBF, and the one with the lowest cost per failure-free year.
Approach. Enumerate all $2^4$ combinations with four nested loops; for each, add the four costs and the four failure rates, invert the rate sum to get the system MTBF, and track three running minima/maxima in the same pass.
#include <stdio.h>
#define NSUB 4 /* brakes, engine, suspension, electrical */
#define NOPT 2 /* two choices per subsystem */
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;
int cheapAt[NSUB], longAt[NSUB], bestAt[NSUB];
double cheapC = -1.0, longM = -1.0, bestR = -1.0;
/* Nested loops, as the hint directs: 2 x 2 x 2 x 2 = 16 whole cars. */
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]; int k;
double c, inv, m, perYear;
pick[0] = b; pick[1] = e; pick[2] = s; pick[3] = l;
c = inv = 0.0;
for (k = 0; k < NSUB; k++) {
c += cost[k][pick[k]];
inv += 1.0 / mtbf[k][pick[k]]; /* failure rates add */
}
m = 1.0 / inv; /* system MTBF, years */
perYear = c * inv; /* = c / m */
printf("%-10s %-13s %-4s %-9s %8.2f %7.4f %9.2f\n",
name[0][b], name[1][e], name[2][s], name[3][l],
c, m, perYear);
if (cheapC < 0.0 || c < cheapC) { cheapC = c; for (k=0;k<NSUB;k++) cheapAt[k]=pick[k]; }
if (longM < 0.0 || m > longM ) { longM = m; for (k=0;k<NSUB;k++) longAt [k]=pick[k]; }
if (bestR < 0.0 || perYear < bestR ) { bestR = perYear; for (k=0;k<NSUB;k++) bestAt [k]=pick[k]; }
}
putchar('\n');
printf("cheapest : %8.2f\n", cheapC);
printf("longest MTBF : %8.4f years\n", longM);
printf("best value : %8.2f per failure-free year\n", bestR);
return 0;
}| Brakes | Engine | Suspension | Electrical | Cost | MTBF | Cost per failure-free year |
|---|---|---|---|---|---|---|
| disc | wankle | air | computer | 1,642.00 | 0.8372 | 1,961.28 |
| disc | wankle | air | standard | 1,552.00 | 1.0588 | 1,465.78 |
| disc | wankle | oil | computer | 1,532.00 | 0.8155 | 1,878.52 |
| disc | wankle | oil | standard | 1,442.00 | 1.0244 | 1,407.67 |
| disc | conventional | air | computer | 2,425.00 | 0.9730 | 2,492.36 |
| disc | conventional | air | standard | 2,335.00 | 1.2857 | 1,816.11 |
| disc | conventional | oil | computer | 2,315.00 | 0.9438 | 2,452.80 |
| disc | conventional | oil | standard | 2,225.00 | 1.2353 | 1,801.19 |
| drum | wankle | air | computer | 1,652.00 | 0.8738 | 1,890.62 |
| drum | wankle | air | standard | 1,562.00 | 1.1180 | 1,397.12 |
| drum | wankle | oil | computer | 1,542.00 | 0.8502 | 1,813.69 |
| drum | wankle | oil | standard | 1,452.00 | 1.0797 | 1,344.83 |
| drum | conventional | air | computer | 2,435.00 | 1.0227 | 2,380.89 |
| drum | conventional | air | standard | 2,345.00 | 1.3740 | 1,706.64 |
| drum | conventional | oil | computer | 2,325.00 | 0.9906 | 2,347.14 |
| drum | conventional | oil | standard | 2,235.00 | 1.3166 | 1,697.54 |
| Question asked | Winning system | Cost | MTBF (years) | Cost per failure-free year |
|---|---|---|---|---|
| 1. Cheapest | disc / wankle / oil / standard | $1,442.00 | 1.0244 | $1,407.67 |
| 2. Longest MTBF | drum / conventional / air / standard | $2,345.00 | 1.3740 | $1,706.64 |
| 3. Lowest cost per failure-free year | drum / wankle / oil / standard | $1,452.00 | 1.0797 | $1,344.83 |
| Combinations enumerated | 16 | |||