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);
}- A. 3
- B. 2
- C. 5
- 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
- What is printed? c #include <stdio.h> int main(void) { int a[] = {2, 7, 1}; int e = a + sizeof a / sizeof a[0] - 1; printf("%d", e); }
- What is printed? Assume int is 4 bytes. c #include <stdio.h> int main(void) { int a[4]; printf("%ld", (long)((char )&a[2] - (char…
- How many valid strings of n = 3 pairs of parentheses are there? The program counts them recursively. What is printed? c #include <stdio.h>…
- What is printed? c #include <stdio.h> #include <stdlib.h> int cmp(const void a, const void b) { return (const unsigned char )a - (const…
- What is printed? c #include <stdio.h> int main(void) { unsigned x = 0xFF & ~0x1Cu; printf("%u", x); }
- What is printed? c #include <stdio.h> int main(void) { unsigned x = 0x0F; x |= 0x40; x &= ~1u; printf("%u", x); }