title: Using the returned values as an input to the function continuously
body: <p>I have this code that used KD tree written in C used in searching for nearest neighbor and then uses the returned value to search for the next nearest neighbor from that point. I want to do this for about 5 iterations i.e the result of first iteration is used as an input to the 2nd and the result of the second used for the 3rd etc. I am a beginner and I thought probably do while loop will work but fails after 2 iterations i.e i get the same input again.</p>
  
  <p>How do I change the value of this variable to reflect changes such that the output of first iteration is the input of the last operation been performed. Also if there is a way to create a function for this, it will be highly appreciated. The code works with gcc compiler.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;math.h&gt;
  
  #define MAX_DIM 4
  
  struct kd_node_t{
      double x[MAX_DIM];
      struct kd_node_t *left, *right;
  };
  
      inline double
  dist(struct kd_node_t *a, struct kd_node_t *b, int dim)
  {
      double t, d = 0;
      while (dim--) {
          t = a-&gt;x[dim] - b-&gt;x[dim];
          d += t * t;
      }
      return d;
  }
  inline void swap(struct kd_node_t *x, struct kd_node_t *y) {
      double tmp[MAX_DIM];
      memcpy(tmp,  x-&gt;x, sizeof(tmp));
      memcpy(x-&gt;x, y-&gt;x, sizeof(tmp));
      memcpy(y-&gt;x, tmp,  sizeof(tmp));
  }
  
  
  /* quickselect method */
      struct kd_node_t*
  find_median(struct kd_node_t *start, struct kd_node_t *end, int idx)
  {
      if (end &lt;= start) return NULL;
      if (end == start + 1)
          return start;
  
      struct kd_node_t *p, *store, *md = start + (end - start) / 2;
      double pivot;
      while (1) {
          pivot = md-&gt;x[idx];
  
          swap(md, end - 1);
          for (store = p = start; p &lt; end; p++) {
              if (p-&gt;x[idx] &lt; pivot) {
                  if (p != store)
                      swap(p, store);
                  store++;
              }
          }
          swap(store, end - 1);
  
          /* median has duplicate values */
          if (store-&gt;x[idx] == md-&gt;x[idx])
              return md;
  
          if (store &gt; md) end = store;
          else        start = store;
      }
  }
  
      struct kd_node_t*
  make_tree(struct kd_node_t *t, int len, int i, int dim)
  {
      struct kd_node_t *n;
  
      if (!len) return 0;
  
      if ((n = find_median(t, t + len, i))) {
          i = (i + 1) % dim;
          n-&gt;left  = make_tree(t, n - t, i, dim);
          n-&gt;right = make_tree(n + 1, t + len - (n + 1), i, dim);
      }
      return n;
  }
  
  
  int visited;
  
  void nearest(struct kd_node_t *root, struct kd_node_t *nd, int i, int dim,
          struct kd_node_t **best, double *best_dist)
  {
      double d, dx, dx2;
  
      if (!root) return;
      d = dist(root, nd, dim);
      dx = root-&gt;x[i] - nd-&gt;x[i];
      dx2 = dx * dx;
  
      visited ++;
  
      if (!*best || d &lt; *best_dist) {
          *best_dist = d;
          *best = root;
      }
  
  
      nearest(dx &gt; 0 ? root-&gt;left : root-&gt;right, nd, i, dim, best, best_dist);
      if (dx2 &gt;= *best_dist) return;
      nearest(dx &gt; 0 ? root-&gt;right : root-&gt;left, nd, i, dim, best, best_dist);
  }
  
  int main(void)
  {
      int i;
  
    struct kd_node_t wp[] = {
          {{7, 9, 5, 56}},{{2, 4, 8, 10}},  {{81, 2, 31, 80}}, {{31, 4, 900, 1}},{{4, 7, 1, 9}}, {{9, 6, 2,0}}, {{4, 3, 11, 2}},{{7, 7, 9, 1}}, {{6, 9, 0,2}}
      };
  
      struct kd_node_t testNode = {{0, 2}}; //This is the input
  
      struct kd_node_t *root, *found, *million;
      double best_dist;
      double length =sizeof(wp) / sizeof(wp[1]);
      root = make_tree(wp, sizeof(wp) / sizeof(wp[1]), 0, 2);
  
      visited = 0;
      found = 0;
      nearest(root, &amp;testNode, 0, 2, &amp;found, &amp;best_dist);
  
      printf("&gt;&gt; WP tree\nsearching for (%g, %g)\n"
              "found (%g, %g %g, %g) dist %g\nseen %d nodes\n\n",
              testNode.x[0], testNode.x[1],
              found-&gt;x[0], found-&gt;x[1],found-&gt;x[2], found-&gt;x[3], sqrt(best_dist), visited);
  //It produces an output found-&gt;x[0], found-&gt;x[1], found-&gt;x[2], found-&gt;x[3]
  
  for(int i=0; i&lt;5; i++) //Where the problem is and where i will like to continuously use the returned value as input
  {
  
  
      testNode = {{found-&gt;x[2], found-&gt;x[3]}}; // This is the new input i.e the output of the previous run
  printf(" (%g, %g) ",
                  found-&gt;x[2], found-&gt;x[3]);
       root = make_tree(wp, sizeof(wp) / sizeof(wp[1]), 0, 2);
       nearest(root, &amp;testNode, 0, 2, &amp;found, &amp;best_dist);
    printf("&gt;&gt; WP tree\nsearching for (%g, %g)\n"
              "found (%g, %g %g, %g) dist %g\nseen %d nodes\n\n",
              testNode.x[0], testNode.x[1],
              found-&gt;x[0], found-&gt;x[1],found-&gt;x[2], found-&gt;x[3], sqrt(best_dist), visited);
  
  
  
  }
  
  
  
      return 0;
  }
  </code></pre>
  
  <p>This is a sample output from the running code</p>
  
  <pre><code>&gt;&gt; WP tree
  searching for (0, 2)
  found (2, 4 8, 10) dist 2.82843
  seen 4 nodes
  
  8 10&gt;&gt; WP tree
  searching for (8, 10)
  found (7, 9 5, 56) dist 1.41421
  seen 11 nodes
  
  5 56&gt;&gt; WP tree
  searching for (5, 56)
  found (7, 9 5, 56) dist 1.41421
  seen 16 nodes
  
  5 56&gt;&gt; WP tree
  searching for (5, 56)
  found (7, 9 5, 56) dist 1.41421
  seen 21 nodes
  
  5 56&gt;&gt; WP tree
  searching for (5, 56)
  found (7, 9 5, 56) dist 1.41421
  seen 26 nodes
  
  5 56&gt;&gt; WP tree
  searching for (5, 56)
  found (7, 9 5, 56) dist 1.41421
  seen 31 nodes
  </code></pre>
  
  <p>In fact 5,56 should return 6, 9,0,2 and the next point to be selected should be 0,2 etc.
  Also how do I delete temporarily the value of the returned value from wp so that when it searches, it does not include its coordinate in the search space but at the next iteration, its value should be restored. e.g say I am searching for 5,56; the entire {{7, 9, 5, 56, 30}} will be removed from wp for that current search but will be restored later after returning its nearest data.</p>
  
  <p>Any help will be highly appreciated. Thanks</p>