edited by
10,646 views
23 votes
23 votes

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] - 3;
}
int work2(int *a, int i, int j)
{
    int t1 = i+2;
    int t2 = a[t1];
    a[j] = t2+1;
    return t2 - 3;
}

S1: The transformation form work1 to work2 is valid, i.e., for any program state and input arguments, work2 will compute the same output and have the same effect on program state as work1 

S2: All the transformations applied to work1 to get work2 will always improve the performance (i.e reduce CPU time) of work2 compared to work1

  1. S1 is false and S2 is false
  2. S1 is false and S2 is true
  3. S1 is true and S2 is false
  4. S1 is true and S2 is true
edited by

3 Answers

Best answer
27 votes
27 votes

Consider an array a = 1 2 3 4 5 and condition i + 2 =j. Lets take i =0 and j =2 for this example.

Work 1, 

x = a[0+2] = 3

a[2] = 3 + 1 = 4;  which means a = 1 2 4 4 5

return a[0+2] - 3 = 4 -3 = 1

Work 2

t1 = 0 + 2 = 2

t2 = a[2] = 3

a[2] = 3 + 1 = 4, which means a = 1 2 4 4 5 again

return t2 - 3 = 3 -3 =0

Hence S1 is false when i + 2 =j. S2 will also be false, since we cant explicitly say the performance of work2 will always be better than work1.

Hence answer is A

selected by
9 votes
9 votes
answer - A

when j == i+2 programs will return different results
–1 votes
–1 votes
Ans- C

Only an extra variable t1 has been added in work 2 instead of directly computing the subscript as in work 1.The output will be same. S1 is true.

The addition of variables t1 and t2 will not improve the performance in anyway. i.e S2 is false.

Corrcet me I'm wrong
Answer:

Related questions

10.4k
views
2 answers
19 votes
Rucha Shelke asked Sep 26, 2014
10,420 views
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...
7.5k
views
4 answers
25 votes
go_editor asked Nov 7, 2016
7,533 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 $ L=\left \{ a^{i}b^{j}\mid i\neq j...
12.4k
views
4 answers
45 votes
Rucha Shelke asked Sep 26, 2014
12,411 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 a\mid b$$A\rightar...
11.3k
views
4 answers
34 votes
Rucha Shelke asked Sep 26, 2014
11,347 views
Consider the following translation scheme. $ S\rightarrow ER$$ R\rightarrow *E\left \{ \text{print}(\text{‘}*\text{’}); \right \} R\mid \varepsilon $$ E\rightarrow F+...