#include<stdio.h> #include<stdlib.h> #define MAX 20 void calculateAndPrintData(f
ID: 3697920 • Letter: #
Question
#include<stdio.h>
#include<stdlib.h>
#define MAX 20
void calculateAndPrintData(float* X, float* Y, int n);
int main()
{
float x[MAX], y[MAX];
int i,n;
printf("Enter total number of data: "); //total data
scanf("%d", &n);
printf("Enter x and y one by one for %d times ", n);
//data saved in arrays
for(i=0; i<n; i++)
{
printf(" x[%d]:",i+1); //value point x
scanf("%f", &x[i]);
printf(" y[%d]:",i+1); //value point y
scanf("%f", &y[i]);
}
calculateAndPrintData(x, y, n);
}
void calculateAndPrintData(float* X, float* Y, int n)
{
float Sx=0, Sy=0, Sxy=0, Sx2 = 0;
float denom, b, a1;
int i;
for(i=0; i<n; i++)
{
Sx = Sx + X[i];
Sx2 = Sx2 + X[i]*X[i];
Sy = Sy + Y[i];
Sxy = Sxy + X[i]*Y[i];
}
//calculators
denom = n*Sx2 - Sx*Sx;
b = (n*Sxy - Sx*Sy)/denom;
a1 = (Sy*Sx2 - Sx*Sxy)/denom;
//print data
printf(" Regression Coefficients are as under: ");
printf(" Stop of Regression line:%.2f ",b);
printf(" Intercept for the line:%.2f ",a1);
system("pause");
}
Q) Draw the function structures of the developed source code.
Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
#define MAX 20
struct SetData
{
float x[MAX], y[MAX],denom, b, a1;
int n;
};
void calculateAndPrintData(struct SetData passData);
int main()
{
struct SetData passData;
int i;
printf("Enter total number of data: "); //total data
scanf("%d", &passData.n);
printf("Enter x and y one by one for %d times ", passData.n);
//data saved in arrays
for(i=0; i<passData.n; i++)
{
printf(" x[%d]:",i+1); //value point x
scanf("%f", &passData.x[i]);
printf(" y[%d]:",i+1); //value point y
scanf("%f", &passData.y[i]);
}
calculateAndPrintData(passData);
return 0;
}
void calculateAndPrintData(struct SetData passData)
{
float Sx=0, Sy=0, Sxy=0, Sx2 = 0;
int i;
for(i=0; i<passData.n; i++)
{
Sx = Sx + passData.x[i];
Sx2 = Sx2 + passData.x[i]*passData.x[i];
Sy = Sy + passData.y[i];
Sxy = Sxy + passData.x[i]*passData.y[i];
}
//calculators
passData.denom = passData.n*Sx2 - Sx*Sx;
passData.b= (passData.n*Sxy - Sx*Sy)/passData.denom;
passData.a1 = (Sy*Sx2 - Sx*Sxy)/passData.denom;
//print data
printf(" Regression Coefficients are as under: ");
printf(" Stop of Regression line:%.2f ",passData.b);
printf(" Intercept for the line:%.2f ",passData.a1);
system("pause");
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.