DEV_BUT1/DEV1.1S/CM3/recherche.c

45 lines
944 B
C
Raw Normal View History

2023-02-08 11:18:16 +01:00
#include <stdlib.h>
#include <stdio.h>
int rechercheValeur(int valeur, int* tableau, int taille){
int n= -1;
if (taille == 0){
if (valeur == tableau[taille]){
n = taille;
}
return n;
}
if (valeur == tableau[taille]){
n = taille;
return n;
}
return rechercheValeur(valeur, tableau , taille-1);
}
int main(int argc, char const *argv[])
{
int test, test1, test2, test3, test4;
int oui = 5;
int tab1[5] = {3, 7 ,4 ,8, 9};
int tab2[5] = {6, 7 ,4 ,8, 9};
int tab3[5] = {2, 7 ,4 ,8, 6};
int tab4[5] = {6, 7 ,6 ,6, 9};
test = rechercheValeur(6, tab1 , oui);
test2 = rechercheValeur(6, tab2, oui);
test3 = rechercheValeur(6, tab3, oui);
test4 = rechercheValeur(6, tab4, oui);
printf("Test vide : ");
printf("%d\n", test);
printf("Test 1 fois au début : ");
printf("%d\n", test2);
printf("Test 1 fois a la fin : ");
printf("%d\n", test3);
printf("Test 3 fois au total : ");
printf("%d\n", test4);
return 0;
}