Guest User

Untitled

a guest
Dec 16th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. ////////////////////////////////////////////////////////////////////////////////
  2. // Tecnologico de Costa Rica
  3. // Escuela de Ingenieria en Computación
  4. // Estructuras de Datos
  5. //
  6. // Edward Ovares Villegas
  7. ////////////////////////////////////////////////////////////////////////////////
  8.  
  9. /* Descripcion:
  10. Multi búsqueda: Escriba un método int[] multiBusqueda(int[] datos, string[] buscados)
  11. */
  12.  
  13. //--> defines and macros names
  14. #define cantElem(x) (sizeof(x)/sizeof(x[0]))
  15. //--> Importaciones de librerias
  16.  
  17. #include <iostream>
  18. #include <stdlib.h>
  19. #include <malloc.h>
  20.  
  21. // siganacion de namespace
  22.  
  23. using namespace std;
  24.  
  25.  
  26. /*
  27. Metodo que imprime un arreglo.
  28. Entradas: Un arreglo y un tamaño.
  29. Salida: La impresion del arreglo.
  30. */
  31. void mostrarArreglo(const int arreglo[], int tamano)
  32. {
  33. cout << "\n\n LA LISTA ES LA SIGUIENTE:\n" << endl;
  34. cout << "-------------------------" << endl;
  35. cout << "\tPOS" << "\tELEM"<< "\t|" << endl;
  36. for (int i = 0 ; i < tamano ; i++)
  37. cout << "\t[" << i << "]\t" << arreglo[i] << "\t|" << endl;
  38.  
  39. cout << "-------------------------" << endl;
  40. }
  41.  
  42. /*
  43. Metodo que hace una Busqueda Binaria dentro del Arreglo
  44.  
  45. */
  46. int busquedaBinaria(const int vector[], int n, int dato)
  47. {
  48. int centro,inf=0,sup=n-1;
  49.  
  50. while(inf<=sup)
  51. {
  52. centro=(sup+inf)/2;
  53. if(vector[centro]==dato)
  54. return centro;
  55. else if(dato < vector [centro] )
  56. {
  57. sup=centro-1;
  58. }
  59. else
  60. {
  61. inf=centro+1;
  62. }
  63. }
  64. return -1;
  65. }
  66.  
  67. /*
  68. Metodo de Multi Busqueda
  69. */
  70. int* multiBusqueda(const int datos[],const string buscados[],int pLargoDato,int pLargoBusq)
  71. {
  72. int LARGO = pLargoBusq;
  73.  
  74. int *array_out = new int[LARGO];
  75.  
  76. // Reserva de Memoria
  77. //array_out = (int *)malloc(LARGO*sizeof(int*));
  78.  
  79.  
  80. for(int j = 0; j<LARGO ;j++)
  81. {
  82. string cadena = buscados[j];
  83. int x = atoi(cadena.c_str());
  84.  
  85. x = busquedaBinaria(datos,pLargoDato,x);
  86.  
  87. array_out[j] = x;
  88. cout << "Elemento:\t" << cadena << "\tPosicion:\t"<< x << endl;
  89. }
  90.  
  91.  
  92. return array_out;
  93. }
  94.  
  95.  
  96. // ***********************************************
  97. // Funcion: Principal
  98. // Entrada: null
  99. // Salida: entero
  100. // ***********************************************
  101. int main()
  102. {
  103. int arreglo_datos [] = {10,12,13,17,16,21,25,32,41,54,63,78,84,95,115};
  104.  
  105. string arreglo_buscados [] = {"17","32","100","95"};
  106.  
  107.  
  108. cout << "\n IMPRESION DE LA LISTA ORIGINAL DE ELEMENTOS:" << endl;
  109. mostrarArreglo(arreglo_datos,cantElem(arreglo_datos));
  110.  
  111. // Aplicacion del metodo de Multi Busqueda
  112. int *array = multiBusqueda(arreglo_datos,arreglo_buscados,cantElem(arreglo_datos),cantElem(arreglo_buscados));
  113.  
  114. cout << "\n\n\nIMPRESION DE LAS POSICIONES DE LA LISTA DE BUSCADOS:" << endl;
  115. mostrarArreglo(array,cantElem(arreglo_buscados));
  116.  
  117. return EXIT_SUCCESS;
  118.  
  119. } // End Main
Add Comment
Please, Sign In to add comment