Quadratic Equation Program

quadratic-starter.cpp

Download file: Windows | macOS | Unix

 1// Program    : Quadratic Equation (starter file)
 2// Author     : Prof. Krofchok
 3// Date       : Fall 2025
 4// Description: This program solves quadratic equations having real roots.
 5
 6#include <iostream>
 7
 8void welcome();
 9void input_data(double &a, double &b, double &c);
10void perform_calculations(double a, double b, double c,
11    double &root1, double &root2);
12void output_results(double a, double b, double c,
13    double root1, double root2);
14
15int main()
16{
17    double a, b, c;       // coefficients of Ax^2 + Bx + C = 0
18    double root1, root2;  // roots of the equation
19
20    welcome();
21    input_data(a, b, c);
22    perform_calculations(a, b, c, root1, root2);
23    output_results(a, b, c, root1, root2);
24}
25
26//
27// An output module that displays information about the program.
28//
29void welcome()
30{
31    std::cout << "This program computes and displays the"  << std::endl;
32    std::cout << "roots of a second-degree polynomial"     << std::endl;
33    std::cout << "equation of the form Ax^2 + Bx + C = 0." << std::endl;
34    std::cout                                              << std::endl;
35    std::cout << "You will be prompted to input the three" << std::endl;
36    std::cout << "coefficients A, B, and C."               << std::endl;
37    std::cout                                              << std::endl;
38    std::cout << "This program acts unpredictably when"    << std::endl;
39    std::cout << "given coefficients that do not produce"  << std::endl;
40    std::cout << "real-valued roots."                      << std::endl;
41    std::cout                                              << std::endl;
42}
43
44//
45// An input module that gets the coefficients of a second-degree polynomial
46// in the form Ax^2 + Bx + C
47//
48void input_data(double &a, double &b, double &c)
49{
50    std::cout << "Enter coefficient 'A': ";
51    std::cin >> a;
52    std::cout << "Enter coefficient 'B': ";
53    std::cin >> b;
54    std::cout << "Enter coefficient 'C': ";
55    std::cin >> c;
56}
57
58//
59// A processing module that computes the real-valued roots of the equation,
60// given its coefficients.
61//
62void perform_calculations(double a, double b, double c,
63    double &root1, double &root2)
64{
65}
66
67//
68// An output module that displays the equation and its roots.
69//
70void output_results(double a, double b, double c,
71    double root1, double root2)
72{
73    std::cout << std::endl;
74    std::cout << "The roots of the equation "
75              << a << "x^2 + "
76              << b << "x + "
77              << c << std::endl;
78    std::cout << "are " << root1 << " and " << root2 << std::endl;
79}