Activity #4

Swapping Values

Activity Type: drama

Discipline: programming

Area: Intro Programming (CS1)

Purpose:
To illustrate the necessity for a temporary variable to swap two values.

Time needed (estimate): 5 minutes

Description:
Ask two student volunteers to come to the front of the room and ask both of them to place one hand behind their back. Give them each an object (e.g. a tennis ball) for their available hand. Now ask them to switch objects without putting either of them down. Ideally they shouldn't be able to do this.

Now ask a third student to join them, and ask them if they can do the swap now that they have a third person, and obviously they can by handing the first object to the new person, passing the second object, and then the new person gives back the first object.

You have now illustrated to the class why a temporary variable is needed when swapping two values in a computer program.

Materials needed: two objects (e.g. tennis balls)


Submitted by Dianne Martin, Liz Adams, Christine Mangnall (SIGCSE 97) on Wednesday, August 27, 1997 at 15:12:30 EDT/EST.


After you have tried this activity, you can add feedback on how it worked for you

You can also read the feedback of others who tried this activity


Return to the Activities List