CPPForSchool.com: Flow of Control Set 1: Assignment 10

10. Write a program to find the roots of and quadratic equation of type ax²+bx+c where a is not equal to zero.

Oh goodness, this assignment brought me back to high school.  Of course, I had to go and research the quadratic equation.  It felt like I opened a can of worms that exploded with math.  Now that my math nightmares are reaching out via this assignment, I dove into MathsIsFun in order to reclaim some dusty knowledge.  In order to find the roots, I must break this down using two formulas.  This is due to the fact that the quadratic formula uses a Plus/Minus operator within the coefficient.



Code wise, this would equate to the following formulas:

x = -b + sqrt((b * b) - 4ac) / 2*a;
x = -b - sqrt((b * b) - 4ac) / 2*a;

However, to help simplify the formulas, I will use a variable to represent the "(b * b) - 4ac)" portion of the equation.  With that all out of the way, I went ahead and began coding.  Below is my source code and outputs.  It is important to note that I had to reference Programiz to ensure my approach was sufficient.  Below is my source code, followed by test outputs based on the sample solution.












Comments