-1
I need to work out an algorithm that takes 3 real values and checks whether these values can be the side lengths of a triangle, and in this case, return what kind of triangle is formed.
Why x
, y
and z
form a triangle and it is necessary that the following property be satisfied: the length of each side of a triangle is less than the sum of the length of the other two sides.
Whatever value I put in my algorithm it responds like scalene which is the first if
, what’s wrong with him?
#include <stdio.h>
int func(float x, float y, float z)
{
float a,b,c;
float tri;
a=x+y;
b=x+z;
c=y+z;
if(a>z || b>y || c>x)
{
if(x==y && x==z && y==z)
{
tri=1;
}
else
{
if((x==y && y==z) || (x==z && y==z) || (x==y && x==z))
{
tri=2;
}
if (x!=y && x!=z && y!=z )
{
tri=3;
}
}
}
return tri;
}
int main()
{
float x,y,z;
float tri;
printf("Informe 3 valores reais: \n");
scanf("%f %f %f",&x,&y,&z);
tri=func(x,y,z);
if (tri==1)
{
printf("O triangulo eh escaleno.\n");
}
if(tri==2)
{
printf("O triangulo eh isoceles\n");
}
if(tri==3)
{
printf("O triangulo eh escaleno\n");
}
return 0;
}
The correct is
isósceles
and notisoceles
.. #valorizeoportugues– Sam
How do you know that it only enters the first IF if if you have two IF’s with the same answer?
– Sam
Only one detail: the function returns int, but the variable "tri" is float type.
– G. Bittencourt