Hàm hoán vị trong c

     
*

1. Thought permutation

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

Bạn đang xem: Hàm hoán vị trong c

To permutations 2 number, One can imagine lượt thích us 2 the cup. A glass of lemonade container, B cup pesticide containers. How vày 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 lemonB2: Pour pesticide B cup khổng lồ cup A => B empty, A pesticideB3: Pour into mug cup lemon C B => B contains lemon.Ok. A pesticide hours, B containing lemonade.

*

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 int main() int a, b; printf("Nhap 2 so a, b: "); scanf("%d%d", &a, &b); printf("Ban domain authority nhap: a = %d b = %d ", a, b); int temp = a; a = b; b = temp; printf("Sau khi hoan vi: a = %d b = %d ", 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 void hoanvi(int a, int b) int temp = a; a = b; b = temp;int main() int a, b; printf("Nhap 2 so a, b: "); scanf("%d%d", &a, &b); printf("Ban da nhap: a = %d b = %d ", a, b); hoanvi(a, b); printf("Sau lúc goi si mê hoanvi: a = %d b = %d ", 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 bởi vì not have results permutation?

With this program we built 1 permutation function to lớn 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.

Xem thêm: Đọc Truyện Vương Phi Đa Tài Đa Nghệ Lương Vương Phi Đa Tài Đa Nghệ

You understand the value passed by reference ie the function gọi Honvi(the, b) immediately the value of a và 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 void hoanvi(int *a, int *b) int temp = *a; *a = *b; *b = temp;int main() int a, b; printf("Nhap 2 so a, b: "); scanf("%d%d", &a, &b); printf("Ban da nhap: a = %d b = %d ", a, b); hoanvi(&a, &b); printf("Sau lúc goi đê mê hoanvi: a = %d b = %d ", 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 & *b int. *a và *b ie pointers a và 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 và to take the address of variable a và 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 void hoanvi(int &a, int &b) // only in C++, tệp tin .cpp int temp = a; a = b; b = temp;int main() int a, b; printf("Nhap 2 so a, b: "); scanf("%d%d", &a, &b); printf("Ban domain authority nhap: a = %d b = %d ", a, b); hoanvi(a, b); printf("Sau khi goi mê man hoanvi: a = %d b = %d ", a, b); return 0;Or can use the function swap available in the library algorithm

#include #include // swap int main() int a, b; printf("Nhap 2 so a, b: "); scanf("%d%d", &a, &b); printf("Ban domain authority nhap: a = %d b = %d ", a, b); std::swap(a, b); printf("Sau lúc goi đắm say hoanvi: a = %d b = %d ", a, b); return 0;