Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The task is to create second-degree polynomial calculator for the function, its integral and its derivative. A second-degree polynomial is described by the equation f(x)

The task is to create second-degree polynomial calculator for the function, its integral and its derivative. A second-degree polynomial is described by the equation

f(x) = ax^2 + bx + c

Evaluation of the derivative gives the instantaneous slope or rate of change as (where f"(x) is the derivative of f(x)):

f'(x) = 2ax + b

The integral of the function f(x) (let us call it F(x)) indicates the area underneath the curve. Between points x _1 and x_2, the area A is

A = F(x_2) - F(x_1)

where, for a second-degree polynomial,

F(x) = (a/3)x^3 + (b/2)x^2 + cx

Your program will be used to create a table of data of the independent variable x versus f(x), f ' (x) and A. The user will enter the polynomial coefficients {a,b,c} as well as the starting value of x (denote it x_i), the final value of x (denote it x_f), and the increment value between successive x values (denote it deltaX).

Follow these detailed instructions:

First, read this document in its entirety. After reading this handout, you may optionally create a design sheet for the problem. Your design sheet should help you determine what header files, functions and variables you will need as well as identifying expected test results. You do NOT turn in your design sheet.

When you write your code, include the usual (detailed) comment block including program name, author, date, inputs, outputs and description.

Create a function of type void to explain the program to the user with the description printed to the terminal. Do not forget to call the function from main().

Input the data while executing from your main() function. Query the user to enter the data. You must enter the parameters from the user in the following order: a, b, c and then x_i, x_f and deltaX. Use double for all variables and calculations.

Your program is to loop over all possible x values in your main() function. The first x value is to be x_i , the second x_i + deltaX, etc. with the last value being x_f (or somewhat less than x_f if deltaX does not divide evenly into the range).

The calculation of the area A depends on the initial value of x, x_i. That is, for each x, we calculate the area as A = F(x) - F(x_i). In order to calculate A, you must know both x and x_i. For the first value of x, it should be that A is zero.

******** (This is the most important part please use comments to explain what you are doing) *****************

From main() and for each x, you are to call a function that accepts a, b, c, x and x_i. Calculate and return (via pointer operations) f (x), f ' (x) and A. That is, you must create a single function that accepts eight inputs five are call-by-value (a, b, c, x and x_i), and three are call-by-reference (pointers related to f(x), f ' (x) and A). There must not be any scan/print statements in your function.

Your function uses the values of a, b and c along with the value of x to compute f(x) = ax^2 + bx + c. Similarly, the derivative is computed as f ' (x) = 2ax + b. Then, using x_i in addition to the other parameters, compute F(x_i) = (a/3)x_i^3 + (b/2)x_i^2 + cx_i and F(x) = (a/3)x^3 + (b/2)x^2 + cx. The area is found as A = F(x) - F (x_i). Return the result of your calculations back to the main() function (it will be necessary to reference your outputs via pointers).

All output for your table of x versus f(x), f ' (x) and A must be displayed via statements in your main() function. Write your table to the terminal (i.e., the default output for printf()). Choose a suitable format for your output numbers.

An example of what the output table should look like is given below. Assume that the user enters 2.0 -2.0 -1.0 for a, b, c, respectively, and 0.0 5.0 0.25 for x_i, x_f and deltaX, respectively. Your output should be the following:

f(x) = 2x^2 + -2x + -1

x f(x) f'(x) A

-------------------------------

0.000 -1.000 -2.000 0.000

0.250 -1.375 -1.000 -0.302

0.500 -1.500 0.000 -0.667

0.750 -1.375 1.000 -1.031

1.000 -1.000 2.000 -1.333

1.250 -0.375 3.000 -1.510

1.500 0.500 4.000 -1.500

1.750 1.625 5.000 -1.240

2.000 3.000 6.000 -0.667

2.250 4.625 7.000 0.281

2.500 6.500 8.000 1.667

2.750 8.625 9.000 3.552

3.000 11.000 10.000 6.000

