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 |
#include <iostream> using namespace std; // Function to check triangle validity using pass-by-value bool isTriangleValidByValue(double side1, double side2, double side3) { // A triangle is valid if the sum of any two sides is greater than the third side return (side1 + side2 > side3) && (side1 + side3 > side2) && (side2 + side3 > side1); } // Function to check triangle validity using pass-by-reference void isTriangleValidByReference(double side1, double side2, double side3, bool &isValid) { // A triangle is valid if the sum of any two sides is greater than the third side isValid = (side1 + side2 > side3) && (side1 + side3 > side2) && (side2 + side3 > side1); } int main() { double side1, side2, side3; bool isValidByValue, isValidByReference; // Input the sides of the triangle cout << "Enter the first side: "; cin >> side1; cout << "Enter the second side: "; cin >> side2; cout << "Enter the third side: "; cin >> side3; // Check validity using pass-by-value isValidByValue = isTriangleValidByValue(side1, side2, side3); cout << "Using pass-by-value: The triangle is " << (isValidByValue ? "valid" : "not valid") << "." << endl; // Check validity using pass-by-reference isTriangleValidByReference(side1, side2, side3, isValidByReference); cout << "Using pass-by-reference: The triangle is " << (isValidByReference ? "valid" : "not valid") << "." << endl; return 0; } |
Explanation:
- Pass-by-Value (
isTriangleValidByValue
):- Function Definition:
bool isTriangleValidByValue(double side1, double side2, double side3)
- This function takes three sides of a triangle as input parameters by value.
- It checks if the triangle is valid using the triangle inequality theorem, which states that the sum of any two sides must be greater than the third side.
- It returns
true
if the triangle is valid, otherwise it returnsfalse
.
- Function Definition:
- Pass-by-Reference (
isTriangleValidByReference
):- Function Definition:
void isTriangleValidByReference(double side1, double side2, double side3, bool &isValid)
- This function takes three sides of a triangle as input and a reference variable
isValid
to store the result. - It calculates whether the triangle is valid using the same triangle inequality theorem and updates the
isValid
reference variable.
- Function Definition:
- Main Function (
main
):- The program prompts the user to input the three sides of the triangle.
- It checks if the triangle is valid using both methods and prints the results.
Example Outputs:
Example 1:
Input:
- Side 1:
3
- Side 2:
4
- Side 3:
5
Output:
1 2 3 4 5 |
Enter the first side: 3 Enter the second side: 4 Enter the third side: 5 Using pass-by-value: The triangle is valid. Using pass-by-reference: The triangle is valid. |
Example 2:
Input:
- Side 1:
10
- Side 2:
2
- Side 3:
5
Output:
1 2 3 4 5 |
Enter the first side: 10 Enter the second side: 2 Enter the third side: 5 Using pass-by-value: The triangle is not valid. Using pass-by-reference: The triangle is not valid. |
Example 3:
Input:
- Side 1:
6
- Side 2:
8
- Side 3:
10
Output:
1 2 3 4 5 |
Enter the first side: 6 Enter the second side: 8 Enter the third side: 10 Using pass-by-value: The triangle is valid. Using pass-by-reference: The triangle is valid. |