• recategorized by
3,336 views
3 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?

  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

Related Questions :

3 Answers

Best answer
15 15 votes
  • loop invariant: A fragment of code that resides in the loop and computes the same value in all iterations. In given loop if $\text{(i%2)}$ computes same value within loop of $j$. This code can be moved out of the loop.
 for(i=0;i<n;i++) {
if(i%2)
   {
    for(j=0;j<n;j++)
    {
       ......

This movement is called code hoisting

$A$ is true 

  • The sub expression  $4*j$ is used at two places which can be eliminated.

$B$ is true

  • Strength reduction: Operations that consume more time and space can be replaced by simple operations that produce the same result.

  $4*j$ can be replaced by $j << 2$

$C$ is true

  • There is no dead / Unreachable code.

$D$ is false

Answer is $D.$

• edited by
0 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 ?

Answer:
Position:
Show:

Related questions

3 3 votes
1 1 answer
4.2k
4.2k views
gatecse asked Dec 17, 2017
4,229 views
Consider the following table :$\begin{array}{|l|l|l|} \hline \textbf{A.} & \text{Activation record} & \textbf{p.} & \text{Linking loader} \\\hline \textbf{B.} & \text{Loc...
1 1 vote
2 2 answers
2.0k
2.0k views
gatecse asked Dec 17, 2017
2,017 views
Which languages necessarily need heap allocation in the runtime environment?Those that support recursionThose that use dynamic scoping Those that use global variablesThos...
9 9 votes
2 answers 2 answers
4.7k
4.7k views
gatecse asked Dec 17, 2017
4,694 views
Consider the following $C$ function#include<stdio.h int main(void) { char c[]="ICRBCSIT17" char *p=c; printf("%s",c+2[p]-6[p]-1); return 0; }The output of the program is ...
5 5 votes
2 2 answers
3.7k
3.7k views
gatecse asked Dec 17, 2017
3,693 views
Which of the following related to snowflake schema is true?Each dimension is represented by a single dimensional tableMaintenance efforts are lessDimension tables are nor...