Triangle types

I want to make a program which takes 3 inputs by the user. Each of those numbers should be the border of a triangle.
There are three main types of triangles:
-triangle rectangle
-acute triangle
-obtuse triangle

The program should say, of which type such triangle could be.

To solve this problem I thought about the following algorithm:
I check the triangle rectangle with pytagoras, the acute if two sites are equal the imposible if adding two sites is greater or equal to the third and the obtuse as "else".
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>

int main(){
    int casos;
    std::cin>>casos;
    
    int d1,d2,d3;
    
    for(int n=0;n<casos;n++){
        std::cin>>d1>>d2>>d3;
        
        if(d1==d2 || d1==d3 || d2==d3){
            std::cout<<"ACUTANGULO"<<"\n";
        }
        else if((d1*d1)+(d2*d2)==(d3*d3) || (d1*d1)+(d3*d3)==(d2*d2) || (d2*d2)+(d3*d3)==(d1*d1)){
            std::cout<<"RECTANGULO"<<"\n";
        }
        else if(d1+d2<=d3 || d1+d3<=d2 || d2+d3<=d1){
            std::cout<<"IMPOSIBLE"<<"\n";
        }
        else{
            std::cout<<"OBTUSANGULO"<<"\n";
        }
    }
    return 0;
}


Why is this program wrong if I upload it to the checker?
Hi Yabi2943993,

Is this the same logic about triangles that you had?
Acute = three angles 90 degrees or less
Obtuse = one angle is greater than 90 degrees
Rectangle Triangle = one angle is equal to 90 degrees

Putting into the program these three angles should show these results:


50
50
80
Acute

100
40
40
Obtuse

45
45
90
Rectangle Triangle


Here is the code that was used to create these results:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>

int main()
{
    int casos;
    std::cout << "Number of triangles: ";
    std::cin >> casos;

    int d1, d2, d3;

    for (int n = 0; n < casos; n++) {
        std::cout << "First Angle: ";
        std::cin >> d1;
        std::cout << "Second Angle: ";
        std::cin >> d2;
        std::cout << "Third Angle: ";
        std::cin >> d3;

        if (d1 + d2 + d3 != 180) {
            std::cout << "Three angles must add to 180" << "\n";
        }
        else if (d1 == 90 || d2 == 90 || d3 == 90) {
            std::cout << "Rectangle Triangle" << "\n";
        }
        else if (d1 <= 90 && d2 <= 90 && d3 <= 90) {
            std::cout << "Acute Triangle" << "\n";
        }
        else if (d1 > 90 || d2 > 90 || d3 > 90) {
            std::cout << "Obtuse Triangle" << "\n";
        }
        else {
            std::cout << "Error, its impossible" << "\n";
        }
    }
    return 0;
}


Is this what you were expecting?

Edit: Changed the word angles to triangles.
Last edited on
https://en.wikipedia.org/wiki/Acute_and_obtuse_triangles

It might be a good idea to make sure you have a triangle in the first palce:
https://en.wikipedia.org/wiki/Triangle_inequality
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <algorithm>

using TRITYPE = int;           // did you intend this? or double?

int main()
{
   TRITYPE d1, d2, d3;
    
   std::cout << "Enter three SIDES: ";
   std::cin >> d1 >> d2 >> d3;

   if ( d1 > d2 ) std::swap( d1, d2 );
   if ( d2 > d3 ) std::swap( d2, d3 );
   if ( d1 > d2 ) std::swap( d1, d2 );
   
   if ( d1 + d2 <= d3 )
   {
      std::cout << "Impossible\n";
   }
   else
   {
      TRITYPE discriminant = d1 * d1 + d2 * d2 - d3 * d3;
      if      ( discriminant < 0 ) std::cout << "Obtuse\n";
      else if ( discriminant > 0 ) std::cout << "Acute\n";
      else                         std::cout << "Right-angled\n";
   }        
}
Well, the Flying Nun Bi-orgese won't find that at all fatuous

Perhaps
1
2
3
if ( d1 + d2 <= d3 )
   {
      std::cout << "Fatuous\n";


:)
Why is this program wrong if I upload it to the checker?

Usually because is anyway wrong even if you don’t upload it — I don’t know if there are exceptions.

Does you code pass the test if you check for impossibility as first case?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>


int main()
{
    int casos;
    std::cin >> casos;

    for (int n {}; n < casos; ++n) {
        int d1, d2, d3;
        std::cin >> d1 >> d2 >> d3;

        if ( d1+d2 <= d3 || d1+d3 <= d2 || d2+d3 <= d1 ) {
            std::cout << "IMPOSIBLE\n";
            continue;
        }

        if ( d1==d2 || d1==d3 || d2==d3 ) {
            std::cout << "ACUTANGULO\n";
            continue;
        }

        if (    (d1*d1) + (d2*d2) == (d3*d3)
             || (d1*d1) + (d3*d3) == (d2*d2)
             || (d2*d2) + (d3*d3) == (d1*d1) )
        {
            std::cout << "RECTANGULO\n";
            continue;
        }

        std::cout << "OBTUSANGULO\n";
    }
    return 0;
}

Thanks eugendakin. But actually the inputs aren't the angles. They are the length of the borders of the triangles. Sorry for expressing myself badly...

Does you code pass the test if you check for impossibility as first case?

Yes, I think there must be a mistake in my algorithm. Thanks enoizat anyway.

Thanks also for your help lastchance. With the discriminant you actually check the triangle on pythagoras, right? So if discriminant=0 the triangle should have one angle which is 90 degrees.
But what do you intend with the swaps you use for checking if it is imposible?

Just tested your program and it works!
Final program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <algorithm>

using TRITYPE = int;    

int main()
{
    TRITYPE casos;
    std::cin>>casos;
   TRITYPE d1, d2, d3;
    
    for(int r=0;r<casos;r++){
   std::cin >> d1 >> d2 >> d3;

   if ( d1 > d2 ) std::swap( d1, d2 );
   if ( d2 > d3 ) std::swap( d2, d3 );
   if ( d1 > d2 ) std::swap( d1, d2 );
   
   if ( d1 + d2 <= d3 )
   {
      std::cout << "IMPOSIBLE\n";
   }
   else
   {
      TRITYPE discriminant = d1 * d1 + d2 * d2 - d3 * d3;
      if      ( discriminant < 0 ) std::cout << "OBTUSANGULO\n";
      else if ( discriminant > 0 ) std::cout << "ACUTANGULO\n";
      else                         std::cout << "RECTANGULO\n";
   }        
    }
    return 0;
}


Thanks to all for your help

Last edited on
Does you code pass the test if you check for impossibility as first case?
Yes, I think there must be a mistake in my algorithm.

Ehm… Does it mean if you test firstly for impossibility the code passes the test or not?
Just curious.
With the discriminant you actually check the triangle on pythagoras, right? So if discriminant=0 the triangle should have one angle which is 90 degrees.
But what do you intend with the swaps you use for checking if it is imposible?


The swaps are intended just to make sure that the lengths are in ascending order d1 <= d2 < d3 before doing anything else. It's easier to apply the remaining logic if you can be sure of which is the longest side and which are the other two.

My logic for triangle type actually came from applying the cosine rule (in reverse) for the angle between the two smallest sides. This will determine whether the angle is obtuse, acute or right-angled (cosine is negative, positive or zero). It generalises Pythagoras. It comes down to the same conditions as in the YouTube video that @MathHead200 drew your attention to, which reaches the same principle in a more informal way.
Last edited on
No it does not pass, enoizat
Thanks lastchance!
Last edited on
Topic archived. No new replies allowed.