C++ Examples - Logic

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:-

  • 1. Include all libraries
  • 2. Declare all variables and assign with default values
  • 3. Calculate discriminant and output to screen
  • 4. Use if condition to evaluate if real or not and output roots
  • 5. 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>
using namespace std;

int main()
{
// do nothing yet
return 0;
}
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>
using namespace std;

int main()
{
// declare all variables
double a,b,c,discriminant;
// assign default values
a=1.;b=1.;c=1.;
// calculate the discriminant
discriminant = b * b - 4. * a * c;
cout << " discriminant = " << discriminant << endl;
// use if statement to change output
if(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;
}
return 0;
}

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


void solveQuadratic(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 will look like:


void solveQuadratic(double a,double b,double c)
{
// declare local variables
double discriminant;
// calculate the discriminant
discriminant = b * b - 4. * a * c;
cout << " discriminant = " << discriminant << endl;
// use if statement to change output
if(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;
}
}
and the function can be called from main by writing

int main()
{
solveQuadratic(1.,1.,1.);
return 0;
}

Comments

Popular posts from this blog

Exclusive Interview: 2011 8TV Hot Chef (美食型男) Jason Cha

Career Lessons from Breaking Bad

Things We Now Know