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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| #include <ctype.h>
#include <stdio.h>
#include <stdlib.h> /* nécessaire pour srand(n) et rand() */
#include <time.h>
#define NMAX 200001
void gen_aleat( double v[], int n )
{
int i;
/* rand() retourne un entier aléatoire de l'ens {0,1,2,..,RAND_MAX} (RAND_MAX fixé à 32767 sous VC++)*/
for(i = 0; i < n; i++) v[i] = ( rand() - rand() ) %32768;/* pour avoir aussi des nbrs<0 */
}
void aff_vect( double v[], int n )
{
int i;
for( i = 0; i < n; i++ )
{
printf( "%7.0f ", v[i] );
if ( (i+1) % 10 == 0 ) printf( "\n" );
}
printf( "\n" );
}
int chercheseq (double v[],int n,double e)
{
int i;
i = 0;
while (i < n)
{
if (v[i]==e) return (i);
i++;
}
return (-1);
}
int main()
{
double v[NMAX];
int n;
char rep;
int ok;
double e;
srand( time( NULL ) );
do
{
printf( "Entrer le nb d'elements du vecteur ( <= %d ) : ", NMAX );
ok = scanf( "%d", &n );
while( getchar( ) != '\n' ); /* ou fgets(vb,80,stdin) avec char vb[81]; */
} while( !ok || n < 0 || n > NMAX );
gen_aleat( v, n );
printf( "Voici le tableau genere :\n" );
aff_vect( v, n );
do
{
printf("Taper l'element a rechercher dans le tableau : ");
ok = scanf( "%d", &e );
while( getchar( ) != '\n' );
} while (!ok);
if (chercheseq(v,n,e)==-1) printf("L'element ne figure pas dans le tableau.");
else printf("L'indice de l'element recherche est %d",chercheseq(v,n,e));
return 0;
} |
Partager