Advertisement
Kyrexar

Búsqueda secuencial

May 23rd, 2012
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.97 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define N 100
  5.  
  6. int prim ( int v[], int busq ){
  7.     int i;
  8.  
  9.     for( i=0 ; i<N ; i++ )
  10.         if ( v[i]==busq ){
  11.         printf(" La primera posición es %d \n ",i);
  12.         return i;
  13.     }
  14.  
  15.     printf(" Valor no encontrado \n ");
  16.     return -1;
  17. }
  18.  
  19. int last( int v[] , int busq){
  20.     int i;
  21.  
  22.     for( i=N ; i>=0 ; i-- )
  23.         if ( v[i]==busq ){
  24.         printf(" La ultima posición es %d \n ",i);
  25.         return i;
  26. }
  27.  
  28. void todo( int v[] ){
  29.     int i;
  30.  
  31.     printf(" Posiciones: ");
  32.     for( i=0 ; i<N ; i++ )
  33.         if ( v[i]==busq ){
  34.         printf(" %d  ",i);
  35.     printf(" \n ");
  36. }
  37.  
  38.  
  39. int main(){
  40.     FILE *f_in=fopen("datos.txt");;
  41.     int v[N], i, busq;
  42.  
  43.     while ( fscanf(f_in,"%d ",&v[i]) != EOF ) i++;
  44.  
  45.     printf(" \n Dime que valor buscas: ");
  46.     scanf("%d ",&busq);
  47.  
  48.     prim(v,busq);
  49.     if( prim(v,busq) != -1){
  50.         last(v,busq);
  51.         todo(v,busq);
  52.     }
  53.  
  54.     system("PAUSE");
  55.     return 0;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement