Project 6 consists of one C++ Program: Concepts tested by this program: 1. To wo
ID: 3764908 • Letter: P
Question
Project 6 consists of one C++ Program: Concepts tested by this program: 1. To work with functions 2. To work with Boolean expressions 3. To learn to sort 3 scalar values 4. To learn the advantages of sorting values first 5. To learn the advantage of using functions 6. To learn to test all possible paths in a program 7. To learn to write a longer program step by step.
Program: Write, compile, and run a C++ program that will allow the user to enter sets of 3 lengths and then tell what kind of triangle the 3 lengths form. Function main should consist mainly of a loop and calls to functions. All of the work is done inside functions. The main loop should end when the user enters 0 for each side.
Terms used to describe triangles: Equilateral: the lengths of all 3 sides are equal Isosceles: any two sides (but not all 3) are equal Scalene: no sides are equal Right: C*C = A*A + B*B Obtuse: C*C > A*A + B*B Acute: C*C < A*A + B*B It is possible that the user enters 3 lengths that cannot make a triangle, 3, 4, and 10 for example. If the 3 lengths cannot make a triangle, display an appropriate message. (If not a triangle, you should not then display obtuse or isosceles for instance.) Method: If you sort the 3 sides such that a<=b<=c it is easier to do the next steps. For instance, if we know the sides are in order and a==c then all 3 sides must be equal. In addition to main, your program should have the following functions: • A function to swap two double values. • A function to sort the 3 sides such that a<=b<=c • A function to input the 3 sides. • A function to return or display equilateral, isosceles, or scalene. • A function to return or display right, acute, or obtuse. • A function that returns true if the 3 values can form a triangle, false if not.
Explanation / Answer
#include <iostream>
#include <string>
using namespace std;
const int EQUILATERAL = 1;
const int ISOSCELES = 2;
const int SCALENE = 3;
const int RIGHT = 4;
const int ACUTE = 5;
const int OBTUSE = 6;
void getSides(double sides[]) {
int i = 0;
for(i = 0; i < 3; i++) {
cout << "Enter side " << (i+1) << " of triangle : " <<endl;
cin >> sides[i];
}
}
bool isTriangle(double sides[]) {
int i = 0;
for(i = 0; i < 3; i++) {
if(sides[i] < 0)
return false;
}
//sum of two sides should be greater than 3rd side
if(sides[0]+sides[1] > sides[2] && sides[0]+sides[2] > sides[1] && sides[1]+sides[2] > sides[0] )
return true;
return false;
}
int triangleTypeEIS(double sides[]) {
if(sides[0] == sides[1] && sides[0] == sides[2])
return EQUILATERAL;
else if(sides[0] == sides[1] || sides[1] == sides[2] || sides[0] == sides[2])
return ISOSCELES;
else
return SCALENE;
}
int trinagleTypeRAO(double sides[]) {
//Right: C*C = A*A + B*B Obtuse: C*C > A*A + B*B Acute: C*C < A*A + B*B
if(sides[2] * sides[2] == (sides[0] * sides[0] + sides[1] * sides[1]))
return RIGHT;
else if(sides[2] * sides[2] > (sides[0] * sides[0] + sides[1] * sides[1]))
return OBTUSE;
else if(sides[2] * sides[2] < (sides[0] * sides[0] + sides[1] * sides[1]))
return ACUTE;
}
int main() {
string types[] = {"EQUILATERAL", "ISOSCELES", "SCALENE", "RIGHT Angle", "OBTUSE" , "ACUTE" };
double sides[3];
getSides(sides);
if(isTriangle(sides)) {
int type1 = triangleTypeEIS(sides);
int type2 = trinagleTypeRAO(sides);
cout << " Triangle Type: "<< types[type1] << " and " << types[type2] <<endl;
} else {
cout << "Can not form traingle with given sides" <<endl;
}
}
----------------output------------------
Enter side 1 of triangle :
3
Enter side 2 of triangle :
4
Enter side 3 of triangle :
5
Triangle Type: RIGHT Angle and OBTUSE
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.