Created
April 14, 2024 10:42
-
-
Save 3dgoose/6d76070a5bdbd22b7e2d951d8f5a1b7a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#define TAILLE_MIN 5 | |
#define TAILLE_MAX 25 | |
int main() { | |
int N, i, count, majorite; | |
int T[TAILLE_MAX]; | |
printf("Saisir la taille du tableau N (entre %d et %d) : ", TAILLE_MIN, TAILLE_MAX); | |
scanf("%d", &N); | |
while (N < TAILLE_MIN || N > TAILLE_MAX) { | |
printf("Erreur : la valeur de N doit être comprise entre %d et %d.\n", TAILLE_MIN, TAILLE_MAX); | |
printf("Saisir la taille du tableau N (entre %d et %d) : ", TAILLE_MIN, TAILLE_MAX); | |
scanf("%d", &N); | |
} | |
// Remplissage du tableau T | |
printf("Remplir le tableau T par %d entiers :\n", N); | |
for (i = 0; i < N; i++) { | |
printf("T[%d] = ", i + 1); | |
scanf("%d", &T[i]); | |
} | |
// Recherche de l'élément majoritaire | |
majorite = T[0]; | |
count = 1; | |
for (i = 1; i < N; i++) { | |
if (T[i] == majorite) { | |
count++; | |
} else { | |
count--; | |
if (count < 0) { | |
majorite = T[i]; | |
count = 1; | |
} | |
} | |
} | |
// Vérification et affichage du résultat | |
if (count > 0) { | |
printf("%d est un élément majoritaire dans le tableau.\n", majorite); | |
} else { | |
printf("Le tableau ne possède pas d'élément majoritaire.\n"); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment