C++ Program to find all the roots of a quadratic equation. Using user define functions

Explanation:

  1. Pass-by-Value (findRootsByValue):
    • Function Definition: void findRootsByValue(double a, double b, double c, double &root1, double &root2, bool &hasRealRoots)
    • This function calculates the discriminant of the quadratic equation.
    • Based on the value of the discriminant:
      • If positive, it computes two distinct real roots.
      • If zero, it computes one real root (repeated).
      • If negative, it sets the hasRealRoots flag to false.
    • The roots and the real roots flag are passed by reference so that they can be updated and accessed outside the function.
  2. Pass-by-Reference (findRootsByReference):
    • Function Definition: void findRootsByReference(double a, double b, double c, double &root1, double &root2, bool &hasRealRoots)
    • This function performs the same operations as findRootsByValue but with the results passed by reference.
    • The roots and the real roots flag are updated directly in the calling function.
  3. Main Function (main):
    • The user is prompted to input the coefficients aa, bb, and cc of the quadratic equation.
    • The program calculates and displays the roots using both methods.

Example Outputs:

Example 1:

Input:

  • Coefficient aa: 1
  • Coefficient bb: -3
  • Coefficient cc: 2

Output:

Example 2:

Input:

  • Coefficient aa: 1
  • Coefficient bb: 2
  • Coefficient cc: 1

Output:

Example 3:

Input:

  • Coefficient aa: 1
  • Coefficient bb: 1
  • Coefficient cc: 1

Output: