Wednesday, 27 April 2016

Number Swapping

/*
 * C Program to swap two numbers.
 */

// Includes
#include <stdio.h>

/*
 * Description : Function to swap 2 integers using call by reference.
 * Parameters  : iPtr_num1, iPtr_num2 - pointer to numbers to be swapped.
 * Returns     : Nothing. Swapped numbers are directly reflected in calling function.
 */
void swap(int *iPtr_num1, int *iPtr_num2)
{
    float temp;
    
    // Check if both are pointing to same location.
    // No need to swap in such scenario.
    if (iPtr_num1 == iPtr_num2)
    {
        return;
    }
    
    // Uses temporary variable to swap 2 numbers.
    temp = *iPtr_num1;
    *iPtr_num1 = *iPtr_num2;
    *iPtr_num2 = temp;
}

/*
 * Description : Function to swap 2 integers using addition and subtraction.
 * Parameters  : iPtr_num1, iPtr_num2 - pointer to numbers to be swapped.
 * Returns     : Nothing. Swapped numbers are directly reflected in calling function.
 */
void swapUsingAddSub(int *iPtr_num1, int *iPtr_num2)
{
    // Check if both are pointing to same location.
    // No need to swap in such scenario.
    if (iPtr_num1 == iPtr_num2)
    {
        return;
    }
    
    // Swap using addition and subtraction method
    *iPtr_num1 = *iPtr_num1 + *iPtr_num2;
    *iPtr_num2 = *iPtr_num1 - *iPtr_num2;
    *iPtr_num1 = *iPtr_num1 - *iPtr_num2;
}

/* swapUsingBitwise
 * Description : Function to swap 2 integers using bitwise operators.
 * Parameters  : iPtr_num1, iPtr_num2 - pointer to numbers to be swapped.
 * Returns     : Nothing. Swapped numbers are directly reflected in calling function.
 */
void swapUsingBitwise(int *iPtr_num1, int *iPtr_num2)
{
    // Check if both are pointing to same location.
    // No need to swap in such scenario.
    if (iPtr_num1 == iPtr_num2)
    {
        return;
    }
    
    // Swap using bitwise operators
    *iPtr_num1 = *iPtr_num1 ^ *iPtr_num2;
    *iPtr_num2 = *iPtr_num1 ^ *iPtr_num2;
    *iPtr_num1 = *iPtr_num1 ^ *iPtr_num2;
}

/* swapUsingMultDiv
 * Description : Function to swap 2 integers using multiplication and division.
 * Parameters  : iPtr_num1, iPtr_num2 - numbers to be swapped.
 * Returns     : Nothing. Swapped numbwes are directly reflected in calling function.
 */
void swapUsingMultDiv(float *iPtr_num1, float *iPtr_num2)
{
    // Check if both are pointing to same location.
    // No need to swap in such scenario.
    if (iPtr_num1 == iPtr_num2)
    {
        return;
    }
    
    // Swap using multiplication and division method
    *iPtr_num1 = (*iPtr_num1) * (*iPtr_num2);
    *iPtr_num2 = (*iPtr_num1) / (*iPtr_num2);
    *iPtr_num1 = (*iPtr_num1) / (*iPtr_num2);
}

int main()
{
    int i_num1, i_num2;
    float f_num1, f_num2;
    
    printf("Enter numbwes to be swapped : ");
    scanf("%d %d",&i_num1, &i_num2);
    
    // Swap 2 numbers and print
    swap(&i_num1, &i_num2);
    printf("Swapped                   (%d,%d)\n",i_num1, i_num2);
    
    swapUsingAddSub(&i_num1, &i_num2);
    printf("Swapped using add-sub     (%d,%d)\n",i_num1, i_num2);
    
    swapUsingBitwise(&i_num1, &i_num2);
    printf("Swapped using bitwise xor (%d,%d)\n",i_num1, i_num2);
    
    // For multiplication-division method, arguments needs to be float
    f_num1 = i_num1;    // implicit type casting will take place.
    f_num2 = i_num2;    // implicit type casting will take place.
    swapUsingMultDiv(&f_num1, &f_num2);
    printf("Swapped using mult-div    (%g,%g)\n",f_num1, f_num2);

}