sábado, 6 de septiembre de 2014

14. Que rellene un array con 20 números y luego busque un número concreto.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{

    int  i,x=0,vector[20], n=20, dato, centro,inf=0,sup=n-1;
  
    for (i=0;i<20;i++){
        printf("Escriba un número");
        scanf("%d",&vector[i]);
    }
  
    printf("Escriba el número a buscar");
    scanf("%d",&dato);
  
    while(inf<=sup)
    {
     centro=(sup+inf)/2;
     if (vector[centro]==dato)
     {
        printf("Existe\n");
        x=1;
        break;
     } 
     else if(dato < vector [centro] )
     {
        sup=centro-1;
     }
     else
     {
       inf=centro+1;
     }
    }
   
    if (x==0)
    {
       printf("No existe\n");     
    }
  
    system("PAUSE");     
    return 0;


}

4 comentarios:

  1. Como puedo hacer este programa en idle python

    ResponderEliminar
  2. JackpotCity Casino NJ - Use Code JMKHub for $1K Bonus
    Join now and get $1000 in free BONUS! Play Slots and table games 밀양 출장안마 and win real 남원 출장마사지 money! Sign 출장안마 up and earn FREECREDIT for a 100% match up 논산 출장안마 to 오산 출장샵 $1000!

    ResponderEliminar