Write a C++ Program to Swap Numbers in Cyclic Order Using Call by Reference

In C++, we can swap two numbers in a cyclic order using call by reference. Here is a simple program that demonstrates this:

#include<iostream>
using namespace std;

void cyclicSwap(int &a, int &b, int &c){
    int temp = a;
    a = c;
    c = b;
    b = temp;
}

int main(){
    int a, b, c;
    cout << "Enter three numbers: ";
    cin >> a >> b >> c;
    cout << "Before swapping: " << a << " " << b << " " << c << endl;
    cyclicSwap(a, b, c);
    cout << "After swapping: " << a << " " << b << " " << c << endl;
    return 0;
}

Here, we have defined a function cyclicSwap that takes three integer arguments by reference.

Inside the function, we first store the value of a in a temporary variable temp.

Then, we assign the value of c to a, the value of b to c, and finally, the value of temp (which is the original value of a) to b. This effectively swaps the values of a, b, and c in a cyclic order.

In the main function, we take input from the user for three numbers and display them before swapping.

Then, we call the cyclicSwap function with the three numbers as arguments. After the function call, we display the swapped values of a, b, and c.

Note that we have used call by reference instead of call by value.

This means that the original values of a, b, and c will be modified by the cyclicSwap function, and these modified values will be reflected in the main function as well.

If we had used call by value instead, the function would have worked on copies of the values of a, b, and c, and the original values would not have been modified.

In conclusion, the above program demonstrates how to swap two numbers in a cyclic order using call by reference in C++.