/* Name: Zach Hoggard Student No.: 250 611 551 Email: zhoggard@uwo.ca Section: 004 Date: February 8, 2012 */ #include #include #include using namespace std; int main () { //Declare the purpose of the program cout << "This program solves a quadratic equation of the form ax^2 + bx + c.\n"; //Declare variables double a, b, c, y1, y2, y3, y4, determinant; //Ask user to input numbers and store numbers in memory cout << "Please input a: "; cin >> a; cout << "\n"; cout << "Please input b: "; cin >> b; cout << "\n"; cout << "Please input c: "; cin >> c; cout << "\n"; //Calculate the determinant determinant = ((b * b) - (4 * (a * c))); //Determine what to do depending on the value of the determinant //If the value of a is 0 and b and c have a non-zero value, calculate the one root if (a == 0 && b != 0 && c != 0) { y4 = -c / b; cout << "Your equation has one real root: " << y4 << "\n"; } else { //If the determinant is 0 the program displays that there is not an answer if (determinant == 0) { cout << "A solution does not exist!\n"; } //If the determinant is greater than 0 then the program calculates the real roots and displays the answer if (determinant > 0) { cout << "Your equation has real roots: "; y1 = (-b + sqrt(determinant))/(2 * a); cout << y1 << " and "; y2 = (-b - sqrt(determinant))/(2 * a); cout << y2 << ".\n"; } //If the determinant is less than 0 the program calculates the complex roots and displays the answer if (determinant < 0) { cout << "Your equation has complex roots: " << -b/2; y3 = (sqrt(-determinant)/2); cout << " + " << y3 << "i\n"; cout << " and " << -b/2 << " - " << y3 << "i.\n"; } } //Program is terminated return 0; }