Swap Three Numbers
Learning Objectives
- Perform 3-variable cyclic swaps accurately.
- Avoid assignment-overwrite mistakes.
- Trace value movement step by step.
Concept Explanation
For three values A, B, C, swapping is usually done as rotation:
- Clockwise:
A->B,B->C,C->A - Anti-clockwise:
A->C,C->B,B->A
Clockwise Algorithm
temp = AA = CC = BB = temp
Example
Initial: A=1, B=2, C=3
Clockwise result: A=3, B=1, C=2
Common Mistakes
- No temp variable, causing data loss.
- Defining rotation goal incorrectly.
- Forgetting to print both before/after values.
Practice Tasks
- Implement clockwise rotation.
- Implement anti-clockwise rotation.
- Extend to rotate 4 values.
Summary
Define target mapping first, then write assignments.