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 |
#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 &root1, double &root2, bool &hasRealRoots) { double discriminant = b * b - 4 * a * c; if (discriminant > 0) { // Two distinct real roots root1 = (-b + sqrt(discriminant)) / (2 * a); root2 = (-b - sqrt(discriminant)) / (2 * a); hasRealRoots = true; } else if (discriminant == 0) { // One real root (repeated) root1 = root2 = -b / (2 * a); hasRealRoots = true; } else { // No real roots hasRealRoots = false; } } // 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 &hasRealRoots) { double discriminant = b * b - 4 * a * c; if (discriminant > 0) { // Two distinct real roots root1 = (-b + sqrt(discriminant)) / (2 * a); root2 = (-b - sqrt(discriminant)) / (2 * a); hasRealRoots = true; } else if (discriminant == 0) { // One real root (repeated) root1 = root2 = -b / (2 * a); hasRealRoots = true; } else { // No real roots hasRealRoots = false; } } int main() { double a, b,
c; double root1ByValue, root2ByValue; double root1ByRef, root2ByRef; bool hasRealRootsByValue, hasRealRootsByRef; // Input the coefficients of the quadratic equation cout << "Enter coefficient a: "; cin >> a; cout << "Enter coefficient b: "; cin >> b; cout << "Enter coefficient c: "; cin >> c; // Find roots using pass-by-value findRootsByValue(a, b, c, root1ByValue, root2ByValue, hasRealRootsByValue); if (hasRealRootsByValue) { cout << "Using pass-by-value: Roots are " << root1ByValue << " and " << root2ByValue << "." << endl; } else { cout << "Using pass-by-value: No real roots." << endl; } // Find roots using pass-by-reference findRootsByReference(a, b, c, root1ByRef, root2ByRef, hasRealRootsByRef); if (hasRealRootsByRef) { cout << "Using pass-by-reference: Roots are " << root1ByRef << " and " << root2ByRef << "." << endl; } else { cout << "Using pass-by-reference: No real roots." << endl; } return 0; } |
Explanation:
- 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 tofalse
.
- The roots and the real roots flag are passed by reference so that they can be updated and accessed outside the function.
- Function Definition:
- 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.
- Function Definition:
- Main Function (
main
):- The user is prompted to input the coefficients , , and of the quadratic equation.
- The program calculates and displays the roots using both methods.
Example Outputs:
Example 1:
Input:
- Coefficient :
1
- Coefficient :
-3
- Coefficient :
2
Output:
1 2 3 4 5 |
Enter coefficient a: 1 Enter coefficient b: -3 Enter coefficient c: 2 Using pass-by-value: Roots are 2 and 1. Using pass-by-reference: Roots are 2 and 1. |
Example 2:
Input:
- Coefficient :
1
- Coefficient :
2
- Coefficient :
1
Output:
1 2 3 4 5 |
Enter coefficient a: 1 Enter coefficient b: 2 Enter coefficient c: 1 Using pass-by-value: Roots are -1 and -1. Using pass-by-reference: Roots are -1 and -1. |
Example 3:
Input:
- Coefficient :
1
- Coefficient :
1
- Coefficient :
1
Output:
1 2 3 4 5 |
Enter coefficient a: 1 Enter coefficient b: 1 Enter coefficient c: 1 Using pass-by-value: No real roots. Using pass-by-reference: No real roots. |