ProgrammingModerate
How many iterations does for (i = 1; i < 1024; i *= 2) do?
- A. 9
- B. 10
- C. 11
- D. 1024
Correct answer: B. 10
i takes the values 1, 2, 4, ..., 512, which is 10 values. Doubling loops run log2(n) times.
More C Programming questions
- A binary search over n = 10^6 sorted records, in the worst case, takes how many loop iterations?
- A sieve of Eratosthenes up to n = 100 marks composites by starting each prime p at pp, and only for pp <= n. How many marking operations…
- With fib(1) = fib(2) = 1, what is the largest n for which fib(n) fits in a signed 64-bit long long?
- A loop appends one character to a string with strcat(s, "x") for n = 10^5 times. Each strcat calls strlen on the whole string first. What…
- for (i = 0; i < strlen(s); i++) process(s[i]); with a string of length 5. How many times is strlen called?
- Which C code is correct and fast enough for finding whether a sorted array a of n = 2 x 10^5 ints has two elements summing to t?