Vous devez vous connecter pour exécuter votre code.
Hoare 8 — Inversion de tableau
Écrivez le code C d'une fonction qui inverse un tableau sur place avec annotations Hoare.
Invariant attendu :
0 <= i <= j+1 <= n && t[0..i-1] et t[j+1..n-1] sont inversés
Quantité de contrôle attendue :
j - i + 1
#include <stdio.h>
// @pre: n >= 0
// @post: t est inversé (t[i] = ancien t[n-1-i])
void inverser(int t[], int n) {
int i = 0, j = n - 1;
// @invariant: 0 <= i <= j+1 <= n && t[0..i-1] et t[j+1..n-1] sont les miroirs
// @variant: j - i + 1
while (i < j) {
int tmp = t[i];
t[i] = t[j];
t[j] = tmp;
i = i + 1;
j = j - 1;
}
}
int main() {
int n, t[1000];
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d", &t[i]);
inverser(t, n);
for (int i = 0; i < n; i++) printf("%d ", t[i]);
printf("\n");
return 0;
}