Difference between revisions of "C: bsearch"

From AdonaiMedrado.Pro.Br
Jump to: navigation, search
(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...)
 
(No difference)

Latest revision as of 02:00, 17 April 2009

#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;
}