tp2 exo5-7

This commit is contained in:
Arkitu 2025-09-24 22:47:17 +02:00
parent 54b971cd87
commit 1ad63a6e29
4 changed files with 102 additions and 1 deletions

View File

@ -1,2 +1 @@
# mp2i-info

16
tp2/exo5.c Normal file
View File

@ -0,0 +1,16 @@
#include <stdio.h>
#include <stdlib.h>
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));
}

42
tp2/exo6.c Normal file
View File

@ -0,0 +1,42 @@
#include <stdio.h>
#include <stdlib.h>
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");
}

44
tp2/exo7.c Normal file
View File

@ -0,0 +1,44 @@
#include <stdio.h>
#include <stdlib.h>
// 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]);
}