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 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...
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
Write a C program with the following prompt *do not use gotostatement* "Write an interactive...
Write a C program with the following prompt *do not use goto statement* "Write an interactive program that implements a simple calculator. The program should allow the user to choose a binary arithmetic operation and enter two terms to which to apply the operation. The program should then compute the result and display it to the user. Your calculator will have two modes: double-precision mode and integer mode. Double-precision mode will do calculations and output using variables of type double....
Argument A Since all triangles have three sides, and an isosceles triangle is a triangle, it...
Argument A Since all triangles have three sides, and an isosceles triangle is a triangle, it follows that an isosceles triangle has three sides. Argument a is: inductive/deductive; valid/invalid/strong/weak; sound/unsound/cogent/uncogent? Argument B There is a large crack in Philadelphia's Liberty Bell. From this, we can conclude that there was a defect in the bell's craftsmanship. Argument B is: inductive/deductive; valid/invalid/strong/weak; sound/unsound/cogent/uncogent? Argument C Paleontologists now agree that the Tyrannosaurus Rex was actually a peaceful, plant-eating dinosaur. Therefore, it is probably...
In C write a program that asks the user for the dimensions of 3 sides of...
In C write a program that asks the user for the dimensions of 3 sides of a triangle. Each side dimension must be between 1.00 and 100.00 including 1.00 and 100.00. If the side dimensions indeed can make a valid triangle then the program should use these values to determine the perimeter and the area of the valid triangle 1. Must use programmer define functions 2. Your program must have at least 4 functions, main and the following 3 functions:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT