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

Swapping numbers in a cyclic order is a common operation in programming, where each variable takes the value of the next one in a circular fashion. In C, this can be efficiently done using call by reference. This article explores three different C Program to Swap Numbers in Cyclic Order Using Call by Reference, providing detailed explanations, example code, and outputs for each.

Prerequisites

Before diving into the solutions, make sure you have the following prerequisites:

  • Basic understanding of C programming.
  • Familiarity with functions and pointers in C.
  • A C compiler installed on your system (e.g., GCC).

Solution 1: Simple Cyclic Swap

1.1 Explanation

In this approach, we will create a function that takes three pointers as arguments and swaps the values in a cyclic order: a takes the value of b, b takes the value of c, and c takes the value of a.

1.2 Program

C
#include <stdio.h>

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

int main() {
    int a = 1, b = 2, c = 3;

    printf("Before swapping: a = %d, b = %d, c = %d\n", a, b, c);
    cyclicSwap(&a, &b, &c);
    printf("After swapping: a = %d, b = %d, c = %d\n", a, b, c);

    return 0;
}

1.3 Output

C
Before swapping: a = 1, b = 2, c = 3
After swapping: a = 2, b = 3, c = 1

Solution 2: Cyclic Swap Using Arrays

2.1 Explanation

In this solution, we will use an array to store the values and perform the cyclic swap. This approach simplifies the code when dealing with more than three variables.

2.2 Program

C
#include <stdio.h>

void cyclicSwap(int arr[], int n) {
    int temp = arr[0];
    for (int i = 0; i < n - 1; i++) {
        arr[i] = arr[i + 1];
    }
    arr[n - 1] = temp;
}

int main() {
    int arr[3] = {1, 2, 3};

    printf("Before swapping: ");
    for (int i = 0; i < 3; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");

    cyclicSwap(arr, 3);

    printf("After swapping: ");
    for (int i = 0; i < 3; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");

    return 0;
}

Output

C
Before swapping: 1 2 3
After swapping: 2 3 1

Solution 3: Cyclic Swap Using a Struct

3.1 Explanation

In this approach, we will use a struct to group the variables together and pass the struct by reference to the function. This method provides a more organized way to handle multiple related variables.

3.2 Program

C
#include <stdio.h>

typedef struct {
    int a;
    int b;
    int c;
} Numbers;

void cyclicSwap(Numbers *num) {
    int temp = num->a;
    num->a = num->b;
    num->b = num->c;
    num->c = temp;
}

int main() {
    Numbers num = {1, 2, 3};

    printf("Before swapping: a = %d, b = %d, c = %d\n", num.a, num.b, num.c);
    cyclicSwap(&num);
    printf("After swapping: a = %d, b = %d, c = %d\n", num.a, num.b, num.c);

    return 0;
}

Output

C
Before swapping: a = 1, b = 2, c = 3
After swapping: a = 2, b = 3, c = 1

Conclusion

Using C Program to Swap Numbers in Cyclic Order Using Call by Reference can be accomplished through various methods. The simple cyclic swap approach is straightforward and easy to understand for a small number of variables. Using arrays simplifies the process when dealing with a larger set of variables, making the code more scalable. The struct-based approach organizes related variables into a single entity, making the code more readable and maintainable.