1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
#include <iostream> #include <cmath> // For sqrt() function using namespace std; // Function to find roots of the quadratic equation using pass-by-value void findRootsByValue(double a, double b, double c) { double discriminant = b * b - 4 * a * c; double root1, root2; // Check if the discriminant is positive, zero, or negative if (discriminant > 0) { root1 = (-b + sqrt(discriminant)) / (2 * a); root2 = (-b - sqrt(discriminant)) / (2 * a); cout << "Roots are real and different." << endl; cout << "Root 1 (By Value): " << root1 << endl; cout << "Root 2 (By Value): " << root2 << endl; } else if (discriminant == 0) { root1 = root2 = -b / (2 * a); cout << "Roots are real and the same." << endl; cout << "Root 1 (By Value): " << root1 << endl; cout << "Root 2 (By Value): " << root2 << endl; } else { double realPart = -b / (2 * a); double imaginaryPart = sqrt(-discriminant) / (2 * a); cout << "Roots are complex and different." << endl; cout << "Root 1 (By Value): " << realPart << " + " << imaginaryPart << "i" << endl; cout << "Root 2 (By Value): " << realPart << " - " << imaginaryPart << "i" << endl; } } // Function to find roots of the quadratic equation using pass-by-reference void findRootsByReference(double a, double b, double c, double &root1, double &root2, bool &isComplex) { double discriminant = b * b - 4 * a * c; // Check if the discriminant is positive, zero, or negative if (discriminant > 0) { root1 = (-b + sqrt(discriminant)) / (2 * a); root2 = (-b - sqrt(discriminant)) / (2 * a); isComplex = false; } else if (discriminant == 0) { root1 = root2 = -b / (2 * a); isComplex = false; } else { root1 = -b / (2 * a); root2 = sqrt(-discriminant) / (2 * a); isComplex = true; } } int main() { double a, b, c; double root1, root2; bool isComplex; // Input coefficients of the quadratic equation cout << "Enter coefficients a, b, and c of the quadratic equation (ax^2 + bx + c = 0): "; cin >> a >> b >> c; // Find roots using pass-by-value cout << "=== Pass-by-Value Calculation ===" << endl; findRootsByValue(a, b, c); // Find roots using pass-by-reference cout << "\n=== Pass-by-Reference Calculation ===" << endl; findRootsByReference(a, b, c, root1, root2, isComplex); if (isComplex) { cout << "Roots are complex and different." << endl; cout << "Root 1 (By Reference): " << root1 << " + " << root2 << "i" << endl; cout << "Root 2 (By Reference): " << root1 << " - " << root2 << "i" << endl; } else { cout << "Roots are real." << endl; cout << "Root 1 (By Reference): " << root1 << endl; cout << "Root 2 (By Reference): " << root2 << endl; } return 0; } |
Explanation
1. Pass-by-Value (findRootsByValue
Function)
- Definition:
1void findRootsByValue(double a, double b, double c)
- Parameters: Accepts the coefficients , , and by value.
- Operation:
- Calculates the discriminant ().
- Depending on the value of the discriminant:
- If positive, computes two distinct real roots.
- If zero, computes one real root (both roots are the same).
- If negative, computes the real and imaginary parts of the complex roots.
- Displays the roots based on their nature.
- Effect:
- The original values of , , and remain unchanged after the function call.
2. Pass-by-Reference (findRootsByReference
Function)
- Definition:
1void findRootsByReference(double a, double b, double c, double &root1, double &root2, bool &isComplex)
- Parameters:
a
,b
,c
: Coefficients of the quadratic equation.root1
,root2
: Reference variables to store the roots.isComplex
: Reference variable to indicate whether the roots are complex.
- Operation:
- Similar calculation to
findRootsByValue
. - Updates
root1
,root2
, andisComplex
based on the discriminant.
- Similar calculation to
- Effect:
- The variables
root1
,root2
, andisComplex
are directly updated based on the calculations inside the function.
- The variables
- Parameters:
3. Input Handling and Calculation
- Input: The program prompts the user to enter the coefficients , , and of the quadratic equation.
- Processing:
- Roots are computed using both pass-by-value and pass-by-reference methods.
- The program displays the results of both methods.
Sample Output:
1 2 3 4 5 6 7 8 9 10 |
Enter coefficients a, b, and c of the quadratic equation (ax^2 + bx + c = 0): 1 -3 2 === Pass-by-Value Calculation === Roots are real and different. Root 1 (By Value): 2 Root 2 (By Value): 1 === Pass-by-Reference Calculation === Roots are real. Root 1 (By Reference): 2 Root 2 (By Reference): 1 |
Key Points
- Quadratic Formula:
- The roots of a quadratic equation are computed using the formula:
- The discriminant () determines the nature of the roots:
- Positive discriminant: Two distinct real roots.
- Zero discriminant: One real root (repeated).
- Negative discriminant: Two complex roots.
- The roots of a quadratic equation are computed using the formula:
- Pass-by-Value:
- The function receives copies of the input parameters.
- The original coefficients are not modified.
- Pass-by-Reference:
- The function modifies the
root1
,root2
, andisComplex
variables directly. - Any changes affect the original variables in
main
.
- The function modifies the
- Complex Roots:
- For negative discriminant, the program calculates complex roots and displays them in the form .