3.250 13.625 11.000 9.073

3.500 16.500 12.000 12.833

3.750 19.625 13.000 17.344

4.000 23.000 14.000 22.667

4.250 26.625 15.000 28.865

4.500 30.500 16.000 36.000

4.750 34.625 17.000 44.135

5.000 39.000 18.000 53.333

Use the test set as given above.

My code looks like....

#include #include #include #define MAX_SIZE 100

void explain (); double userinput(char*); void calculations(int a, int b, int c, int x, int x_initial, double *f, double *fprime, double *area);

int main() { double a, b, c, xi, xf, deltaX; // a, b, c, x initial, x final and x step variables double x[MAX_SIZE], f[MAX_SIZE], fprime[MAX_SIZE], A[MAX_SIZE]; int i = 0; //counter for for loops int N = 0; // the size of the arrays explain();

//prompts user to enter values a = userinput("Enter a value for a: "); b = userinput("Enter a value for b: "); c = userinput("Enter a value for c: "); xi = userinput("Enter a value for x initial: "); xf = userinput("Enter a value for x final: "); deltaX = userinput("Enter a value for x step: ");

printf("f(x) = %gx^2 + %gx + %g ", a, b, c);

N = xf / deltaX; // the seize of all arrays

printf(" x\tf(x)\tf'(x)\tA\t "); printf("----------------------------- ");

for(i = 0; i

}

//explains the program to the user void explain() { printf("Hello! This program accepts inputs for a, b, c, x initial, "); printf("x final and x step. The program generates and outputs f(x), "); printf("f'(x) and the area under the curve using the integral. "); }

//error checks for only numbers entering the program double userinput(char message []) {

double answer; //variable to hold answer int status, error; //variable to check if there is an error

do { printf("%s", message); status = scanf("%lf", &answer);

error = status != 1;

if (error) { printf("Input Error... "); fflush(stdin); } } while (error);

return answer; }

void calculations(int a, int b, int c, int x, int x_initial, double *f, double *fprime, double *area){ double Fx_i = 0; double Fx = 0;

Fx_i = (a / 3) * (x_initial * x_initial * x_initial) + (b / 2) * (x_initial * x_initial) + c * x_initial; Fx = (a / 3) * (x * x * x) + (b / 2) * (x * x) + c * x;

*f = (a * x * x) + (b * x) + c; *fprime = (2 * a * x) + b; *area = Fx - Fx_i;

}

And it prints .... the image

image text in transcribed

"WAEGR 107\ Assignment 1\WisbyGenevieve_StructuredProgramming\main.exe" Enter a value for x final: Enter a value for x step: .25 f(x? = 2x^2 +-2x +-1 0.000-1.0002.000 0.000 0.250-1.000 -2.000 0.000 0.500-1.00 2.000 0.000 0.750-1.000 -2.000 .000 1.000 -1.000 2.000-2.000 1.250 -1.000 2.000-2.000 1.500 -1.000 2.000-2.000 1.750 -1.000 2.000-2.000 2.000 3.000 6.00-6.000 2.250 3.000 6.00-6.000 2.500 3.000 6.00-6.000 2.750 3.000 6.00-6.000 3.000 11.000 10.000 -12.000 3.250 11.000 10.000 -12.000 3.500 11.000 10.000-12.000 3.750 11.000 10.000 -12.000 4.000 23.000 14.000 -20.000 4.250 23.000 14.000 -20.000 4.500 23.000 14.000 -20.000 4.750 23.000 14.0G0 -20.000 5.000 39.000 18.00030.000 Process returned 0 ?0x0) execution time : 13.676 s Press any key to continue

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2014 Nancy France September 15 19 2014 Proceedings Part 2 Lnai 8725

Authors: Toon Calders ,Floriana Esposito ,Eyke Hullermeier ,Rosa Meo

2014th Edition

3662448505, 978-3662448502

Students also viewed these Databases questions

Question

What is the difference between OLAP and OLTP?

Answered: 1 week ago