In: Computer Science
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, determine if the scalene triangle is also right triangle. (A^2+ B^2= C^2, 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.
Examples
of test data
3 4 5
1 2 0
7 -3 5
6 6 4
7 7 7
2 2 4
#include <iostream> #include <fstream> using namespace std; bool isValid(int a, int b, int c) { if(a < 0 || b < 0 || c < 0) { return false; } if(a > (b + c)) { return false; } if(b > (a + c)) { return false; } if(c > (a + b)) { return false; } return true; } void processTriangle(int a, int b, int c) { if(!isValid(a, b, c)) { cout << "Invalid triangle" << endl; } else { if(a == b == c) { cout << "equilateral triangle" << endl; } else if(a == b || a == c || b == c) { cout << "isosceles triangle" << endl; } else { if(a > b && a > c && (a*a == (b*b + c*c))) { cout << "Right triangle" << endl; } else if(b > a && b > c && (b*b == (a*a + c*c))) { cout << "Right triangle" << endl; } else if(c > b && c > a && (c*c == (b*b + a*a))) { cout << "Right triangle" << endl; } else { cout << "Scalene triangle" << endl; } } } } int main() { ifstream f("input.txt"); if(f.fail()) { cout << "File input.txt not found." << endl; } else { int a, b, c; while(f >> a >> b >> c) { processTriangle(a, b, c); } } }
************************************************** Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.