0

I need help with the following code that requires me to:

  • Declare 3 double type variables, each representing one of three sides of a triangle.

  • Prompt the user to input a value for the first side, then

  • Set the user’s input to the variable you created representing the first side of the triangle.

  • Repeat the last 2 steps twice more, once for each of the remaining 2 sides of the triangle.

  • Use a series of nested if / else statements to determine if the triangle having side-lengths as set by the user is an EQUILATERAL, ISOSCELES, or SCALENE triangle. [Note: look to the Wikipedia page on ‘triangle’ for definitions of these three types of triangles.]

  • Print the resulting triangle type to the console.

  • Ensure your Triangle detector works by running it 5 times as in the example above. You may use the same values as in the example.

I currently have:

//lab eleven program code on triangles #include <iostream.h> main() { //variables float aside, bside, cside; //enter side a cout<<"enter the length of side a "<<endl; cin>>aside; //enter side b cout<<"enter the length of side b "<<endl; cin>>bside; //enter side c cout<<"enter the length of side c "<<endl; cin>>cside; // all sides equal if(aside==bside && bside==cside) cout << "Equilateral triangle\n"; // at least 2 sides equal else if(aside==bside || aside==cside || bside==cside) cout << "Isosceles triangle\n"; // no sides equal else cout << "Scalene triangle\n"; } 

But I need help with the if and else if statements to determine the type triangle. Our professor has not covered this topic in class.

We use the program Ch 6.3 on Windows.

13
  • 2
    can't resist: Note: look to the Wikipedia page on ‘triangle’ for definitions of these three types of triangles. LOL! Commented Jun 26, 2011 at 21:36
  • Do you understand the three types of triangles? Commented Jun 26, 2011 at 21:36
  • 11
    There should be a fourth possible result: NOT A TRIANGLE. Commented Jun 26, 2011 at 21:37
  • english is my second language this isn't lol for me.. ;) Commented Jun 26, 2011 at 21:38
  • those are the guidelines for the assignment, the professor references wikipedia if that gives you an idea of how bad he is. I simply don't understand anything with double type variables. I am a political science major and this is computer science 101. I have no clue what I'm doing Commented Jun 26, 2011 at 21:40

6 Answers 6

3
if(a==b && b==c) // all sides equal cout << "Equilateral triangle\n"; else if(a==b || a==c || b==c) // at least 2 sides equal cout << "Isosceles triangle\n"; else // no sides equal cout << "Scalene triangle\n"; 
Sign up to request clarification or add additional context in comments.

11 Comments

+1 Although you probably should use std::cout as he is using C++ and does not include cstdio
At least two problems with this: 1. Let a = 2.0, b = 3.0, c = 100.0 This is not a triangle. 2. It's always poor form to use "==" with non-integral values.
Otherwise to check for three numbers to make a triangle do the following (a+b) > c && (a+c) > b && (b+c) > a
if anyone could copy what I have and adjust the code according to their suggested edits, it would truly help. I can't even attempt to act like i know what I'm doing, and I am in this course because I failed logic
Me too, tomorrow if I don't understand this, they'll fail me.
I have no idea why you guys are overcomplicating this so much, it's so obvious what his teacher wants...
|
2

As your professor suggested, you should look at:

http://en.wikipedia.org/wiki/Triangle#Types_of_triangles

You should also look at:

http://www.teacherschoice.com.au/maths_library/trigonometry/solve_trig_sss.htm

Algorithm:

Solve for all angles, a1, a2, a3 (see the article above) If you can't find a solution: Output "Error: Not a valid triangle" Else: If (a1 == a2) && (a2 == a3): Output "EQUILATERAL" and stop If (a1 == a2) || (a2 == a3) || (a1 == a3): Output "ISOSCELES" and stop Output "SCALENE" and stop 

Also note: Be careful about "equality" with floating point (float/double) values (such as angles). If you are doing such a comparison, you should usually use this instead:

abs(x - y) < epsilon 

Where epsilon is a "sufficiently small value".

5 Comments

Note this is intentionally not C++.
am i supposed to enter this below each cin statement?
@jason: Which part? I'd recommend making a function for comparing floats, if that's what you're asking.
i don't know what I'm asking, I have no resources, knowledge or guidance on this topic. the extent of my knowledge and understanding of programming is what I have above
@jason: Oh. Yeah, this whole thing is after all the std::cin >> statements. But check out Owen's answer. It didn't occur to me that you don't even need to know the angles :)
2

The logic falls out neatly from the definition of these different types of triangles, which as the professor notes, is information readily obtained from Wikipedia. It just involves a simple comparison of side lengths; you don't have to go as far as angles. But I'll give you some help with the "not a triangle" condition. Don't be afraid to put on your math hat here and go wandering in, a little logic isn't a bad thing for a poli sci student to endure every now and then. :-)

