/*********************************************** * * * * * CSCI 1470, Fall 2005 * * Machine Problem: MP * * Purpose: Compute real and complex roots * *of a quadratic equation a set number of times* ***********************************************/ #include #include #include using namespace std; int main(void) { //Declare variables double num_equations, a, b, c, root1, root2, d, com_root; int i; bool test_d; //Find out how many equations the user wants to run cout << "Enter number of equations to calculate: "; cin >> num_equations; //For loop runs the number of times specified by the for loop for(i = 1; i <= num_equations; i = i + 1) { // Get data from user with prompt cout << "Enter the coefficient of x^2 in the equation:"; cin >> a; cout << endl; cout << "Enter the coeffecient of x in the equation:"; cin >> b; cout << endl; cout << "Enter the constant in the equation:"; cin >> c; cout << endl; cout << "................................................................................" << endl << endl; cout << "Equation is: " << a << " * x^2 + " << b << " * x + " << c << " = 0" << endl << endl;; d = (pow(b,2) - 4 * a * c); test_d = d >= 0; switch(test_d) { case(1): cout << "Real Roots" << endl << endl; root1 = (-b + sqrt(d)) / (2 * a); root2 = (-b - sqrt(d)) / (2 * a); cout << "Root1 = " << setprecision(3) << fixed << root1 << " (formatted to three decimal places)" << endl << endl; cout << "Root2 = " << setprecision(3) << fixed << root2 << " (formatted to three decimal places)" << endl << endl; cout << "................................................................................"; cout << endl << endl; break; case(0): cout << "Complex Roots" << endl << endl; com_root = (sqrt(fabs(d))) / (2 * a); cout << "Root1 = " << -b / (2 * a) << " + i * " << setprecision(3) << fixed << com_root << " (formatted to three decimal places)" << endl << endl; cout << "Root2 = " << -b / (2 * a) << " - i * " << setprecision(3) << fixed << com_root << " (formatted to three decimal places)" << endl << endl; cout << "................................................................................"; cout << endl << endl; break; }//end of switch }//end of for loop return 0; }//end of function main