pabloliva87

Test FuDePan

Jul 18th, 2011
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.79 KB | None | 0 0
  1. /* Examen de tesinas para FuDePAN */
  2.  
  3. /** Funciones Auxiliares */
  4.  
  5. /* Assignandcheck coloca el valor de array[arrindex] en res[place], aumenta arrindex en 1,
  6.    y verifica si se ha llegado al final de array; si es asi, le da el valor -1 a arrindex;
  7.    devuelve el nuevo valor de arrindex */
  8. int assignandcheck (int arrindex, const int place, const unsigned int size, const int array[], int * res) {
  9.     res[place] = array[arrindex];
  10.     arrindex ++;
  11.     if ((unsigned int)arrindex == size) {   /* Si se invoca esta funcion, arrindex >= 0 */
  12.                         /* por lo que no hay problemas con castearlo a unsigned */
  13.         arrindex = -1;
  14.     }
  15.     return arrindex;
  16. }
  17.  
  18. /** La funcion requerida en el enunciado */
  19.  
  20. unsigned int merge(const int array1[], unsigned int size1, const int array2[], unsigned int size2, int result[])
  21.     {
  22.  
  23.     int fstindex, sndindex; /* Indices para recorrer el primero y segundo arreglos, respectivamente */
  24.                 /* Valen -1 si se llego al final del arreglo correspondiente */
  25.     unsigned int i;
  26.  
  27.     unsigned int size_result = size1 + size2;
  28.    
  29.     fstindex = 0;
  30.     sndindex = 0;
  31.     for (i=0; i<size_result; i++){
  32.         if (fstindex != -1 && sndindex != -1) { /* Si todavia no se llego al final de ambos arreglos... */
  33.             if (array1[fstindex] <= array2[sndindex]) {
  34.                 fstindex = assignandcheck (fstindex, i, size1, array1, result);
  35.             } else {
  36.                 sndindex = assignandcheck (sndindex, i, size2, array2, result);
  37.             }
  38.         } else {    /* Si se llego al final de un arreglo pero el ciclo todavia no termino... */
  39.                 /* ...entonces todavia falta recorrer parte del otro arreglo */
  40.             if (sndindex == -1) {
  41.                 fstindex = assignandcheck (fstindex, i, size1, array1, result);
  42.             } else if (fstindex == -1) {
  43.                 sndindex = assignandcheck (sndindex, i, size2, array2, result);
  44.             }
  45.         }
  46.     }
  47.      
  48.     return size_result;
  49.     }
Add Comment
Please, Sign In to add comment