Initial commit of Polytech'Marseille tutorial C exercices
[TD_C.git] / exo1 / exo1.c
1 #include <stdio.h>
2 #include <stdbool.h>
3
4
5 void promptValue(const char* msg, int* addr) {
6 puts(msg);
7 scanf("%d", addr);
8 }
9
10 void xorSwap (int *v1, int *v2) {
11 if (v1 != v2) {
12 *v1 ^= *v2;
13 *v2 ^= *v1;
14 *v1 ^= *v2;
15 }
16 }
17
18 void swap(int* v1, int* v2) {
19 int tmp = *v1;
20 if (v1 != v2) {
21 *v1 = *v2;
22 *v2 = tmp;
23 }
24 }
25
26 void displayArray(int* array, int count) {
27 for (int i = 0; i < count; i++) {
28 printf("Value in array index %d = %d\n", i, array[i]);
29 }
30 }
31
32 bool sortFirst(int* array, int length) {
33 for (int i = 0; i < length-1; i++) {
34 if (array[i] > array[i+1]) {
35 swap(&array[i], &array[i+1]);
36 return true;
37 }
38 }
39 return false;
40 }
41
42 int main() {
43 int array[5];
44 for (int i = 0; i < 5; i++) {
45 promptValue("Valeur ?", &array[i]);
46 }
47 displayArray(array, 5);
48
49 return 0;
50 }