Question: Link to previous question this is based on: https://www.chegg.com/homework-help/questions-and-answers/https-wwwcheggcom-homework-help-questions-answers-assignment-require-arithmetic-c-involvin-q67737756?trackid=M9De3kqL Question: no longer need to solve for the roots of the quadratic, and you may remove
Link to previous question this is based on:
https://www.chegg.com/homework-help/questions-and-answers/https-wwwcheggcom-homework-help-questions-answers-assignment-require-arithmetic-c-involvin-q67737756?trackid=M9De3kqL
Question:
no longer need to solve for the roots of the quadratic, and you may remove those functions from your code
will still evaluate y = a x 2 + b x + c and print the results, for:
- x ( 0.0 , 1.0 , 2.0 , 3.0 , . . . , 10.0 )
- x ( 1.0 , 0.1 , 0.01 , . . . , 0.00000001 )
- x ( 1.0 , 10.0 , 100.0 , . . . , 1 000 000 000.0 )
- x ( 10.0 , 9.0 , 8.0 , . . . , 8.0 , 9.0 , 10.0 )
But now, there are additional caveats:
- You must create arrays of all four of the above sets of values
- You must use loops to initialize these arrays
- You must create arrays to store the y-results for all four of the above sets of values
- You still must have a single function that returns y = a x 2 + b x + c for a given a, b, c, and x; and use the loops to call it repeatedly
- You must also have a single function that:
- Accepts a, b, and c
- Accepts an array of x values
- Accepts a count of those x values
- Accepts an array buffer for y values
- Writes the computed y value for each x value into the corresponding spot in the y array buffer
- You must still format the printouts nicely, and make sure to distinguish between the four sets of values so the output is readable!
You must still go by the following:
- Do no arithmetic in main(). Create functions that do all your math.
- Do not use any global variables.
- Comment every function, every "paragraph" of code as we discussed in class, and any line of code that does more than two things.
- Indent your code properly.
Code:
#include
//function to calculate and return the discriminant double discriminant(double a, double b, double c){ //calculate the discriminant as b^2 - 4ac return (b * b - 4 * a * c); }
//function to calculate and print roots void get_roots(double a, double b, double c){ double d = discriminant(a,b,c); double root1,root2; //if d is positive, two distinct real roots exist if(d>0){ //calculate the two roots root1 = ( -b - sqrt(d) )/(2*a); root2 = ( -b + sqrt(d) )/(2*a); //print the two roots printf("\tThe roots of this quadratic equation are : %lf and %lf",root1,root2); } //if d = 0, then real and equal roots are present else if(d==0){ //calculate the root as -b/2a root1 = root2 = (-b)/(2*a); //print the root of the equation printf("\tThe given equation has two real and equal roots which is : %lf",root1); } //if d<0, no real roots exist else if(d<0){ //calculate the real and imaginary parts of the roots double real = (-b)/(2*a); double imaginary = sqrt(-d)/(2*a); //print the roots of the equation printf("\tThe given equation has two imaginary roots : %lf +i%lf and %lf -i%lf",real,imaginary,real,imaginary); } }
//function to evaluate value of y for the given valuues of a,b,c and x double evaluate(double a, double b, double c, double x){ //calculate ax^2 + bx + c double y = a*x*x + b*x +c; //return the calculated value return y; }
int main(){ double a,b,c,x; //user input for a.b and c printf(" \tEnter the value of a: "); scanf("%lf",&a); printf("\tEnter the value of b: "); scanf("%lf",&b); printf("\tEnter the value of c: "); scanf("%lf",&c); //call root function get_roots(a,b,c); //set x=0 x=0; //loop for x in (0.0,1.0,2.0,3.0,....,10.0) for(int i=0;i<11;i++){ //call evaluate function and print value of y corresponding to x printf(" \t for x = %lf y = %lf",x,evaluate(a,b,c,x)); //update x x+=1; } //set x=1 x=1; //loop for x in (1.0,0.1,0.01,0.001,....,0.00000001) for(int i=0;i<9;i++){ //call evaluate function and print value of y corresponding to x printf(" \t for x = %lf y = %lf",x,evaluate(a,b,c,x)); //update x as x = x/10 x/=10; } //set x=1 x=1; //loop for x in (1,10,100,1000,..., 1 000 000 000) for(int i=0;i<9;i++){ //call evaluate function and print value of y corresponding to x printf(" \t for x = %lf y = %lf",x,evaluate(a,b,c,x)); //update x as x = x*10 x*=10; } //set x = -1= x=-10; //loop for x in (-10,-9,-8,-7.....,8,9,10) for(int i=0;i<21;i++){ //call evaluate function and print value of y corresponding to x printf(" \t for x = %lf y = %lf",x,evaluate(a,b,c,x)); //increment x x+=1; } return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
