PLEASE HELP ME revise this code to include a struct, pointer and union. I have t
ID: 3707912 • Letter: P
Question
PLEASE HELP ME revise this code to include a struct, pointer and union. I have the code i wrote but it has mistakes PLEASE use the code i wrote also PLEASE make sure i coded the math portion correctly. This is the prompt
#include<stdio.h>
#include <math.h>
double sum(double [], double [], int n);
double r(double [], double [], int , double , double );
float func(float ,float );
float euler(float ,float ,float ,int );
union num {
int n;
};
struct info {
//double x0=2;
double sumx, sumy,sumxx,sumxy;
double a,b;
};
int main(){
double x[]={2,3};
double y[]={3};
double one[]={1,1};
float xn,y0,e, x0=2;
int n=2;
//double x0=2;
printf("inputn: ");
scanf("%d",&n);
printf("input x0: ");
scanf("%f",&x0);
printf("input xn: ");
scanf("%f",&xn);
printf("input y0: ");
scanf("%f",&y0);
struct info a;
struct info *aPtr;
aPtr=&a;
a.sumx=sum(x,one,n);
a.sumy=sum(y,one,n);
a.sumxx=sum(x,x,n);
a.sumxy=sum(x,y,n);
a=(n*sumxy-sumx*sumy)/(n*sumxx-sumx*sumx);
b=(sumy*sumxx-sumx*sumxy)/(n*sumxx-sumx*sumx);
e = euler(x0,xn,y0,n);
printf(" By linear regression, the function is: y=%fx+%f ",a,b);
printf(" When x= %f, y= %f ",a*x0+b,x0);
printf(" r = %f ",r(x,y,n,a,b));
return 0;
}
double sum(double x[],double y[], int n){
double s=0.0;int i=0;
for (i=0;i<n;i++)
s=s+x[i]*y[i];
return s;
}
double r(double x[],double y[], int n, double a, double b){
double s0=0,s=0,ybar=0,total=0;
int i=0;
for (i=0;i<n;i++){
total+=y[i];
}
ybar=total/n;
for (i=0;i<n;i++){
s0+=pow((y[i]-ybar),2);
s+=pow((y[i]-b-a*x[i]),2);
}
return pow((s0-s)/s0,0.5);
}
float func(float x,float y){
return ((x*x)-(3*x));
}
float euler(float x0,float xn,float y0,int n){
float x,y,h;
int i;
x=x0;
y=y0;
h=(xn-x0)/n;
printf("y(%f)=%6.4f ",x0,y0);
for(i=1;i<=n;i++) {y=y+h*func(x,y);
x=x0+i*h;
printf("y(%f)=%6.4f ",x,y);
}
return y;
}
Develop a C-code to meet the requirements below 1) Find the solution of the differential equation below by using Euler's method. The range of x is 2 to 3. dyldx-x2 3x with y-3 at x-2 2) Find the appropriate step size for this specific problem 3) Print all the y values within the range of x-2 to x-3. 4) Solve the differential equation analytically and compare with the Euler's method 5) Obtain a linear regression y-ax+b and show the r value Your C-code must contain the following features: (1) use of arrays, (2) use of pointers, (3) use of structure, (4) use of union, (5) use of functions and function calls, (6) formatted output on screen and (7) saving of the same output on a file.Explanation / Answer
#include<stdio.h>
#include <math.h>
double sum(double [], double [], int n);
double r(double [], double [], int , double , double );
float func(float ,float );
float euler(float ,float ,float ,int );
union num {
int n;
};
struct info {
//double x0=2;
double sumx, sumy,sumxx,sumxy;
double a,b;
};
int main(){
double x[]={2,3};
double y[]={3};
double one[]={1,1};
float xn,y0,e, x0=2;
int n=2;
//double x0=2;
printf("inputn: ");
scanf("%d",&n);
printf("input x0: ");
scanf("%f",&x0);
printf("input xn: ");
scanf("%f",&xn);
printf("input y0: ");
scanf("%f",&y0);
struct info a;
struct info *aPtr;
aPtr=&a;
a.sumx=sum(x,one,n);
a.sumy=sum(y,one,n);
a.sumxx=sum(x,x,n);
a.sumxy=sum(x,y,n);
a=(n*sumxy-sumx*sumy)/(n*sumxx-sumx*sumx);
b=(sumy*sumxx-sumx*sumxy)/(n*sumxx-sumx*sumx);
e = euler(x0,xn,y0,n);
printf(" By linear regression, the function is: y=%fx+%f ",a,b);
printf(" When x= %f, y= %f ",a*x0+b,x0);
printf(" r = %f ",r(x,y,n,a,b));
return 0;
}
double sum(double x[],double y[], int n){
double s=0.0;int i=0;
for (i=0;i<n;i++)
s=s+x[i]*y[i];
return s;
}
double r(double x[],double y[], int n, double a, double b){
double s0=0,s=0,ybar=0,total=0;
int i=0;
for (i=0;i<n;i++){
total+=y[i];
}
ybar=total/n;
for (i=0;i<n;i++){
s0+=pow((y[i]-ybar),2);
s+=pow((y[i]-b-a*x[i]),2);
}
return pow((s0-s)/s0,0.5);
}
float func(float x,float y){
return ((x*x)-(3*x));
}
float euler(float x0,float xn,float y0,int n){
float x,y,h;
int i;
x=x0;
y=y0;
h=(xn-x0)/n;
printf("y(%f)=%6.4f ",x0,y0);
for(i=1;i<=n;i++) {y=y+h*func(x,y);
x=x0+i*h;
printf("y(%f)=%6.4f ",x,y);
}
return y;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.