Monday, February 28, 2011

Arrays

Write a program for fitting a straight line through a set of points (xi,yi), i = 1, ..., n.
The straight line equation is y = mx + c
and the value of m and c are given by
m=(n*(x[i]*y[i])-(x[i])*(y[i]))/(n*(x[i]*x[i])-(x[i]*x[i]));
c=(1/n)*(y[i]-m*x[i]);

All summations are from 1 to n.

#include <stdio.h>
void main()
{
int i,x[10],y[10],n;
float m,c,line;
clrscr();
printf("Enter the value of terms:");
scanf("%d",&n);
printf("Enter the value of xi and yi\n");
for(i=1;i<=n;i++)
scanf("%d %d",&x[i],&y[i]);
for(i=1;i<=n;i++)
{
m=(n*(x[i]*y[i])-(x[i])*(y[i]))/(n*(x[i]*x[i])-(x[i]*x[i]));
c=(1/n)*(y[i]-m*x[i]);
printf("m = %5.2f C = %5.2f\n",m,c);
}
for(i=1;i<=n;i++)
{
line=m*x[i]+c;
printf("y = mx+c = %5.2f\n",line);
}
getch();
}

No comments:

Post a Comment