diff --git a/README.md b/README.md index a37ba0a..f5a7fba 100644 --- a/README.md +++ b/README.md @@ -1,2 +1 @@ # mp2i-info - diff --git a/tp2/exo5.c b/tp2/exo5.c new file mode 100644 index 0000000..de04e8b --- /dev/null +++ b/tp2/exo5.c @@ -0,0 +1,16 @@ +#include +#include + +int f(int t[], int len, int x) { + int count = 0; + while (len >= 0) { + count += t[len] == x; + len--; + } + return count; +} + +int main() { + int t[8] = {3, 6, 7, 6, 9, 9, 5, 9}; + printf("nombre de 9 : %d\n", f(t, 8, 9)); +} diff --git a/tp2/exo6.c b/tp2/exo6.c new file mode 100644 index 0000000..049ba65 --- /dev/null +++ b/tp2/exo6.c @@ -0,0 +1,42 @@ +#include +#include + +void f(int t[], uint len) { + while (len > 1) { + uint i = 0; + while (i + 1 < len) { + if (t[i] > t[i + 1]) { + t[i] = t[i] ^ t[i + 1]; + t[i + 1] = t[i] ^ t[i + 1]; + t[i] = t[i] ^ t[i + 1]; + } + i++; + } + len--; + } +} + +void print_t(int t[], uint len) { + if (len == 0) { + printf("[]"); + return; + } + uint i = 1; + printf("[%d", t[0]); + while (i < len) { + printf(", %d", t[i]); + i++; + } + printf("]"); +} + +int main() { + int t[9] = {2, 9, 4, 7, 5, 3, 6, 1, 8}; + printf("Unsorted : "); + print_t(t, 9); + printf("\n"); + f(t, 9); + printf("Sorted : "); + print_t(t, 9); + printf("\n"); +} \ No newline at end of file diff --git a/tp2/exo7.c b/tp2/exo7.c new file mode 100644 index 0000000..71e3acf --- /dev/null +++ b/tp2/exo7.c @@ -0,0 +1,44 @@ +#include +#include + +// Expect t to be a non empty list of columns. Expect res to be of size 2 +void f(int *t, uint rows, uint cols, int res[2]) { + int i = (rows * cols) - 1; + int i_max = i; + while (i > 0) { + i--; + if (t[i] >= t[i_max]) { + i_max = i; + } + } + res[0] = i_max % cols; + res[1] = i_max / cols; +} + +void print_t(int t[], uint len) { + if (len == 0) { + printf("[]"); + return; + } + uint i = 1; + printf("[%d", t[0]); + while (i < len) { + printf(", %d", t[i]); + i++; + } + printf("]"); +} + +int main() { + int t[][3] = {{2, 9, 4}, {7, 5, 3}, {6, 1, 8}}; + int res[2]; + f(t[0], 3, 3, res); + + printf("t = [\n"); + print_t(t[0], 3); + printf(",\n"); + print_t(t[1], 3); + printf(",\n"); + print_t(t[2], 3); + printf("\n]\nMaximum : (%d, %d)\n", res[0], res[1]); +} \ No newline at end of file