C: bsearch

From AdonaiMedrado.Pro.Br
Jump to: navigation, search
#include <stdio.h>
#include <stdlib.h>
#define TAMANHO_VETOR 10
 
int comparar(const void *a, const void *b)
{
	return *(int *)a-*(int *)b;
}
 
int main(void)
{
	int i, v[TAMANHO_VETOR];
	int n;
	int *res;
	for(i=0;i<TAMANHO_VETOR;i++)
		scanf("%d",v+i);
 
	scanf("%d",&n);
 
	qsort(v,TAMANHO_VETOR,sizeof(int),comparar);
	res=(int *)bsearch((void *)&n,v,TAMANHO_VETOR,sizeof(int),comparar);
	printf("%s\n",res==NULL ? "N" : "S");
 
	return 0;
}