488 views
1 votes
1 votes

Consider these two functions.
 

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;
}


how o/p produced by work2 is less than the o/p produced by t1?

1 Answer

1 votes
1 votes

They work differently and eventually give different result in some particular case.

by looking at the code segements of both work1 and work2, the intended purposes of them could be like as follows:

1. $a[j]$ = ( initial second element of array from Ith position + 1 )

2. return value = ( initial second element of array from Ith position + 1 ) - 3;

But above code segement work1 & work2 will give different results if input $(*a,i,j)$ is such that $j=i+2$

if $j=i+2;$

in work1 we are not using any temporary variable to store old $a[j]$, So,
return value of work1 = $modified \ \ a[j] -3$


But,
return value of work2 = $old \ \ a[j] - 3$

And for j = i+2 case work1 o/p is greater than work2 o/p by 1

edited by

Related questions

0 votes
0 votes
1 answer
4