views:

182

answers:

4

the formula is pretty complicated. the numerator is num and the denominator is den, in the formula there is a root on the denominator so i have putted den in sqrrt() but sqrrt only accepts doubles

#include<stdio.h>
#include<conio.h>
#include<math.h>
#define LEN 11
// for the following set of x and y find r by the formula ..
float sum(float arr[]);
void main(void)
{    int i;
    float x[]={43.22,39.87,41.85,43.23,40.06,53.29,53.29,54.14,49.12,40.71,55.15};
    float y[]={102.43,100.93,97.43,97.81,98.32,98.32,100.07,97.08,91.59,94.85,94.6};
    float num,den[LEN],r[LEN],xy[LEN],x2[LEN],y2[LEN];
    for(i=0;i<LEN;i++)
    {
        x2[i]=x[i]*x[i];
        y2[i]=y[i]*y[i];
        xy[i]=x[i]*y[i];
    }
    num=sum(xy)-sum(x)*sum(y);
    for(i=0;i<LEN;i++)
    {
        den[i]=((LEN*sum(x2)-(sum(x))*(sum(x)))*(LEN*sum(y2))-(sum(y2))*(sum(y2)));
        r[i]=num /sqrt(den);  /*<----------the problem is here-----> */

    }
    printf("%f",r);
    getch();
}
float sum(float arr[])
    {
    int i;
    float total=0;
    for(i=0;i<=LEN;i++)
    {
        total+=arr[i];
    }
    return total;
    }
A: 

I don't get it. This is unclear: why don't you just sum all the table entry then return the sqrt of the sum?

Aif
Not an answer - should be a comment
Paul R
indeed it is an answer: to find the "square root" of an array, sum all elements (table entry/ies) and return the sqrt of the sum. Since the question is unclear, as first try answer is reasonable after all (but @Aif read comments and consider making it fit what the OP intended indeed!).
ShinTakezou
Should I delete my answer?
Aif
A: 

You need to give the index den[i] at the denominator....instead in your code you have just passed the base address!

 r[i]=num /sqrt(den[i]);

If this is what you want to achieve, which is quite unclear.

gavishna
A: 

To be consistent with the rest of the code, you should presumably be writing:

r[i] = num / sqrt(den[i]);

However, the calculation is not one I recognize. The body of the second loop is going to produce the same result for each value in den and therefore also in r, which is probably not what the question asked for.

Jonathan Leffler
i guess i am sending the address of the array to the function so i dont need to add [i] to it
fahad
@fahad: the `sqrt()` function only accepts a `double` (or a value such as an integer or `float` that can be converted to `double`); it does not accept pointers.
Jonathan Leffler
why did you repeat gavishna's answer?
vol7ron
@vol7ron: I didn't; I was typing at the same time that Gavishna was. I fixed up the typography of Gavishna's answer too...and my answer contains some content which the other does not, which is why I have not removed mine.
Jonathan Leffler
@Jonathan Leffler, it stinks that I can't see timestamps :)
vol7ron
+4  A: 

Out of sheer boredom I have fixed your code. It is still ugly and extremely inefficient but compiles and should work. I'll leave you or someone else to make it decent.

#include <stdio.h>
#include <math.h>
#define LEN 11


// for the following set of x and y find r by the formula ..
float sum(float arr[]);
int main(void)
{ int i;
      float x[]={43.22,39.87,41.85,43.23,40.06,53.29,53.29,54.14,49.12,40.71,55.15};
      float y[]={102.43,100.93,97.43,97.81,98.32,98.32,100.07,97.08,91.59,94.85,94.6};


      float num,den,r[LEN],xy[LEN],x2[LEN],y2[LEN];
      for(i=0;i<LEN;i++)
          {
                x2[i]=x[i]*x[i];
                y2[i]=y[i]*y[i];
                xy[i]=x[i]*y[i];
              }
      num=LEN*sum(xy)-sum(x)*sum(y);
      den = (LEN*sum(x2)) - sum(x)*sum(x);

      float alpha = sum(y)/LEN - (num/den)*sum(x)/LEN;

      printf("beta = %f,  alpha = %f\n", num/den, alpha);     
      for(i=0;i<LEN;i++)
          {
            float term = y[i] - alpha - (num/den)*x[i];
            r[i] = (term*term);

                printf("%f",r[i]);
          }
}
float sum(float arr[])
  {
      int i;
      float total=0;
      for(i=0;i<=LEN;i++)
          {
                total+=arr[i];
              }
      return total;
      }
Il-Bhima
what are alpha and beta here?+ why dint u use n in the denominator! the formula has n in the denominator!
fahad
formulae use symbols which have a meaning. Ask yourself: what is the "meaning" of n in the formula? Likely it is just the number of elements; you don't need to use n, if you have already LEN "representing" that value.
ShinTakezou
disregard my prev comments if you refer to `den = (LEN*sum(x2)) - sum(x)*sum(x);` - but the comment is ok anyway. Consider also showing us the formula you have!
ShinTakezou
http://en.wikipedia.org/wiki/Simple_linear_regression.
Il-Bhima
@Il-Bhima the invite to show the formula was for the OP;) so that we can see what he's seeing since it seems in its formula he has n
ShinTakezou