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

PLEASE HELP me revise this code. I need it to include a struct, array, pointer a

ID: 3708055 • Letter: P

Question

PLEASE HELP me revise this code. I need it to include a struct, array, pointer and union. PLEASE use my code and make it as simple as possible

#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;

}

Explanation / Answer

#include<stdio.h>
#include <math.h>

// Prototype of functions
double sum(double [], double [], int n);
double r(double [], double [], int , double , double );
float func(float ,float );
float euler(float ,float ,float ,int );

// Creates a union num
union num
{
int n;
};// End of union

// Creates a structure info
struct info
{
double sumx, sumy,sumxx,sumxy;
double a,b;
};// End of structure

// main function definition
int main()
{
// Creates double array and assigns data
double x[] = {2, 3};
double y[] = {3};
double one[] = {1, 1};
float xn, y0, e, x0 = 2;
int n = 2;

// Accepts data from the user
printf("Input n: ");
scanf("%d",&n);

printf("Input x0: ");
scanf("%f",&x0);

printf("Input xn: ");
scanf("%f",&xn);

printf("Input y0: ");
scanf("%f",&y0);

// Declares a structure object
struct info aObj;
// Declares a structure pointer
struct info *aPtr;
// Structure pointer points to structure object
aPtr = &aObj;

// Calls the function to calculate sum and stores the result in structure data member
aPtr->sumx = sum(x, one, n);
aPtr->sumy = sum(y, one, n);
aPtr->sumxx = sum(x, x, n);
aPtr->sumxy = sum(x, y, n);

// Calculates and stores the result in structure data member
aPtr -> a = (n * aPtr -> sumxy - aPtr -> sumx * aPtr -> sumy) / (n * aPtr -> sumxx - aPtr -> sumx * aPtr -> sumx);
aPtr -> b = (aPtr -> sumy * aPtr -> sumxx - aPtr -> sumx * aPtr -> sumxy) / (n * aPtr -> sumxx - aPtr -> sumx * aPtr -> sumx);

// Calls the function to do calculation
e = euler(x0, xn, y0, n);

printf(" By linear regression, the function is: y = %f x + %f ",aPtr -> a, aPtr -> b);

printf(" When x = %f, y = %f ", aPtr -> a * x0 + aPtr -> b, x0);

printf(" r = %f ", r(x, y, n, aPtr -> a, aPtr -> b));

return 0;
}// End of main function

// Function to calculate sum and return the result
double sum(double x[], double y[], int n)
{
// To store sum
double s = 0.0;
// Loop variable
int i = 0;
// Loops n times
for (i = 0; i < n; i++)
// Calculates sum
s = s + x[i] * y[i];
// Returns sum
return s;
}// End of function


// Function to calculate and return result
double r(double x[], double y[], int n, double a, double b)
{
double s0 = 0,s = 0,ybar = 0,total = 0;
// Loop variable
int i = 0;
// Loops n times
for (i = 0; i < n; i++)
{
// Calculates sum of y array
total += y[i];
}// End of for loop

// Stores the average
ybar = total/n;

// Loops n times
for (i = 0; i < n; i++)
{
// Calculates power
s0 += pow((y[i] - ybar), 2);
s += pow((y[i] - b - a * x[i]), 2);
}// End of for loop
// Returns the result
return pow((s0 - s) / s0, 0.5);
}// End of function

// Function to calculate the difference
float func(float x, float y)
{
return ((x * x) - (3 * x));
}// End of function

// Euler function
float euler(float x0, float xn, float y0, int n)
{
float x, y, h;
// Loop variable
int i;
x = x0;
y = y0;
h = (xn - x0) / n;
// Displays x0 and y0 value
printf("y(%f) = %6.4f ", x0, y0);
// Loops n times
for(i = 1; i <= n; i++)
{
y = y + h * func(x, y);
x = x0 + i * h;
printf("y(%f) = %6.4f ", x, y);
}// End of for loop
return y;
}// End of function

Sample Output:

Input n:
5
Input x0:
4
Input xn:
2
Input y0:
3
y(4.000000) = 3.0000
y(3.600000) = 1.4000
y(3.200000) = 0.5360
y(2.800000) = 0.2800
y(2.400000) = 0.5040
y(2.000000) = 1.0800

By linear regression, the function is: y = 1.#QNAN0 x + 1.#QNAN0

When x = 1.#QNAN0, y = 4.000000

r = 1.#QNAN0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote