retagged by
10,459 views
19 votes
19 votes

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? 

  1. The code contains loop invariant computation 
  2. There is scope of common sub-expression elimination in this code 
  3. There is scope of strength reduction in this code 
  4. There is scope of dead code elimination in this code 
retagged by

2 Answers

Best answer
35 votes
35 votes
4*j

is used at two places- so common subexpression 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. 

selected by
1 votes
1 votes

There is scope for dead code elimination in this code.

Explanation:

  1. The code contains loop invariant computation:

    • True. The expressions (4*j + 5*i) and (7 + 4*j) are computed inside the inner loop but do not depend on the inner loop variable j. They can be moved outside the inner loop to improve efficiency.
  2. There is scope for common sub-expression elimination in this code:

    • True. The expression (4*j + 5*i) is computed twice with the same values of i and j inside the inner loop. Common sub-expression elimination can be applied to avoid redundant computations.
  3. There is scope for strength reduction in this code:

    • True. The expression (4*j + 5*i) involves multiplication and addition. Strength reduction could be applied by replacing the multiplication with a series of cheaper operations.
  4. There is scope for dead code elimination in this code:

    • False. The code does not contain unreachable or dead code. All statements inside the loops are reachable during the execution of the loops.
Answer:

Related questions

10.7k
views
3 answers
23 votes
Rucha Shelke asked Sep 26, 2014
10,713 views
Consider these two functions and two statements S1 and S2 about them. int work1(int *a, int i, int j) { int x = a[i+2]; a[j] = x+1; return a[i+2] - ... S2 is falseS1 is false and S2 is trueS1 is true and S2 is falseS1 is true and S2 is true
7.6k
views
4 answers
25 votes
go_editor asked Nov 7, 2016
7,603 views
The grammar$S\rightarrow AC\mid CB$C\rightarrow aCb\mid \epsilon$A\rightarrow aA\mid a$B\rightarrow Bb\mid b$generates the language $ ... max (l,m) + 2$l + m + 2$l + m + 3$\max (l,m) + 3$
12.5k
views
4 answers
45 votes
Rucha Shelke asked Sep 26, 2014
12,479 views
Which one of the following grammars generates the language $ L=\left \{ a^{i}b^{j}\mid i\neq j \right \}$?$S\rightarrow AC\mid CB$C\rightarrow aCb\mid ... \mid CB$C\rightarrow aCb\mid \varepsilon$A\rightarrow aA\mid a$B\rightarrow Bb\mid b$
11.4k
views
4 answers
34 votes
Rucha Shelke asked Sep 26, 2014
11,389 views
Consider the following translation scheme. $ S\rightarrow ER$ ... * 3 + 4$2 * +3 \ 4$2 \ 3 * 4 +$2 \ 3 \ 4+*$