C++ Examples - Logic
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
- Declare variables
- Assign values
- Calculate
- If real Output real roots
- If complex Output complex roots
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
Compile and run and check your program outputs a black terminal.
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
// do nothing yet
return 0;
}
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.
and assign default values to the parameters
double a,b,c,discriminant;
Again check your program at every stage that it compiles and runs.
a=1.;b=1.;c=1.;
Next we can calculate the discriminant from the formula as
Check that the value of the discriminant appear to be correct.
discriminant = b * b - 4. * a * c;
cout << " discriminant = " << discriminant << endl;
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
Enter this function definition into your code above the main function and below the libraries.
void solveQuadratic(double a,double b,double c)
{
}
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:
and the function can be called from main by writing
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;
}
}
int main()
{
solveQuadratic(1.,1.,1.);
return 0;
}
Comments
Post a Comment