Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. The algorithm to generate the trendline of a set of points is: Generate Trend

ID: 3716063 • Letter: 1

Question

1. The algorithm to generate the trendline of a set of points is: Generate Trendline Calculate the slope Calculate the y_intercept Trendline is y = slope * x + y_intercept (y = mx + b form) Calculate the Slope Set sum_xy to zero Set sum_x to zero Set sum y to zero Set sum x_sq to zero Set qty to the number of points in the set For each point sum_xy += x * y sum_x += x sum_y += y sum_x_sq += x2 Set slope to ((qty * sum_xy) – (sum_x * sum_y ))/ ((qty * sum_x_sq) – (sum_x2 )) Calculate the y_intercept y_intercept = mean of y – slope * mean of x Mean of a set of numbers //left to the user to complete Sum of a set of numbers //left to the user to complete Write a program which accepts 20 points from the user and generates the trendline. The 20 points must be stored in an array of structs using the following definition:

The program must be written in C language. AND MUST BE ABLE TO RUN ON VISUAL STUDIO

1. The algorithm to generate the trendline of a set of points is: Generate Trendline Calculate the slope Calculate the y intercept Trendline is y slope *xy intercept (v-mx b form) Calculate the Slope Set sum xy to zerdo Set sum _x to zero Set sum y to zero Set sum x_sq to zero Set qty to the number of points in the set For each point sum x +=x sum. ? +-? Set slope to ((ty*sum_xy) (sum_x * sum_y))/((qty *sum_x_sq) (sum_x2)) Calculate the y intercept y_intercept - mean of y - slope mean of x Mean of a set of numbers //left to the user to complete Sum of a set of numbers //left to the user to complete Write a program which accepts 20 points from the user and generates the trendline. The 20 points must be stored in an array of structs using the following definition: typedef struct double x; double y; Point_t;

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>

//structure for point
typedef struct{
double x;
double y;

}Point_t;

//calculate mean
double mean(double a[],int n)
{
double sum=0;
for(int i=0;i<n;i++)
sum+=a[i];
return sum/n;
}
//calculate slope and return
double slope(Point_t p[],int n)
{
double sum_xy=0,sum_x=0,sum_y=0,sum_x_sq=0;
double slope;

for(int i=0;i<n;i++)
{
sum_xy+=p[i].x*p[i].y;
sum_x+=p[i].x;
sum_y+=p[i].y;
sum_x_sq+=p[i].x*p[i].x;

}
slope=((n*sum_xy)-(sum_x * sum_y))/((n*sum_x_sq)-(sum_x*sum_x));
return slope;
}

//calculate y intercept
double y_intercept(Point_t p[],int n)
{
double y_inter,mean_y,mean_x;
double sum_x=0,sum_y=0;
for(int i=0;i<n;i++)
{
sum_x+=p[i].x;
sum_y+=p[i].y;
}
mean_y=sum_y/n;
mean_x=sum_x/n;
y_inter=mean_y- (slope(p,n)*mean_x);

return y_inter;
}
//generate the trendline
void generateTrendline(Point_t p[],int n)
{
double y,slop;
y=y_intercept(p,n);
slop=slope(p,n);

printf("y=%.2lf *x+ %.2lf ",slop,y);
}
int main()
{
Point_t p[20];
int n;
  
//get number of points
printf("Enter number of points: ");
scanf("%d",&n);

//get upto 20 points
for(int i=0;i<n;i++){
printf("Enter x and y point 1: ");
scanf("%lf%lf",&p[i].x,&p[i].y);
}
//call function
generateTrendline(p,n);
return 0;
}