Pages

Problème fonction recherche sujet

mercredi 26 mars 2014




Bonjour,

j'ai un petit problème avec un exercice sur les fonctions:

ce que je cherche à faire:

écrire la fonction (rechdicho) qui détermine si une valeur donnée appartient à un tableau donné à une dimension d’entiers. Le tableau est supposé trié. Si la valeur se trouve dans le tableau, la fonction renvoie sa position


Code:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48


#include <stdio.h>
#include <stdlib.h>

#define TAILLE_TAB 10

/*
*
*/
void rechdicho(int val, int *tab, int taille){
int compteur, pos;

if (val == tab[taille/2]) {
pos = taille/2;
}
else if (val < tab[taille/2]) {
for (compteur=0; compteur <= taille/2; compteur++){
if (val == tab[compteur]){
pos = compteur;
}
}
}
else {
for (compteur=taille/2; compteur <= taille-1; compteur++){
if (val == tab[compteur]){
pos = compteur;
}
}
}
return (pos);
}

int main(int argc, char** argv) {
int position;
int tableau[TAILLE_TAB] = {1,3,5,6,7,8,9,11,44,55};
int val;
char MSG_USERS1[255] = "Encodez une valeur: ";
char MSG_USERS2[255] = "La position de votre valeur dans le tableau est: ";

printf ("%s",MSG_USERS1);
scanf ("%d", &val);

position = rechdicho(val, tableau, TAILLE_TAB);
printf ("%s",MSG_USERS1);
printf ("%d", position);

}






Aucun commentaire:

Enregistrer un commentaire