APL/APL1.1/TP11/recherche.c

54 lines
897 B
C
Raw Permalink Normal View History

2021-10-05 16:17:21 +02:00
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define TABSIZE 10
int main(int argc, char * argv[]) {
int tab[TABSIZE] = {};
srand(time(NULL));
int rand_v = rand();
for (int i = 0; i < TABSIZE; i++) {
srand(rand_v);
tab[i] = rand() % 101 > 50 ? (-rand() % 100) + 50: (rand() % 100) - 50;
rand_v = rand();
printf("+-----");
}
printf("+\n");
for (int i = 0; i < TABSIZE; i++) {
printf("| %3d ", tab[i]);
}
printf("|\n");
for (int i = 0; i < TABSIZE; i++) {
printf("+-----");
}
printf("+\n");
int value;
int index = -1;
printf("Veuillez entrer une valeur : ");
scanf("%d", &value);
for (int i = 0; i < TABSIZE; i++) {
if (tab[i] == value) {
index = i + 1;
break;
}
}
if (index != -1) {
printf("L'index n°%d contient votre nombre.\n", index);
} else {
printf("Le tableau ne contient pas votre nombre.\n");
}
return EXIT_SUCCESS;
}