**Question:**Write a function that takes the arguments a, b and c, computes and then displays the roots of the quadratic equation.

The idea of this task is to create a simple logical step in which we define what the program should do in different circumstances. In the previous examples there was a linear flow in the logic of the program, which was

- Declare variables
- Assign values
- Calculate
- Output results

where as now we must have something like

- Declare variables
- Assign values
- Calculate
- If real:: Output real roots
- If complex:: Output complex roots

In order to access this functionality we use the if condition to evaluate whether or not something is true and then execute an action depending on the result.

Again when writing a program you should always be thinking about the structure and try to stage your program so that you can run and test as you go along. Those stages in this program might be:-

- Include all libraries
- Declare all variables and assign with default values
- Calculate discriminant and output to screen
- Use if condition to evaluate if real or not and output roots
- Lift your code into a function and call it from main

Give the only libraries you should need are the input/output and mathematical functions your initial program should look like:

#include<iostream>#include<cmath>usingnamespacestd; intmain() {// do nothing yetreturn0; }

Compile and run and check your program outputs a black terminal.

Now add in the variables we need, for a quadratic we will need to store all of the parameters a, b and c as well as the value of the discriminant.

double a,b,c,discriminant;

and assign default values to the parameters

a=1.;b=1.;c=1.;

Again check your program at every stage that it compiles and runs.

Next we can calculate the discriminant from the formula as

discriminant = b * b - 4. * a * c; cout << " discriminant = " << discriminant << endl;

Check that the value of the discriminant appear to be correct.

Now use an if condition to do something different depending on whether the discriminant is positive or negative

if(discriminant < 0.) { cout << " The roots are complex " << endl; }else{ cout << " The roots are real " << endl; }

Now compile and run the program and check that the output is given as expected. Change the values of the parameters a, b and c and check that the program still works. You may add an else if statement in the middle for the case when the discriminant is zero. Add some extra code inside the if blocks to output the values of the roots. Again check and compile that the code is working. At this stage your program should look something like:

#include<iostream>#include<cmath>usingnamespacestd; intmain() {// declare all variablesdouble a,b,c,discriminant;// assign default valuesa=1.;b=1.;c=1.;// calculate the discriminantdiscriminant = b * b - 4. * a * c; cout << " discriminant = " << discriminant << endl;// use if statement to change outputif(discriminant < 0.) { cout << " The roots are complex and are " << endl; cout << " x+ = " << -b/2./a << "+" <<sqrt(-discriminant)/2./a << "i" << endl; cout << " x- = " << -b/2./a << "-" <<sqrt(-discriminant)/2./a << "i" << endl; }else{ cout << " The roots are real and are" << endl; cout << " x+ = " << (-b +sqrt(discriminant))/2./a << endl; cout << " x- = " << (-b -sqrt(discriminant))/2./a << endl; }return0; }

Once your program has been thoroughly checked, we need to create a function above the main function that does everything in the main function. Now we need to think about what are the inputs and outputs from the function. The input will clearly be a, b and c, but what about outputs? There are only printed statements to screen so in fact the returned value is nil or void. As such we can write the definition as

voidsolveQuadratic(double a,double b,double c) { }

Enter this function definition into your code above the main function and below the libraries.

Now we can copy/paste all statements from the main into solveQuadratic (but not the return statement since there is no return value in this function). The only things that need to be changed are to remove a, b, and c from being redeclared and reassigned. The function can be called from main and you final program should look like:-

#include<iostream>#include<cmath>usingnamespacestd;// function to solve a quadratic equation// a x^2 + b x + c = 0// takes a, b, c as real number inputs// output roots to the screenvoidsolveQuadratic(double a,double b,double c) {// declare local variablesdouble discriminant;// calculate the discriminantdiscriminant = b * b - 4. * a * c; cout << " discriminant = " << discriminant << endl;// use if statement to change outputif(discriminant < 0.) { cout << " The roots are complex and are " << endl; cout << " x+ = " << -b/2./a << "+" <<sqrt(-discriminant)/2./a << "i" << endl; cout << " x- = " << -b/2./a << "-" <<sqrt(-discriminant)/2./a << "i" << endl; }else{ cout << " The roots are real and are" << endl; cout << " x+ = " << (-b +sqrt(discriminant))/2./a << endl; cout << " x- = " << (-b -sqrt(discriminant))/2./a << endl; } } intmain() {solveQuadratic(1.,1.,1.);return0; }

## No comments:

## Post a Comment