Département de Mathématiques et Informatique
Licence 1 Informatique
INF131
Nom de l’étudiant: MONEGUEM WAFFO ANAELLE DELIANE
Matricule : 24S98725
Filière :INFORMATIQUE
Contact : 659740543
Sous supervision de Dr. Moskolai.
Ecrivons un programme en c qui effectue le classement d’un tableau
dans l’ordre croissant et décroissant
#include <stdio.h>
#include <stdlib.h>
#define MAX 1000
void croissant(int tab[], int n) {
int i, j, temp;
for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
if (tab[i] > tab[j]) {
temp = tab[i];
tab[i] = tab[j];
tab[j] = temp;
}
}
}
for (i = 0; i < n; i++) {
printf("--%d--", tab[i]);
}
}
void decroissant(int tab[], int n) {
int i, j, temp;
for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
if (tab[i] < tab[j]) {
temp = tab[i];
tab[i] = tab[j];
tab[j] = temp;
}
}
}
for (i = 0; i < n; i++) {
printf("--%d--", tab[i]);
}
}
int main() {
int choix, n = 0, i;
int tab[MAX];
printf("Entrer les éléments (saisir 0 pour terminer) :\n");
for (i = 0; i < MAX; i++) {
printf("Entrer l'élément %d : ", i + 1);
if (scanf("%d", &tab[i]) != 1) {
printf("Erreur de saisie. Veuillez réessayer.\n");
i--; // réessayer la saisie
}
if (tab[i] == 0) {
n = i;
break;
}
}
do {
printf("\n--- Menu ---\n");
printf("1: Trier dans l'ordre croissant\n");
printf("2: Trier dans l'ordre décroissant\n");
printf("3: Quitter\n");
printf("Choix : ");
scanf("%d", &choix);
switch (choix) {
case 1:
croissant(tab, n);
printf("\nTri effectué avec succès !\n");
break;
case 2:
decroissant(tab, n);
printf("\nTri effectué avec succès !\n");
break;
case 3:
printf("\nAu revoir !\n");
break;
default:
printf("\nChoix invalide. Veuillez réessayer.\n");
}
} while (choix != 3);
return 0;
}