C language !! If (x1,y1), (x2,y2), (Xn,yn) are n points in the xy coordinate sys
ID: 3635497 • Letter: C
Question
C language !!
If (x1,y1), (x2,y2), (Xn,yn) are n points in the xy coordinate system, then the least squares approximation to these points is the line y=mx+b, where Write a program that will calculate and displays the equation of the least squares line. Prompt the user to input n "the number of points". Use a user defined function calculate_m to fill all the possible points of xi and yi to find m and another user defined function calculate _b to fill all the possible points of xi and yi to find b. Note: Using Loops and user defined functions is a must.Explanation / Answer
please rate - thanks
#include <stdio.h>
#include <conio.h>
double calculate_m(double[],double[],int);
double calculate_b(double[],double[],int,double);
int input(double[],double[]);
void output(double[],double[],int,double,double);
int main()
{int n;
double x[50],y[50],m,b;
n=input(x,y);
m=calculate_m(x,y,n);
b=calculate_b(x,y,n,m);
output(x,y,n,m,b);
getch();
return 0;
}
double calculate_m(double x[],double y[],int n)
{double sumx=0,sumy=0,sumxy=0,sumx2=0;
int i;
for(i=0;i<n;i++)
{sumx+=x[i];
sumy+=y[i];
sumx2+=(x[i]*x[i]);
sumxy+=(x[i]*y[i]);
}
return (n*sumxy-(sumx*sumy))/((n*sumx2)-(sumx*sumx));
}
double calculate_b(double x[],double y[],int n,double m)
{double sumx=0,sumy=0;
int i;
for(i=0;i<n;i++)
{sumx+=x[i];
sumy+=y[i];
}
return (sumy-m*sumx)/n;
}
int input(double x[],double y[])
{int n,i;
printf("How many points do you have? ");
scanf("%d",&n);
for(i=0;i<n;i++)
{printf("for point %d ",i+1);
printf("Enter x: ");
scanf("%lf",&x[i]);
printf("Enter y: ");
scanf("%lf",&y[i]);
}
}
void output(double x[],double y[],int n,double m,double b)
{int i;
printf("given points x y ");
for(i=0;i<n;i++)
printf("%d %.2f %.2f ",i+1,x[i],y[i]);
printf("Formula: y = %.2fx + %.2f ",m,b);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.