in Programming in C
2,634 views
3 votes
3 votes

Consider the code segment

int i,j,x,y,m,n;
n=20;
for(i=0;i<n;i++)
{
        for(j=0;j<n;j++)
        {
            if(i%2)
            {
                x+=((4*j)+5*i);
                y+=(7+4*j);
            }
        }
}
m=x+y;

Which one of the following is false ?

A) The code contains loop invariant computation

B) There is scope of common sub-expression elimination in this code

C)There is scope of strength reduction in this code

D)There is scope of dead code elimination in this code

in Programming in C
2.6k views

4 Comments

int i,j,x,y,m,n;
n=20;
for(i=0;i<n;i++)
{
        for(j=0;j<n;j++)
        {
            if(i%2)
            {
                x+=((4*j[4<<j] strength reduction)+5*i (loop invarient place after 1st for loop));
                y+=(7+4*j);
            }
        }
}
m=x+y;
2
2
I mean D is correct ! It is false among all
1
1
What is dead code elimination????
0
0

1 Answer

0 votes
0 votes

Option A : Why the code 

x+=((4*j)+5*i);
y+=(7+4*j);

is loop invariant ? x and y get updated with with change in values of i,j for every iteration

Option D: Also if we use constant propogation for n the loop headers become .. i<20... and j<20... respectively.

Hence n = 20 is a possible candidate for dead code elimination 

So isnt A the right option ?

Related questions