• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1387
  • Last Modified:

How do I write a function that receives three integer inputs for the lengths of the sides of a triangle using C++?

  How do I write a function that receives three integer inputs for the lengths of the sides of a triangle and returns one of four values to determine the triangle type (1=scalene, 2=isosceles, 3=equilateral, 4=error) using C++?  
   I followed a thread wherein a member posted a question similar to mine; however, the solution was provided using Perl.  And no, this is not homework-related as I have been out of school for many years.  This is what I have come up with so far...
{
...
}
0
LYRivers
Asked:
LYRivers
  • 2
  • 2
  • 2
  • +2
4 Solutions
 
trigger-happyCommented:
Check code snippet

--trigger-happy
int CheckTri(int a, int b, int c){
     //equilateral
     if(a == b && b == c){
          return 3;
     }
     //isosceles
     if(a == b || a == c || b == c){
          return 2;
     }
     //I guess that what you mean by error is if it doesn't form a triangle. Not sure how to check that yet though
     //scalene
     return 1;
}

Open in new window

0
 
oleberCommented:
what means the 4? What kind of error do you need to check?
0
 
evilrixSenior Software Engineer (Avast)Commented:
As far as I remember (it's been a long time since I was at school) it is not possible to make a triangle if the sum of any 2 sides is less than the 3rd so something like below should allow you to test for error.

if (((a + b) < c) | ((b + c) < a) | ((a + c) < b)) {  return 4; }

NB. I have not tested nor proven this formula works so caveat emptor :)
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
Infinity08Commented:
>> What kind of error do you need to check?

Probably if one of the sides has a negative or 0 length ...

Expanding on trigger-happy's code :


#include <cmath>
 
typedef enum {
  TriangleType_scalene = 1,
  TriangleType_isosceles = 2,
  TriangleType_equilateral = 3,
  TriangleType_error = 4
} TriangleType;
 
inline int cmpDouble(double &d1, double &d2) {
  if (fabs(d1 - d2) < 0.00001) return 0;
  if (d1 < d2) return -1;
  else return 1;
}
 
TriangleType checkTriangle(double a, double b, double c) {
    if ((a <= 0.0) || (b <= 0.0) || (c <= 0.0)) {
        return TriangleType_error;
    }
 
    if (!cmpDouble(a, b) && !cmpDouble(b, c)) {
        return TriangleType_equilateral;
    }
 
    if (!cmpDouble(a, b) || !cmpDouble(b, c) || !cmpDouble(a, c)) {
        return TriangleType_isosceles;
    }
 
    return TriangleType_scalene;
}

Open in new window

0
 
Infinity08Commented:
>> if the sum of any 2 sides is less than the 3rd

Yep, that sounds like an error to me too ;) You can easily add that to the code heh.
0
 
oleberCommented:
you can also consider they are all positive;

if  ( (a<0) | (b<0) | (c<0) ) {
   return 4;
}

or to be really crazy

int max = (a>b) ? ( (a>c) ? a : c ) : ( (b>c) ? b : c);
int min = (a<b) ? ( (a<c) ? a : c ) : ( (b<c) ? b : c);

so your error can be

if (min < 0 | 2*max>a+b+c) {
   return 4;
}

I can also get you nicier expressions, but I steel don't know what he considers a error.
0
 
LYRiversAuthor Commented:
Thanks for the responses, everyone.  The error condition is that if it isn't one of the 3 types of triangles listed, then it isn't a triangle; hence, the output  is "error".  Thanks.
0
 
LYRiversAuthor Commented:
I took bits from all of the solutions posted and thus, had to divide the total points.  Thanks again, everyone.
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 2
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now