Kamis, 21 Maret 2013

Exchanging the values of two variable

book : How To Solve it by Computer

Algoritm 2.1
Exchanging the values of two variable

The problem : how to exchange values between vairiable a and variable b.

To more clearly see the following example:
The initial state:


        From the example image above, how to swap the value of the variable b to be 721, and the alue of a variable to 463.



        To get a result as above, we can not directly attached to the alue of the variable b to a, and  a to b, if  we take this way, there will be:


         This happened because, when the value of variable b is copied to a variable b the value of the old into the new value of a variable, then the new value of a variable copied to b, the result is the value of the variable b, same as the old value of the variable b.


         Anyway is to make the variable t (new variable):


          We can copy the value of a variable to t, then the value is copied into a variable b, then value of the variable t copied to b.


0 komentar:

Posting Komentar

 

Blogger news

Blogroll

About