Permutation function in C

1. Thought permutation

In reality, we must build the program, permutation functions to swap the value of the element, eg sequencer program is a typical.

To permutations 2 number, One can imagine like us 2 the cup. A glass of lemonade container, B cup pesticide containers. How do we move into the cup lemon juice B and moved to cup a pesticide? Simply use more 1 women's cup c cup and started moving:

  • B1: Pour cup of lemon A to C cup. => A hollow, C contains lemon
  • B2: Pour pesticide B cup to cup A => B empty, A pesticide
  • B3: Pour into mug cup lemon C B => B contains lemon.
  • Ok. A pesticide hours, B containing lemonade.

permutation

How swapped 2 glass of water

2. Permutation code

Do the same in the programming we will swap the values ​​of 2 variable.

// e.g about swap in C - code by nguyenvanquan7826
#include <stdio.h>

int main()
{
    int a, b;
    printf("Nhap 2 so a, b:\n");
    scanf("%d%d", &a, &b);

    printf("Ban da nhap:\na = %d \nb = %d\n", a, b);

    int temp = a;
    a = b;
    b = temp;

    printf("Sau khi hoan vi:\na = %d\nb = %d\n", a, b);
    return 0;
}

Result:

Import 2 so a, b:
3
6

You have entered:
a = 3
b = 6

After permutation:
a = 6
b = 3

Ok. Now try to split into permutation function see stars:

3. Permutation function in C

// e.g about swap in C - code by nguyenvanquan7826
#include <stdio.h>

void hoanvi(int a, int b) 
{
    int temp = a;
    a = b;
    b = temp;
}

int main()
{
    int a, b;
    printf("Nhap 2 so a, b:\n");
    scanf("%d%d", &a, &b);

    printf("Ban da nhap:\na = %d \nb = %d\n", a, b);

    hoanvi(a, b);

    printf("Sau khi goi ham hoanvi:\na = %d\nb = %d \n", a, b);
    return 0;
}

Result:

Import 2 so a, b:
3
6

You have entered:
a = 3
b = 6

After the call hoanvi:
a = 3
b = 6

Oh, Why we do not have results permutation?

With this program we built 1 permutation function to change positions between 2 terminals a and b, however, we have used the value should take the values ​​of a and b does not change, or in other words, they are not the same permutation.

You understand the value passed by reference ie the function call Honvi(the, b) immediately the value of a and b (3 and 6) be included in the function, not the variable a, b should turn a, b's we did not change when the function ends.

Correct code is as follows:

// e.g about swap in C - code by nguyenvanquan7826
#include <stdio.h>

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

int main()
{
    int a, b;
    printf("Nhap 2 so a, b:\n");
    scanf("%d%d", &a, &b);

    printf("Ban da nhap:\na = %d \nb = %d\n", a, b);

    hoanvi(&a, &b);

    printf("Sau khi goi ham hoanvi:\na = %d\nb = %d \n", a, b);
    return 0;
}

Result:

Import 2 so a, b:
3
6

You have entered:
a = 3
b = 6

After permutation:
a = 6
b = 3

Above we build with jaw hoanvi 2 argument is *a and *b int. *a and *b ie pointers a and b pointer. In the body of the function we write *a, *b (CEO: int temp = *a) then sign * represents the value of the pointer a.

Because the function pointer should use when calling us to transmit the address of the variables ie hoanvi(&a, &b) , here sign & to take the address of variable a and variable b.

4. C ++ function permutations in

If you write C ++ (files ending with .cpp) they can write content a little easier permutations as follows.

// e.g about swap in C - code by nguyenvanquan7826
#include <stdio.h>

void hoanvi(int &a, int &b)  // only in C++, file .cpp
{
    int temp = a;
    a = b;
    b = temp;
}

int main()
{
    int a, b;
    printf("Nhap 2 so a, b:\n");
    scanf("%d%d", &a, &b);

    printf("Ban da nhap:\na = %d \nb = %d\n", a, b);

    hoanvi(a, b);

    printf("Sau khi goi ham hoanvi:\na = %d\nb = %d \n", a, b);
    return 0;
}

Or can use the function swap available in the library algorithm

#include <stdio.h>
#include <algorithm> // swap 

int main()
{
    int a, b;
    printf("Nhap 2 so a, b:\n");
    scanf("%d%d", &a, &b);

    printf("Ban da nhap:\na = %d \nb = %d\n", a, b);

    std::swap(a, b);

    printf("Sau khi goi ham hoanvi:\na = %d\nb = %d \n", a, b);
    return 0;
}