Solution to Lecture Quiz 1

Solution to Lecture Quiz 1


////
//
// This program solves the quadratic formula
//
//     (-b +/- sqrt(b^2 - 4ac)) / 2a
//
// It inputs three real numbers for the formula variables a, b, and c.  It
// computes the two solutions for the formula, and outputs the solutions.
//
// Author: Gene Fisher (gfisher@calpoly.edu)
// Created: 21apr99
// Modified: 21apr99
//
////

#include <iostream.h>
#include <math.h>

int main() {

    float a, b, c;              // Formula input variables.
    float pos_solution;         // Variable for the positive solution.
    float neg_solution;         // Variable for the negative solution.

    //
    // Prompt for and input the three variables.
    //
    cout << "Input three real numbers, separated by spaces: ";
    cin >> a >> b >> c;

    //
    // Compute the solutions to the formula.
    //
    pos_solution = (-b + sqrt(pow(b, 2.0) - 4.0 * a * c)) / (2.0 * a);
    neg_solution = (-b - sqrt(pow(b, 2.0) - 4.0 * a * c)) / (2.0 * a);

    //
    // Output the results
    //
    cout << "The positive and negative solutions are: "
         << pos_solution << ", "
         << neg_solution
         << endl;

    return 0;
}


index | lectures | labs | handouts | assignments | solutions | grades | help