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(int angle1, int angle2, int angle3) { // Check if the sum of angles is exactly 180 degrees return (angle1 + angle2 + angle3 == 180); } // Function to check triangle validity using pass-by-reference void isTriangleValidByReference(int angle1, int angle2, int angle3, bool &isValid) { // Check if the sum of angles is exactly 180 degrees isValid = (angle1 + angle2 + angle3 == 180); } int main() { int angle1, angle2, angle3; bool isValidByValue, isValidByReference; // Input the angles cout << "Enter the first angle: "; cin >> angle1; cout << "Enter the second angle: "; cin >> angle2; cout << "Enter the third angle: "; cin >> angle3; // Check validity using pass-by-value isValidByValue = isTriangleValidByValue(angle1, angle2, angle3); cout << "Using pass-by-value: The triangle is " << (isValidByValue ? "valid" : "not valid") << "." << endl; // Check validity using pass-by-reference isTriangleValidByReference(angle1, angle2, angle3, 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(int angle1, int angle2, int angle3)
- This function takes three angles as input parameters by value.
- It checks if the sum of the three angles equals 180 degrees.
- If the sum is exactly 180 degrees, it returns
true
, indicating that the triangle is valid; otherwise, it returnsfalse
.
- Function Definition:
- Pass-by-Reference (
isTriangleValidByReference
):- Function Definition:
void isTriangleValidByReference(int angle1, int angle2, int angle3, bool &isValid)
- This function also takes three angles as input, but the result is passed by reference.
- It calculates whether the sum of the angles is exactly 180 degrees and stores the result in the
isValid
reference variable.
- Function Definition:
- Main Function (
main
):- The program prompts the user to input three angles of a triangle.
- It checks if the triangle is valid using both methods and prints the results.
Example Outputs:
Example 1:
Input:
- First Angle:
60
- Second Angle:
60
- Third Angle:
60
Output:
1 2 3 4 5 |
Enter the first angle: 60 Enter the second angle: 60 Enter the third angle: 60 Using pass-by-value: The triangle is valid. Using pass-by-reference: The triangle is valid. |
Example 2:
Input:
- First Angle:
90
- Second Angle:
45
- Third Angle:
50
Output:
1 2 3 4 5 |
Enter the first angle: 90 Enter the second angle: 45 Enter the third angle: 50 Using pass-by-value: The triangle is valid. Using pass-by-reference: The triangle is valid. |
Example 3:
Input:
- First Angle:
90
- Second Angle:
60
- Third Angle:
30
Output:
1 2 3 4 5 |
Enter the first angle: 90 Enter the second angle: 60 Enter the third angle: 30 Using pass-by-value: The triangle is not valid. Using pass-by-reference: The triangle is not valid. |