-
Consider the following C code segment :
for (i = 0, i < n; i ++) {
for (j = 0, j < n; j ++) {
if (i % 2){
x + = (4 * j + 5 * i);
Y + = (7 + 4 * j);}}}
Which one of the following is false?
-
- The code contains loop invariant computation.
- There is scope of common sub-expression elimination in this code
- There is scope of strength reduction in this code
- There is scope of dead code elimination in this code
- The code contains loop invariant computation.
Correct Option: D
The expression (4*j) is used at two places- so common sub-expression elimination is possible i% 2 is loop invariant for the inner loop
5*i is also loop invariant for inner loop x + = 5*i can be replaced by x + = p;
p + = 5; (p must be initialized to 0 before the loop).
Thus replacing * with + gives strength reduction. So, only (d) is false here.