The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+8 votes
441 views

What does the following code do?

var a, b: integer;
begin
    a:=a+b;
    b:=a-b;
    a:a-b;
end;
  1. exchanges $a$ and $b$
  2. doubles $a$ and stores in $b$
  3. doubles $b$ and stores in $a$
  4. leaves $a$ and $b$ unchanged
  5. none of the above
     
asked in Algorithms by Veteran (59.5k points)
edited by | 441 views
0
typo mistake in a:a-b; it should be a:=a-b;

3 Answers

+16 votes
Best answer
Answer is simply $A$ i.e. it swaps the values of the two.. Take any two values for $A$ and $B$. and perform the given operations over them.
answered by Boss (19.7k points)
edited by
+6 votes
(a)
initially a = 10 b = 5
a = 15
b =  10
a = 5
answered by Boss (40.2k points)
+1 vote

1. a= a+b

2. b= a-b= (a+b)-b= a (from 1)

3. a= a-b= (a+b) - (a) = b (from 1 and 2)

Hence the values are getting exchanged

Answer(A).

answered by Active (1.4k points)


Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

39,847 questions
46,812 answers
141,145 comments
59,058 users