I must write a C program that meets the following requirements: 1. Data Entry St
ID: 3704156 • Letter: I
Question
I must write a C program that meets the following requirements:
1. Data Entry Step: reads N ordered pairs of data (xi,yi) into a pair of arrays x[] and y[]. Query the user for a value for N in advance; N will not be greater than 100.
2. Analysis Step: using the obtained data and the formulas above, calculates the parameters m and b of the least-squares fit to the data and prints those values to the screen in an appropriate way.
3. Predictive Step: reads in a series of x values (number not known in advance, but no more than 100 values) until the user enters the sentinel value -999. Then (after all values are input) the program prints out a table of values for the least-fit line y=mx+b in two tab-separated columns labeled x and y, and computes and prints out the centroid – the half-way point on the line segment between the extreme points on the line segment entered.
Linear Least-Squares is an attempt to find a straight line that best represents a set of data points. It is a very common problem in modeling and statistics – you have a large set of observed data points, and you want to be able to describe it with a single line that “fits” the data as well as possible.
Explanation / Answer
#include<stdio.h>
#include<math.h>
int main()
{
int x[30],X=0,xx=0,xy=0,y[30],Y=0,n=100,
bnum,bden,mnum,mden,a[30],N,Yn;
float b=0.0,m=0.0;
int i=0,sent=0;
printf("Enter the 100 ordered pairs ");
for(i=0;i<=n;i++)
{
scanf("%d%d",&x[i],&y[i]);0
printf(" ");
}
printf("The ordered pairs are ");
for(i=0;i<n;i++)
{
printf("%2d%2d",x[i],y[i]);
printf(" ");
}
for(i=0;i<n;i++)
{
X=X+x[i]; //Sum of values in x array
xx=xx+x[i]*x[i]; //Sum of square of values in x array
xy=xy+x[i]*y[i]; //Sum of product of values of x and y array
Y=Y+y[i]; //Sum of values in y array
}
bnum=(xx*Y)-(X*xy);
bden=(n*xx)-(X*X);
// y intercept
b=bnum/bden;
printf("The Y intercept b = %.2f ",b);
mnum=(n*xy)-(X*Y);
mden=bden;
//slope
m=mnum/mden;
printf("The slope m = %.2f ",m);
i=0;
// reading set of x values
while(sent!=-100000)
{
printf("Enter the x values ");
scanf("%d",&a[i]);
i=i++;
printf("If done enter -100000 or enter another number to continue ");
scanf("%d",&sent);
}
N=i;
printf("x y ");
for(i=0;i<N;i++)
{
Yn=(m*a[i])+b;
printf("%3d%3d ",a[i],Yn);
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.