title: Having problems with my function in C trying to calculate a Regression slope using lesser block method. Can anyone help me figure out my mistake?
body: <p>This is C, can't use anything but C.</p>
  <p>So I was given the equation here:</p>
  <p><a href="https://i.stack.imgur.com/PZC9p.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/PZC9p.png" alt="enter image description here" /></a></p>
  <p>I am trying to calculate the 'b' in the formulae. The formula is Regression slope done via Lease Squares Formulae</p>
  <p>My code takes in a list of numbers called 'arr[]' and the amount of elements in that list being 's'. I go through the calculations and I get the wrong number. If anyone can help me out that would be awesome and if I'm not allowed to ask this here, please point me in the direction to where I can ask this.</p>
  <p>calcMean is a function that calculates the mean.
  An Example of what I need:
  With a list of numbers being: 1 1 1 2 2 3 3 3 4 5 5 6 7
  I need to get a: 0.429
  My code currently gives me an answer that is quite off.</p>
  <p>My code:</p>
  <pre><code>double calcRegSlope(double arr[], int s) {
      double result = 0.0;
      double meanX = 0.0;
      double meanY = 0.0;
      double sumX = 0.0;
      double sumY = 0.0;
      double xValue = 0.0;
      double yValue = 0.0;
      for (int i = 0; i &lt; s; i++) {
          sumX += pow((double)i,2.0);    
          sumY += (arr[i] * (double)i);
          meanX += (double)i;
      }
      
      meanX = meanX / (double)s;
      meanY = calcMean(arr, s);
      
      
      xValue = sumX - (s * pow(meanX, 2.0));                            
      yValue = sumY - (s * meanX * meanY);                            
      
      result = yValue / xValue;
      
      return (double)result;
  }
  </code></pre>