Simvik

Minimum and maximum of a log

Scan an array of structs and return two results through pointers. An easy embedded C problem (Structs, unions and memory layout), run and graded in the browser.

easy · Structs, unions and memory layout · structs · arrays · out-parameters

Find the smallest and largest value among the n samples and store them in *min and *max. Return true.

If n is 0 there is no answer: return false and leave both outputs unchanged.

Starter code

bool value_range(const sample_t *s, size_t n, int16_t *min, int16_t *max) {
  // Write the smallest and largest value; return false for an empty log.
  return false;
}

Example tests

Mixed values

sample_t s[] = { { 10, 5 }, { 20, -3 }, { 30, 12 }, { 40, 7 } };
int16_t lo = 99, hi = 99;
bool ok = value_range(s, 4, &lo, &hi);
printf("%d %d %d\n", ok, lo, hi);

Expected output:

1 -3 12

One sample

sample_t s[] = { { 10, 4 } };
int16_t lo = 99, hi = 99;
bool ok = value_range(s, 1, &lo, &hi);
printf("%d %d %d\n", ok, lo, hi);

Expected output:

1 4 4

Learn it first

More Structs, unions and memory layout problems