The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+8 votes
475 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.6k points)
edited by | 475 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 (41k 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.6k points)
Answer:

Related questions

0 votes
0 answers
7
asked Sep 30, 2014 in Non GATE by Kathleen Veteran (59.6k points) | 113 views


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

42,599 questions
48,600 answers
155,672 comments
63,739 users