Question

In: Computer Science

Assignment: Write an interactive C++·program to determine a type of a triangle based on three sides....

Assignment:

Write an interactive C++·program to determine a type of a triangle based on three sides.

  • Have a user input the length of three sides of a triangle. Allow the user to enter the sides in any order.
  • Determine if the entered sides form a valid triangle. The triangle may not have negative sides. The sum of any two sides must be greater than the third side to form a triangle.
  • Determine if the entered sides are part of a scalene, isosceles, or equilateral triangle. Using the Pythagorean Theorem, determine if the scalene or isosceles triangle is a right triangle. (A squared plus B squared equals C squared, where C is the hypotenuse.)
  • Adequately check entered data for validity. Use adequate test data to process all valid data and representative combinations of invalid data.
  • Label output clearly.
  • Be sure your output printout contains user prompts and what was entered by the user in addition to the results of your program processing.
  • Test your program thoroughly

Input

Length of three sides of a triangle.

Output

Length of three sides; type of triangle; whether or not a triangle is a right triangle. Output an appropriate error message for invalid lengths entered for one or more sides or side lengths that cannot form a triangle.

Solutions

Expert Solution

C++ code:

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

// function to check if the valid triangle can be formed
bool is_valid(int a, int b, int c){
    if ( a+b <= c) return false;
    if ( b+c <= a) return false;
    if ( c+a <= b) return false;
    return true;
}


int main() {

    // Take input
    float a, b, c;
    cout << "Enter the three sides of a triangle separated by a space: ";
    cin >> a >> b >> c;

    // Invalid lengths
    if ( a <= 0 || b <= 0 || c <= 0 ){
        cout << "Invalid lengths have been entered" << endl;
    }
    
    // Invalid triangle
    else if (is_valid(a, b, c) == false){
        cout << "The given lengths cannot form a triangle" << endl;
    }

    // Equilateral Triangle
    else if ( a == b && b == c){
        cout << a << " " << b << " " << c << "; " << "Equilateral Triangle; " << "Not a right triangle" << endl; 
    }

    // Isosceles Triangle
    else if ( a == b || b == c || c == a){
        if ( pow(a,2) + pow(b,2) == pow(c,2) || pow(b,2) + pow(c,2) == pow(a,2) || pow(c,2) + pow(a,2) == pow(b,2) ){
            cout << a << " " << b << " " << c << "; " << "Isosceles Triangle; " << "Is a right triangle" << endl; 
        }else{
            cout << a << " " << b << " " << c << "; " << "Isosceles Triangle; " << "Not a right triangle" << endl; 
        }
    }

    // Scalene Triangle
    else{
        if ( pow(a,2) + pow(b,2) == pow(c,2) || pow(b,2) + pow(c,2) == pow(a,2) || pow(c,2) + pow(a,2) == pow(b,2) ){
            cout << a << " " << b << " " << c << "; " << "Scalene Triangle; " << "Is a right triangle" << endl; 
        }else{
            cout << a << " " << b << " " << c << "; " << "Scalene Triangle; " << "Not a right triangle" << endl; 
        }
    }

    return 0;
}


Sample execution of the above code:


Related Solutions

Write a program that accepts the lengths of three sides of a triangle as inputs. The...
Write a program that accepts the lengths of three sides of a triangle as inputs. The program output should indicate whether or not the triangle is a right triangle. Recall from the Pythagorean theorem that in a right triangle, the square of one side equals the sum of the squares of the other two sides. Use The triangle is a right triangle. and The triangle is not a right triangle. as your final outputs. An example of the program input...
For this computer assignment, you are to write and implement an interactive C++ program to find...
For this computer assignment, you are to write and implement an interactive C++ program to find and print all prime numbers, which are less than or equal to a given value of n, using the algorithm known as the Sieve of Eratosthenes. A prime number p is an integer greater than 1 that is divisible only by 1 and p (itself). The algorithm begins by initializing a set container to contain all the integers in the range 2 to n....
Write a program that prompts for the lengths of the sides of a triangle and reports...
Write a program that prompts for the lengths of the sides of a triangle and reports the three angles. Make sure you have a good introduction stating what the program does when it is run, and a helpful prompt for the user when asking for input: i.e. Here is the generated output from a sample program: This program computes the angles of a triangle given the lengths of the sides. What is the length of side 1? <wait for user...
Write an interactive C++·program to have a user input their three initials and the length of...
Write an interactive C++·program to have a user input their three initials and the length of three sides of a triangle. Allow the user to enter the sides in any order. Determine if the entered sides form a valid triangle.The triangle may not have negative sides.The sum of any two sides must be greater than the third side to form a triangle. Determine if the entered sides are part of a scalene, isosceles, or equilateral triangle. Using the Pythagorean Theorem,...
(PYTHON) Write aprogram that prompts user to enter three sides of a triangle....
(PYTHON) Write a program that prompts user to enter three sides of a triangle. The program should determine if the three sides can form a triangle. If the three sides can form a triangle, then determine the type of the triangle.There are three types of triangles: Equilateral triangle (all 3 sides are equal) Isosceles triangle (two sides are equal, the third side is of a different length) Scalene triangle (all 3 sides are of different lengths)   The program should...
“Triangle Guessing” game in Python The program accepts the lengths of three (3) sides of a...
“Triangle Guessing” game in Python The program accepts the lengths of three (3) sides of a triangle as input . The program output should indicate if the triangle is a right triangle, an acute triangle, or an obtuse triangle. Make sure each side of the triangle is a positive integer. Use try/except for none positive integer input. validating the triangle. That is the sum of the lengths of any two sides of a triangle is greater than the length of...
An equilateral triangle is a triangle whose sides are equal. You are to write a class...
An equilateral triangle is a triangle whose sides are equal. You are to write a class called Triangle, using filenames triangle.h and triangle.cpp, that will allow the creation and handling of equilateral triangles, whose sides are integers in the range 1-39. Details: 1. The single constructor for the Triangle class should have 3 parameters: an integer size (required), which is the length of a side; a border character (optional, with a default of '#'); and a fill character (optional, with...
5.11 LAB: Drawing a right triangle c++ This program will output a right triangle based on...
5.11 LAB: Drawing a right triangle c++ This program will output a right triangle based on user specified height triangleHeight and symbol triangleChar. (1) The given program outputs a fixed-height triangle using a * character. Modify the given program to output a right triangle that instead uses the user-specified triangleChar character. (1 pt) (2) Modify the program to use a nested loop to output a right triangle of height triangleHeight. The first line will have one user-specified character, such as...
Q6: (Sides of a Right Triangle) Write a function that reads three nonzero integers and determines...
Q6: (Sides of a Right Triangle) Write a function that reads three nonzero integers and determines whether they are the sides of a right-angled triangle. The function should take three integer arguments and return 1 (true) if the arguments comprise a right-angled triangle, and 0 (false) otherwise. Use this function in a program that inputs a series of sets of integers. Hint: a^2+b^2=C^2 Codes in C please.s
Q6: (Sides of a Right Triangle) Write a function that reads three nonzero integers and determines...
Q6: (Sides of a Right Triangle) Write a function that reads three nonzero integers and determines whether they are the sides of a right-angled triangle. The function should take three integer arguments and return 1 (true) if the arguments comprise a right-angled triangle, and 0 (false) otherwise. Use this function in a program that inputs a series of sets of integers. Hint: a^2+b^2=C^2
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT