← Back to Book Detail

2 Swapping (9/11) -- Patterns for Beginning Programmers

Browse
81%

2 Swapping

2 Swapping Programs commonly need to swap the contents of two variables. While this probably seems simple at first glance, it’s actually a little more complicated than it seems. Motivation Suppose, for example, that you are writing a fantasy role playing game. In such games, players commonly acquire items of various kinds (e.g., weapons, spells, gold, food). When they acquire such an item, a representation of it (e.g., a 'T' character for a teleportation spell) is assigned to a variable of appropriate type (e.g., a char variable named spell ). As the game progresses, two players meet and realize that it would benefit both of them to swap their spells. This chapter considers a sequence of statements that can be used to solve this problem. Review There are a couple of things to recall in this regard. First, remember that the assignment operator (i.e., the = operator), stores the right side operand in the memory location identified by the left side operand. So, the statements in the following fragment: char swordOfAlice, swordOfBetty; swordOfAlice = 'C'; // Caladbolg swordOfBetty = 'D'; // Durendal can be visualized as in Figure 2.1. The first assignment statement stores the binary representation of the character 'C' (i.e., 01000011 using an ASCII representation) into the memory location identified by swordOfAlice and the second assignment statement stores the binary representation of the character 'D' (i.e., 01000100 using an ASCII representation) into the memory location identified by swordOfAlice . Second, remember that a variable can hold only one “thing” (either a value or a reference, depending on the type of the variable). So, the statements in the following fragment: swordOfAlice = swordOfBetty; swordOfBetty = swordOfAlice; can be visualized as in Figure 2.2. The first assignment statement stores the current contents[1] of swordOfBetty (i.e., a 'D' ) in the memory location identified by swordOfAlice . The second assignment statement stores the current contents of swordOfAlice (i.e., now a 'D' ) in the memory location identified by swordOfBetty . In other words, these statements didn’t swap the contents of the two variables at all. Instead, the memory locations identified by both variables now contain a binary representation of the character 'D' . Thinking About The Problem One way to think about solving the swapping problem is to imagine a situation in which you have Caladbolg in your left hand and Durendal in your right hand and you now want to swap the two. To make progress, you need a place where you can temporarily store one of the swords. For example, you can place Caladbolg on a table, move Durendal from your right hand to your left hand, and then pick up Caladbolg with your right hand. Note that this situation is not completely analogous to the way assignment works because the assignment operator replaces the current contents of the memory identified by a variable with a copy of the right side operand. However, it does provide the fo
← Previous Chapter Next Chapter →