This problem is based on the Programming Exercise on page 516, #1, from the text
ID: 3840770 • Letter: T
Question
This problem is based on the Programming Exercise on page 516, #1, from the textbook C++ Programming 7th edition - with slight modifications, please take note of this!
Write a program that prompts the user to input the length of the sides of a triangle (as positive integers) and outputs the shape of the triangle.
In the program, define an enumeration type, triangleType, that has the values scalene, isosceles, equilateral, and noTriangle.
Write a function, triangleShape, that takes as parameters three numbers, each of which represents the length of a side of the triangle. The function should return the shape of the triangle.
Display the return value of the function triangleShape in a description.
A triangle is scalene if the three sides are unequal (no two sides are the same length).
A triangle is isosceles if two of its sides are equal (but not all three for purposes of this lab).
A triangle is equilateral if all three sides are the same length.
In a triangle, the sum of the lengths of the two sides is greater than the length of the third side. If this formula is not supported, then the three sides do not describe a triangle.
Explanation / Answer
#include <iostream>
#include <iomanip>
using namespace std;
char triangleShape (double, double, double);
bool isTriangle (double, double, double);
int main()
{
double a, b, c;
double t;
char description;
cout << "Enter first side of the triangle: ";
cin>>a;
cout << "Enter second side of the triangle: ";
cin>>b;
cout << "Enter third side of the triangle: ";
cin>>c;
t = isTriangle(a,b,c);
if(t == true)
{
description = triangleShape (a,b,c);
if (description == 'e')
cout << "This is an equilateral triangle" << endl;
else if(description == 'i')
cout << "This is an isosceles triangle" << endl;
else if(description =='s')
cout <<"This is a scalene triangle" << endl;
else if(description == 'n')
cout <<" These three sides do not describe a triangle "<<endl;
}
else
{
cout<<" Please enter positive length for triangle sides "<<endl;
}
}
bool isTriangle (double a, double b, double c)
{
if ( a < 0 || b < 0 || c < 0)
return false;
else
return true;
}
char triangleShape (double a, double b, double c)
{
if (a == b && b == c)
return 'e';
else if (a == b || b == c || a == c)
return 'i';
else if (a+b<c || a+c<b || b+c<a)
return 'n';
else
return 's';
}
Note:
In this programme, i am using two functions i.e.,
isTriangle() -- if user enters any negitive value for length of the side, it will give error message;
triangleShape() -- it will returns the type of the triangle.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.