retagged by
96,165 views
4 votes
4 votes

Pankaj and Mythili were both asked to write the code to evaluate the following expression: 
$$a - b + c/(a-b) + (a-b)^2 $$
Pankaj writes the following code statements (Code A):

print (a-b) + c/(a-b) + (a-b)*(a-b)

Mythili writes the following code statements (Code B):

d = (a-b)
print d + c/d + d*d

If the time taken to load a value in a variable, for addition, multiplication or division between two operands is same, which of the following is true?

  1. Code A uses lesser memory and is slower than Code B 
  2. Code A uses lesser memory and is faster than Code B 
  3. Code A uses more memory and is faster than Code B 
  4. Code A uses more memory and is slower than Code B
retagged by

2 Answers

Best answer
9 votes
9 votes

Option 1 : Code A uses lesser memory and is slower than Code B 

In code A, three variables are used a, b and c to store values. The computation of a-b is repetitive and is performed 4 times in print statement requires more time compared to the computation of print statement of code B, because of the use of additional variable d, to store the computed value of a-b. The print statement of code B replaces d with the already computed value of a-b.

selected by
–1 votes
–1 votes
their is the second option is true.

 

Option 2 : Code A uses lesser memory and is faster than Code B

If we are define 3 variable then low memory used. and if go with the 4 variable then every time d=(a-b)

firstly. so the code becomes slow. so the I m go with Option 2 : Code A uses lesser memory and is faster than Code B.

Related questions

0 votes
0 votes
0 answers
1
rahuldb asked Nov 17, 2016
2,114 views
Write the assembly language code segment to evaluate the following arithmetic expression:X=((A+B)*C)/(D-E*F+G*H)Usingi)Stack based organizationii)RISC organization
0 votes
0 votes
3 answers
2
radha gogia asked Dec 9, 2015
1,360 views
In this one I am unable to follow in the above node marked as "-" ,it has two edges one upward and one downward for "+" node so then how to proceed with this ?
0 votes
0 votes
1 answer
3