ProgrammingHardTCS NQT · Infosys

What is the difference between the largest and the smallest element of {4, -2, 9, 0}, found in a single pass with pointers? Compute it with this program.

What is the difference between the largest and the smallest element of {4, -2, 9, 0}, found in a single pass with pointers? Compute it with this program.
#include <stdio.h>
int main(void) {
    int a[] = {4, -2, 9, 0};
    int *lo = a, *hi = a;
    for (int *p = a + 1; p < a + 4; p++) {
        if (*p < *lo) lo = p;
        if (*p > *hi) hi = p;
    }
    printf("%d", *hi - *lo);
}
  1. A. 13
  2. B. 9
  3. C. 11
  4. D. 7

Correct answer: C. 11

The largest value is 9 and the smallest is -2, so the difference is 11.

More C Programming questions

Exams that ask this topic