ProgrammingHardCompetitive coding (CP)

How many elements of {0, 2, 1, 3, 4, 7, 6} equal their own index? Count with this program.

How many elements of {0, 2, 1, 3, 4, 7, 6} equal their own index? Count with this program.
#include <stdio.h>
int main(void) {
    int a[] = {0, 2, 1, 3, 4, 7, 6}, c = 0;
    for (int i = 0; i < 7; i++)
        if (a[i] == i) c++;
    printf("%d", c);
}
  1. A. 3
  2. B. 2
  3. C. 5
  4. D. 4

Correct answer: D. 4

The matching indexes are 0, 3, 4 and 6, so there are four fixed points.

More C Programming questions

Exams that ask this topic