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 |
#include <iostream> using namespace std; // Function to determine the type of triangle using pass-by-value string triangleTypeByValue(double side1, double side2, double side3) { if (side1 <= 0 || side2 <= 0 || side3 <= 0) { return "Invalid"; // Side lengths must be positive } if (side1 == side2 && side2 == side3) { return "Equilateral"; } else if (side1 == side2 || side2 == side3 || side1 == side3) { return "Isosceles"; } else { return "Scalene"; } } // Function to determine the type of triangle using pass-by-reference void triangleTypeByReference(double side1, double side2, double side3, string &triangleType) { if (side1 <= 0 || side2 <= 0 || side3 <= 0) { triangleType = "Invalid"; // Side lengths must be positive } else if (side1 == side2 && side2 == side3) { triangleType = "Equilateral"; } else if (side1 == side2 || side2 == side3 || side1 == side3) { triangleType = "Isosceles"; } else { triangleType = "Scalene"; } } int main() { double side1, side2, side3; string triangleTypeByVal, triangleTypeByRef; // 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; // Determine the type of triangle using pass-by-value triangleTypeByVal = triangleTypeByValue(side1, side2, side3); cout << "Using pass-by-value: The triangle is " << triangleTypeByVal << "." << endl; // Determine the type of triangle using pass-by-reference triangleTypeByReference(side1, side2, side3, triangleTypeByRef); cout << "Using pass-by-reference: The triangle is " << triangleTypeByRef << "." << endl; return 0; } |
Explanation:
- Pass-by-Value (
triangleTypeByValue
):- Function Definition:
string triangleTypeByValue(double side1, double side2, double side3)
- This function takes three sides of a triangle as input parameters by value.
- It determines the type of triangle based on the side lengths:
- Equilateral: All three sides are equal.
- Isosceles: Two sides are equal.
- Scalene: All three sides are different.
- If any side length is non-positive, the function returns
"Invalid"
. - It returns a
string
indicating the type of triangle.
- Function Definition:
- Pass-by-Reference (
triangleTypeByReference
):- Function Definition:
void triangleTypeByReference(double side1, double side2, double side3, string &triangleType)
- This function also takes three sides of a triangle as input, but uses a reference variable
triangleType
to store the result. - It calculates the type of triangle using the same conditions and updates the
triangleType
reference variable.
- Function Definition:
- Main Function (
main
):- The program prompts the user to input the three sides of the triangle.
- It determines the type of triangle using both methods and prints the results.
Example Outputs:
Example 1:
Input:
- Side 1:
5
- Side 2:
5
- Side 3:
5
Output:
1 2 3 4 5 |
Enter the first side: 5 Enter the second side: 5 Enter the third side: 5 Using pass-by-value: The triangle is Equilateral. Using pass-by-reference: The triangle is Equilateral. |
Example 2:
Input:
- Side 1:
5
- Side 2:
5
- Side 3:
8
Output:
1 2 3 4 5 |
Enter the first side: 5 Enter the second side: 5 Enter the third side: 8 Using pass-by-value: The triangle is Isosceles. Using pass-by-reference: The triangle is Isosceles. |
Example 3:
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 Scalene. Using pass-by-reference: The triangle is Scalene. |
Example 4:
Input:
- Side 1:
0
- Side 2:
5
- Side 3:
5
Output:
1 2 3 4 5 |
Enter the first side: 0 Enter the second side: 5 Enter the third side: 5 Using pass-by-value: The triangle is Invalid. Using pass-by-reference: The triangle is Invalid. |