tp2 exo 1-4

This commit is contained in:
Arkitu 2025-09-23 16:10:17 +02:00
parent 25d910abee
commit 54b971cd87
4 changed files with 69 additions and 0 deletions

16
tp2/exo1.c Normal file
View File

@ -0,0 +1,16 @@
#include <stdio.h>
#include <stdlib.h>
void f(int *a, int *b) {
*a = *a ^ *b;
*b = *a ^ *b;
*a = *a ^ *b;
}
int main() {
int a = 94;
int b = 247;
printf("Before swap : a=%d | b=%d\n", a, b);
f(&a, &b);
printf("After swap : a=%d | b=%d\n", a, b);
}

15
tp2/exo2.c Normal file
View File

@ -0,0 +1,15 @@
#include <stdio.h>
#include <stdlib.h>
int f(int t[]) {
t[0] = t[0] ^ t[1];
t[1] = t[0] ^ t[1];
t[0] = t[0] ^ t[1];
}
int main() {
int t[3] = {3, 6, 7};
printf("t = {%d, %d, %d}\n", t[0], t[1], t[2]);
f(t);
printf("t = {%d, %d, %d}\n", t[0], t[1], t[2]);
}

19
tp2/exo3.c Normal file
View File

@ -0,0 +1,19 @@
#include <stdio.h>
#include <stdlib.h>
void f(int t[], uint len) {
uint i = 0;
while (i + 1 < len) {
t[i] = t[i] ^ t[i + 1];
t[i + 1] = t[i] ^ t[i + 1];
t[i] = t[i] ^ t[i + 1];
i++;
}
}
int main() {
int t[3] = {3, 6, 7};
printf("t = {%d, %d, %d}\n", t[0], t[1], t[2]);
f(t, 3);
printf("t = {%d, %d, %d}\n", t[0], t[1], t[2]);
}

19
tp2/exo4.c Normal file
View File

@ -0,0 +1,19 @@
#include <stdio.h>
#include <stdlib.h>
int f(int t[], uint len, int x) {
uint i = 0;
while (i < len) {
if (t[i] == x) {
return i;
}
i++;
}
return -1;
}
int main() {
int t[3] = {3, 6, 7};
printf("emplacement de 6 : %d\n", f(t, 3, 6));
printf("emplacement de 4 : %d\n", f(t, 3, 4));
}