Occurence maximum tableau
Bonsoir j'ai fait un petit algorithme en C++ pour pouvoir trouver l’occurrence d'une valeur maximale dans un tableau sauf que ça me renvoie toujours 0 je ne comprends pas si quelqu'un pouvait m'aider merci d'avance. (Exemple : T[7] ={ 1 , 1 ,3 , 3, 3 , 3 ,3 ,4}; ça doit renvoyer 3
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| int MaxOccurence(int *T, int n){
int cpt1 = 1;
int res = 0;
int maxcpt = 1;
for (int i=1; i<n; i++){
if (T[i] == T[i-1]) {
cpt1 = cpt1+1;
} else {
if (cpt1 > maxcpt) {
maxcpt = cpt1;
res = T[i-1];
}
}
cpt1 = 1;
}
if(cpt1 > maxcpt) {
maxcpt = cpt1;
res = T[n-1];
}
return res;
} |