C++ Program Implementation
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 |
#include <iostream> using namespace std; // Function to check if a triangle is valid using pass-by-value void checkTriangleByValue(int side1, int side2, int side3) { bool isValid = false; switch ((side1 + side2 > side3) + (side2 + side3 > side1) + (side1 + side3 > side2)) { case 3: isValid = true; break; default: isValid = false; break; } if (isValid) { cout << "The triangle is valid (By Value)." << endl; } else { cout << "The triangle is not valid (By Value)." << endl; } } // Function to check if a triangle is valid using pass-by-reference void checkTriangleByReference(int &side1, int &side2, int &side3, bool &isValid) { switch ((side1 + side2 > side3) + (side2 + side3 > side1) + (side1 + side3 > side2)) { case 3: isValid = true; break; default: isValid = false; break; } if (isValid) { cout << "The triangle is valid (By Reference)." << endl; } else { cout << "The triangle is not valid (By Reference)." << endl; } } int main() { int side1 = 3, side2 = 4, side3 = 5; // Example sides of a triangle bool isValid; // Check triangle validity using pass-by-value cout << "=== Pass-by-Value Check ===" << endl; checkTriangleByValue(side1, side2, side3); // Check triangle validity using pass-by-reference cout << "\n=== Pass-by-Reference Check ===" << endl; checkTriangleByReference(side1, side2, side3, isValid); return 0; } |
Explanation
1. Pass-by-Value (checkTriangleByValue
Function)
- Definition:
1void checkTriangleByValue(int side1, int side2, int side3)
- Parameters: Takes the lengths of the triangle’s sides as inputs by value.
- Operation:
- The function checks the validity of the triangle using the triangle inequality theorem: the sum of the lengths of any two sides must be greater than the length of the remaining side.
- The switch statement evaluates whether all conditions hold true.
- Displays whether the triangle is valid or not.
- Effect:
- Since the function uses pass-by-value, the original values of the sides remain unchanged after the function call.]
Pass-by-Reference (checkTriangleByReference
Function)
- Definition:
-
1void checkTriangleByReference(int &side1, int &side2, int &side3, bool &isValid)
- Parameters:
side1
,side2
,side3
: References to the lengths of the triangle’s sides.isValid
: A reference to a boolean variable that stores whether the triangle is valid.
- Operation:
- The function checks the validity of the triangle using the triangle inequality theorem, as in the pass-by-value function.
- The result is stored in the
isValid
variable. - Displays whether the triangle is valid or not.
- Effect:
- Since the function uses pass-by-reference, the
isValid
variable is directly modified with the result after the function call.
- Since the function uses pass-by-reference, the
- Parameters:
Sample Output:
1 2 3 4 5 |
=== Pass-by-Value Check === The triangle is valid (By Value). === Pass-by-Reference Check === The triangle is valid (By Reference). |
Key Points
- Triangle Inequality Theorem:
- A triangle is valid if the sum of the lengths of any two sides is greater than the length of the remaining side.
- Pass-by-Value:
- The function receives copies of the actual arguments.
- Changes made inside the function do not affect the original variables.
- Pass-by-Reference:
- The function receives references (aliases) to the actual arguments.
- The
isValid
variable is directly updated based on the validation inside the function.
- Switch Statement:
- The switch statement is used to handle the validation logic.
- It checks if all three conditions of the triangle inequality theorem are satisfied.