Permutation function in C
Content
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.
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
6You have entered:
a = 3
b = 6After 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
6You have entered:
a = 3
b = 6After 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
6You have entered:
a = 3
b = 6After 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; }
Or too! Now you know
Through this site you explain why PES
int temp = a;
a = b;
b = temp;
it is 3 command to change the value of a and b to each other through temp. like having 2 A glass of water, B containing lemon and want, want more interchangeable they should not contain anything that cup temp.
So ask yourself what int temp is declared so ạk
is declared integer variable, like int a, int b reviews.
Yes, let me ask if the test requires me to permute 5 can I still use even an integer variable?
yeah you.
hey can i get acquainted with , Thanks for making friends with me through Zalo to ask me to share my experience with.sdt .0966771824.looking forward to you a kb