Algorithm 2.1 Exchanging The Values Of Two Variables
This page is a generated reference surface for selective reading. It exists to keep the learner apps guide-first while still preserving source access.
Learning objectives
- Explain the main ideas and vocabulary in Algorithm 2.1 Exchanging The Values Of Two Variables.
- Work through the source examples for Algorithm 2.1 Exchanging The Values Of Two Variables without depending on raw chunk order.
- Use Algorithm 2.1 Exchanging The Values Of Two Variables as selective reference when learner modules point back to How To Solve It By Computers.
Prerequisites
- None curated yet.
Module targets
module-05-problem-solving
AI companion modes
- Explain simply
- Socratic tutor
- Quiz me
- Challenge my understanding
- Diagnose my confusion
- Generate extra practice
- Revision mode
- Connect forward / backward
Source-of-truth note
This unit is anchored to How To Solve It By Computers and the source chapter "Algorithm 2.1 Exchanging The Values Of Two Variables". Use external resources only to clarify, extend, or modernize details without replacing the chapter's conceptual spine.
External enrichment
No chapter-specific enrichment resources are curated yet. Add them in the unit manifest when a source clearly improves learning.
Source provenance
- Primary source:
How To Solve It By Computers - Source chapter: Algorithm 2.1 Exchanging The Values Of Two Variables
- Raw source file:
018-algorithm-2-1-exchanging-the-values-of-two-variables.md
Merged source
Algorithm 2.1 Exchanging The Values Of Two Variables
id: '018-algorithm-2-1-exchanging-the-values-of-two-variables' book: "How to Solve It by Computer" source_file: "../how-to-solve-it-by-computer-r-g-dromey-for-unit-1.pdf" ocr_source: "../ocr-extracted.txt" chunk_index: 18 chunk_total: 138 title: "Algorithm 2.1 EXCHANGING THE VALUES OF TWO VARIABLES" part: "" chapter: "" pages: start: 64 end: 68 words: 828
Algorithm 2.1 EXCHANGING THE VALUES OF TWO VARIABLES
Algorithm 2.1 EXCHANGING THE VALUES OF TWO VARIABLES
Problem
Given two variables, a and b, exchange the values assigned to them.
ueveuupruuell'
The problem of interchanging the values associated with two variables
involves a very fundamental mechanism that occurs in many sorting and data
manipulation algorithms. To define the problem more clearly we will
examine a specific example.
Consider that the variables a and b are assigned values as outlined
Starting configuration
463 721
This means that memory cell or variable a contains the value 721, and
contents of a with 463, and the contents of b with 721. In other words we
want to end up with the configuration below:
Target configuration
a
721 463
To change the value of a variable we can use the assignment operator.
Because we want a to assume the value currently belonging to b, and b the
helnno;no tn n we nerhnnc the evohnnoe with the follnwino b assignments:
(1)
(2)
where " is the assignment operator. In (1) "
causes the value stored
in memory cell b to be copied into memory cell a.
Let us work through these two steps to make sure they have the desired
effect.
qtnrted ennfionrntinn with We the
then nfter eyecutinn of nqqionment have the we
The assignment (1) has changed the value of a but has left the value of b
untoucncu. cnccK111g With oui colli1guEdL1011 wc see iliac a nas
assumed the value 463 as required. So far so good! We must also check on b.
When the nqqiønment cten (2) i e h: = n iq made after executing sten (1 we
end up with:
In executing steo (2) a is not changed while b takes on the value that currentlv
VVV up UUVD
represent the solution we are seeking. The problem arises because in making
the assignment:
b
we have lost the value that originally belonged to a (i.e. 721 has been lost). It
stated more carefully as:
old value of b; new value of a
old value of a new value of b
What we have done with our present proposal is to make the assignment
new value of b new value of a
In other words when we execute step (2) we are not using the value a that will
make things work correctly--because a has already changed.
To solve this exchange problem we need to find a way of not destroying
"the old value of a" when we make the assignment
b a.
A way to do this is to introduce a temporary variable t and copy the original
value of a into this variable before executing step (1). The steps to do this
are:
b a.
After these two steps we have
b a
We are better off than last time because now we still have the old value of a
stored in t. It is this value that we need for assignment to b. We can therefore
make the assignment
t
After execution of this step we nave:
a
Rechecking with our target configuration we see that a and b have now been
interchanged as required.
The exchange nrocednre he outlined can now
Alaorithm descriDtion
-
Save the original value of a in t.
-
Assign to a the original value of b.
-
Assign to b the original value of a that is stored in t.
The exchange mechanism as a programming tool is most usefully
implemented as a Drocedure that acceots two variables and returns their
Pascal implementation
procedure exchange (var a,b: integer);
var. ameger, begin {save the original value of a then exchange a and b} {assert:
a =aOAb --
b:=t (assert:
a =bOAb end
Notes on design
- The use of an intermediate temporary variable allows the exchange of
two variables to proceed correctly.
VI" pnaDIL..,VD at a KA y a a v a 1
always assumes the value dictated by the most recent assignment made
to that variable.
- Working through the mechanism with a particular example can be a
useful way of detecting design faults.
A more common application of exchange involves two array elements 4.
(e.g. a[i] and a[j]). The steps for such an exchange are:
e-- nrn.
a[j];
t
Applications
Sorting algorithms.
Supplementary problems
2.1.1 Given two glasses marked A and B. Glass A is full of raspberry drink
and glass B is full of lemonade. Suggest a way of exchanging the
onntentQ nf A R
2.1.2 Design an algorithm that makes the following exchanges:
The arrows indicate that b is to assume the value of a, c the value of
b, and so on.
2.1.3 Design an algorithm that makes the following exchanges:
Given two variables of integer type a and b, exhange their values 2.1.4
without using a third temporary variable.
2.1.5 What happens when the arguments given to the procedure exhange
are i and a[i]?