title: writing a function that splits an interval in c
body: <p>I'm writing a function that's supposed to increase or decrease an interval between 1 and 100, depending on which parameter it takes.</p>
  
  <p>It starts from <code>(0,100)</code>, then, if <code>parameter=-1</code> it becomes <code>(0,50)</code>, if <code>parameter=1</code> afterwards, it becomes <code>(25, 50)</code>. It stores the last "guess" and gives the next one based on that.</p>
  
  <p>I'd like to know what I'm doing wrong when <code>parameter=-1</code> (the part where it's supposed to decrease the interval). Other parts work fine, you don't have to look at them, they give the expected output BUT this (-1) one gives <code>25 12 6 3 2</code> instead of <code>25 12 6 3 1</code>.</p>
  
  <p>here's the code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  int NextGuess(int  param) {
  
      int num;
      static int a=1, b=99;
  
      //this part is where it goes wrong (the output doesn't match with the expected one)//
      if (param==-1) //less
      {
          b-=(a+b)/2 - a+1;
          num=(a+b)/2;
      }
      //these below are doing fine//
  
      if (param==1) //greater
      {
          a+=(a+b)/2 - a+1;
          num=(a+b)/2;
      }
  
      if (param==0) //equal
      num=(a+b)/2;
  
      return num;
  }
  //main isn't important, it's just for testing the output//
  int main() {
  printf ("%d ", NextGuess(-1));
  printf ("%d ", NextGuess(-1));
  printf ("%d ", NextGuess(-1));
  printf ("%d ", NextGuess(-1));
  printf ("%d ", NextGuess(-1));
  //output should be: 25 12 6 3 1  but this gives: 25 12 6 3 2    
      return 0;
  }
  </code></pre>
  
  <p>If someone could just point to what I'm doing wrong in that part, I've been thinking about this simple thing for far too long now...</p>
  
  <p>other examples of what the function is supposed to output when called:</p>
  
  <pre><code>printf ("%d ", NextGuess(1));
  printf ("%d ", NextGuess(-1));
  printf ("%d ", NextGuess(-1));
  printf ("%d ", NextGuess(1));
  //output: 75 62 56 59
  </code></pre>