C: bsearch

From AdonaiMedrado.Pro.Br
Revision as of 02:00, 17 April 2009 by Adonaimedrado (Talk | contribs) (New page: <code lang="c"> #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[TAM...)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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;
}