Triangle type

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). Generate test cases for the function assuming another developer coded the function.

C++:
01 void Three_sort(int a, int b, int c, int& max, int& mid, int& min)
02 {
03     if(a>=b){
04         max=a;
05         min=b;
06     }else{
07         max=b;
08         min=a;
09     }
10     if(c>max){
11         mid=max;
12         max=c;
13     }else if((max>=c)&&(c>=min)){
14         mid=c;
15     }else if(c<min){
16         mid=min;
17         min=c;
18     }
19 }
20 
21 int typeTriangle(int a, int b, int c)
22 {
23     int max,mid,min;
24     Three_sort(a,b,c,max,mid,min);
25     if(mid+min<max)
26         return 4;
27     else if(max==min)
28         return 3;
29     else if((max==mid)||(mid==min))
30         return 2;
31     else
32         return 1;
33 }
Advertisements
This entry was posted in Geometry. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s