Computer Science, asked by jainvansh189, 1 year ago

1) Write a program in C++ that exchanges data (passing by reference) using swap function to
interchange the given two numbers. The program must print the two numbers before and after
swapping​

Answers

Answered by vickyMverma
6

Answer:

#include<iostream.h>

void swap(int &x,int &y)

{

int tmp=x;

x=y;

y=tmp;

}

void main()

{

int A=10,B=20;

cout<<"===before===";

cout<<A<<" "<<B;

swap(A,B);

cout<<"===After===";

cout<<A<<" "<<B;

}

Explanation:

Answered by VineetaGara
0

There are 5 ways to swap two numbers in C++ :

  1. Using a third variable.
  2. Using swap() function.
  3. Using Bitwise Operator.
  4. Using Friend Function.
  5. Short Way

Let’s start discussing each of these methods in detail.

1. Using 3rd Variable : The idea is simple in this approach:

Assign a to a temp variable: temp = a

Assign b to a: a = b

Assign temp to b: b = temp

Below is the C++ program to implement the above approach:

// C++ program to swap two

// numbers using 3rd variable

#include<bits/stdc++.h>

using namespace std;

// Driver code

int main()

{

   int a = 2, b = 3;

       cout << "Before swapping a = " <<

            a << " , b = " << b << endl;

   int temp;  

   temp = a;

   a = b;

   b = temp;

   cout << "After swapping a = " << a <<

           " , b = " << b << endl;

   return 0;

}

Output : Before swapping a = 2 , b = 3

              After swapping a = 3 , b = 2

2. Using swap() Function : This approach uses the in-built swap() function to swap the two variables a and b. Below is the C++ program to implement the above approach:

// C++ program to swap two

// numbers using swap()

// function

#include<bits/stdc++.h>

using namespace std;

// Driver code

int main()

{

   int a = 2, b = 3;

    cout << "Before swapping a = " <<

            a << " , b = " << b << endl;

          // Built-in swap function

   swap(a, b);

   cout << "After swapping a = " <<

            a << " , b = " << b << endl;

   return 0;

}

Output : Before swapping a = 2 , b = 3

              After swapping a = 3 , b = 2

3. Using Bitwise Operator : The following approach will be used here:

Assign to “a” the XOR of “a” and “b” i.e. a = a ^ b.

Assign to “b” the XOR of “b” and “a” i.e. b = b ^ a.

Assign to b the difference of “b” and “a” i.e. a = a ^ b.

Below is the C++ program to implement the above approach :

// C++ program to swap two

// numbers using Bitwise Operator.

#include<bits/stdc++.h>

using namespace std;

// Driver code

int main()

{

   int a = 7, b = 3;

      cout << "Before swapping a = " <<

            a << " , b = " << b << endl;

   a = a ^ b;

   b = b ^ a; // b = b ^ (a ^ b) = a

   a = a ^ b; // a = (a ^ b) ^ a = b

    cout << "After swapping a = " <<

            a << " , b = " << b << endl;

   return 0;

}

Output : Before swapping a = 7 , b = 3

               After swapping a = 3 , b = 7

4. Using Friend function : Declare three variables i.e., a, b, and temp in a class Swap , and create a constructor for inputs.

Declare a friend function in it.

Define the friend function (outside the class scope). This can be done by taking arguments as call by reference to pass the copy of Swap Object.

Perform the swap operation with swap variables, and swap values of a and b using temp variable.

Below is the C++ program to implement the above approach :

// C++ Program to swap two numbers using friend function

#include <iostream>

using namespace std;

class Swap {

// Declare the variables : temp, a and b

int temp, a, b;

public:

Swap(int a, int b) //define a method Swap

{

this->a = a;

this->b = b;

}

friend void swap(Swap&); //calling friend function

};

void swap(Swap& s1) // method for swapping values

{

cout << "\nBefore Swapping: " << s1.a << " " << s1.b;

s1.temp = s1.a;

s1.a = s1.b;

s1.b = s1.temp;

cout << "\nAfter Swapping: " << s1.a << " " << s1.b;

}

// Driver Code

int main()

{

// Declaring and Initializing the Swap object

Swap s(4, 6);

swap(s);

return 0;

}

Output : Before Swapping: 4 6

               After Swapping: 6 4

5.  Improved and short way : This is a simplified approach in which we can swap the values of two numbers in one line, using basic operators.

Below is the C++ program to implement the above approach :

// C++ program to swap two numbers using arithmetic operators.

#include<bits/stdc++.h>

using namespace std;

// Driver code

int main()

{

int a = 5, b = 8;

cout << "Values before swapping a = " <<

a << " , b = " << b << endl;

b = a - b + (a = b);

cout << "Values after swapping a = " << a <<

" , b = " << b << endl;

return 0;

}

Output : Values before swapping a = 5 , b = 8

              Values after swapping a = 8 , b = 5

#SPJ2

Similar questions