Table of Contents
Swap function
A swap function is a function that allows you to exchange the values of two variables. It takes two variables as input and swaps their values. The purpose of a swap function is to provide a convenient and reusable way to perform the swapping operation.
How to swap
Swapping the values of two variables using a third variable involves the following concept:
- Declare the variables: Start by declaring the two variables that you want to swap, let’s call them
variable1
andvariable2
. Assign the initial values to these variables. - Display initial values (optional): You can choose to display the initial values of the variables before swapping to verify their current values.
- Declare a third variable: Declare a third variable, commonly named
temp
, to temporarily hold the value of one of the variables during the swapping process. - Assign value of variable1 to temp: Assign the value of
variable1
totemp
. This step ensures that the value ofvariable1
is saved before it gets overwritten during the swapping process. - Assign value of variable2 to variable1: Assign the value of
variable2
tovariable1
. Now, bothvariable1
andvariable2
hold the same value. - Assign value of temp to variable2: Assign the value of
temp
tovariable2
. This step effectively swaps the value ofvariable2
with the original value ofvariable1
that was saved intemp
. - Display swapped values (optional): Finally, you can choose to display the swapped values of the variables to confirm that the swapping process was successful.
C program that swaps the values of two variables using a third variable:
Example in c
#include<stdio.h> void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } int main() { int x = 10; int y = 20; printf("Before swapping:\n"); printf("x = %d\n", x); printf("y = %d\n", y); swap(&x, &y); printf("\nAfter swapping:\n"); printf("x = %d\n", x); printf("y = %d\n", y); return 0; }
Output
Before swapping: x = 10 y = 20 After swapping: x = 20 y = 10
In this example, the swap
function takes two integer pointers (int *a
and int *b
) as parameters. It uses the same swapping logic we discussed earlier: it stores the value of *a
in a temporary variable, assigns the value of *b
to *a
, and then assigns the value of the temporary variable to *b
. By using pointers, the function can modify the values of the original variables passed as arguments.
In the main
function, two variables x
and y
are declared and assigned initial values. The swap
function is then called with the addresses of x
and y
(&x
and &y
) as arguments. This allows the function to directly modify the values of x
and y
, effectively swapping their values. Finally, the swapped values are displayed.
Example in python
# Swapping values of two variables using a third variable # Input values a = 10 b = 20 # Displaying initial values print("Before swapping:") print("a =", a) print("b =", b) # Swapping logic temp = a a = b b = temp # Displaying swapped values print("\nAfter swapping:") print("a =", a) print("b =", b)
Output
Before swapping: a = 10 b = 20 After swapping: a = 20 b = 10
In this program, we use a third variable called temp
to temporarily store the value of one of the variables while swapping their values. The value of a
is stored in temp
, then b
is assigned to a
, and finally, the value of temp
is assigned to b
. This effectively swaps the values of a
and b
.This way, the values of a
and b
are successfully swapped using a third variable additionally .
- Understanding NP-Hard Problems and NP-Complete Problems
- Abstraction in Object-Oriented Programming (OOP) and System Design(lecture-5)
- Understanding Polymorphism in Object-Oriented System Design Using C++(lecture-6)
- Understanding Inheritance in Object-Oriented System Design Using C++(lecture-7)
- Understanding Classes and Objects in Object-Oriented System Design Using C++(lecture-3)