For the sides to make a proper triangle, for each pair of sides (I'll call them f and g), they must add up to greater than the third side's length (I'll call it h). If you're dealing with equilateral triangles, you automatically know this condition is met (why?). If you're dealing with isosceles or scalene triangles, you technically only need to check the smaller two sides against the largest side, and if it's true for them, it's true for the other two cases as well (why?). However, it may be just as convenient for you to check all three cases.

Looking at why this inequality has to hold: if the sum of two sides was exactly equal to the third side's length, you'd have a "degenerate" triangle where sides f and g could only lay on top of h! If they added up to less, the two sides could connect to the endpoints of h but then would never meet at a third point! You can test this yourself by cutting lengths of string or strips of paper and trying it out.

Three other things to think about:

  1. In C++, double and float are not the same thing. One has less precision than the other. Make sure you use the one the professor asks for.
  2. Checking to make sure the sides are non-negative is a great idea. You could probably reasonably rule out lengths of 0 as well, to eliminate the possibility of degenerate triangles that just look like line segments or points.
  3. When comparing floating-point numbers, you should always be careful to consider whether a strict equality is going to get you what you want. For checking the equilateral/isosceles/scalene conditions, you're fine because the user is directly entering in the floating-point numbers and you're not manipulating them, so there's no chance for you to introduce error. But when checking the "not a triangle" condition, it's relatively easy to set up a situation where adding the two sides rounds off (because of the vicissitudes of floating-point arithmetic in the CPU) to something that's very close to, but not quite exactly, the third side. In those cases, if you want to catch degenerate triangles, what you usually do is pick an "epsilon" value (some very small value relative to the numbers you're dealing with) that represents the maximum amount of roundoff you're willing to tolerate. You then check whether the sum of f and g is somewhere between h - epsilon and h + epsilon – or put another way, whether the absolute value of f + g - h is less than or equal to epsilon. If it is, you claim that f + g = h (as best as you can tell) and deal with the degenerate case.

2 Comments

+1; It didn't occur to me that the angle's equality was implied by the length's equality.
In fact, in geometry these things were taught to me in terms of side lengths (hence the name equilateral, not equiangular!), and we derived the corresponding equality of angles as a theorem. But you can think of it in whatever way is most convenient for the particular situation you're in. :-)
1

To complete this program you will need the following:

Make sure input is valid. In this case, input must be greater than 0. You could catch your input using a loop like

 while (invar <= 0) { cout<<"Enter length"<<endl; cin>>invar; if (invar <= 0) { cout<<"invalid input"<<endl; } } 

I am not sure if this is proper c++ syntax, I haven't used it in about 8 years. you can do this for all 3 inputs. I would probably make a function to determine the triangle using 3 input variables and 1 return variable. The following is pseudo-code

if (a + b <= c) or (a + c <= b) or (b + c <= a) { return "you don't have a triangle." } else { if (a == b) or (a == c) or (b == c) { if (a == b and b == c) { return "equilateral" } return "isosceles" } return "scalene" } return -1 

2 Comments

thanks very much, this is the type explanation i've been needing. I don't have to understand the format for a test, I simply need the code to match the guidelines. Thanks
@jason This code is not meant to be the best algorithm, or in proper c++ syntax, but as long as it helps, I do not want to do all of it for you.
0
#include<stdio.h> #include<ctype.h> #include<conio.h> #include<math.h> int main() { float Side1,Side2,Side3; float Flag1,Flag2,Sum_of_sq1,Sum_of_sq2,Sum_of_sq3; clrscr(); printf("Enter Three Sides Side1 Side2 Side3 :"); scanf("%f %f %f", &Side1 , &Side2 , &Side3); Flag1=(Side1==Side2)?(Side2==Side3?1:0):((Side2==Side3)?0:-1); if(Flag1==0) { printf("Triangle is Isoceles\n"); } if (Flag1==1) { printf("Equilateral Triangle"); } Sum_of_sq1=pow(Side1,2)+pow(Side2,2); Sum_of_sq2=pow(Side1,2)+pow(Side3,2); Sum_of_sq3=pow(Side2,2)+pow(Side3,2); if (sqrt(Sum_of_sq1)==Side3 ||sqrt(Sum_of_sq2)==Side2 || sqrt(Sum_of_sq3)==Side1) printf("The Triangle is Right Angled Triangle"); getch(); return(0); } 

Comments

0
#include<iostream> using namespace std; //create a class class Triangle { //declare three sides for the triangle double side1; double side2; double side3; public: //constructor to initialize the data members Triangle(double s1, double s2, double s3) { side1 = s1; side2 = s2; side3 = s3; } void triangleType() { //all sides equal if((side1 == side2)&&(side2 == side3)) cout << "It is an Equilateral Triangle" << endl; //at least two sides are equal else if((side1 == side2) || (side2 == side3) || (side1 == side3)) cout << "It is an Isosceles Triangle" << endl; //all are different else cout << "It is a Scalene Triangle" << endl; } }; int main() { //local variable double a_side, b_side, c_side; //taking the user inputs cout << "Enter the three sides of a triangle: " << endl; cin >> a_side >> b_side >> c_side; Triangle t1(a_side, b_side, c_side); //create an object of Triangle t1.triangleType(); //call the function return 0; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.