a. Define an enumeration type, triangleType, that has the values scalene, isosceles, equilateral, and noTriangle.
b. Write a function, triangleShape, that takes as parameters three num- bers, each of which represents the length of a side of the triangle. The function should return the shape of the triangle. (Note: In a triangle, the sum of the lengths of any two sides is greater than the length of the third side.)
c. Write a program that prompts the user to input the length of the sides of a triangle and outputs the shape of the triangle.
#include<iostream>
using namespace std;
enum TriangleType {scalene, isosceles, equilateral, noTriangle};
//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.
//(Note: In a triangle, the sum of the lengths of any two sides is greater than the length of the third side.)
TriangleType triangleShape(int side1,int side2,int side3){
if(side1<=0 || side2<=0 || side3<=0){
return noTriangle;
}
//If all sides are equal
if(side1==side2 && side2==side3){
return equilateral;
}
//If any two sides are equal
else if(side1==side2 || side1==side3 || side2==side3){
return isosceles;
}
else{
//If none sides are equal
return scalene;
}
}
int main(){
int side1;
int side2;
int side3;
// Write a program that prompts the user to input the length of the sides of a triangle and outputs the shape of the triangle.
cout<<"Enter side 1: ";
cin>>side1;
cout<<"Enter side 2: ";
cin>>side2;
cout<<"Enter side 3: ";
cin>>side3;
//Display result
if(triangleShape(side1,side2,side3)==scalene){
printf("Triangle is scalene\n\n");
}
if(triangleShape(side1,side2,side3)==isosceles){
printf("Triangle is isosceles\n\n");
}
if(triangleShape(side1,side2,side3)==equilateral){
printf("Triangle is equilateral\n\n");
}
if(triangleShape(side1,side2,side3)==noTriangle){
printf("It is not a triangle\n\n");
}
system("pause");
return 0;
}
Comments
Leave a comment