Does my program only execute the first if?

-1

I need to work out an algorithm that receives 3 real values and checks whether these values can be the lengths of the sides of a triangle, and in this case, return the type of triangle formed.

So that 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 and less than the sum of the length of the other two sides

Any value that I put in my algorithm it responds as scalene which is the first if , what's wrong with it?

#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;  
}  
    
asked by anonymous 27.05.2018 / 05:26

2 answers

0

You have not considered the case where x , y and z are not part of a triangle:

int func(float x, float y, float z)
{
    int retorno = 0; // zero é o caso em que não é triângulo

    float somaxy = x + y;
    float somaxz = x + z;
    float somayz = y + z;

    if (somaxy > z || somaxz > y || somayz > x)
    {
        // É um triângulo; verificar de qual tipo agora.
        if (x == y && y == z)
        {
            retorno = 1; // É triângulo equilátero
        }
        else if (x == y || x == z || y == z)
        {
            retorno = 2; // É triângulo isósceles
        }
        else
        {
            retorno = 3; // É triângulo regular
        }
    }

    return retorno;
}

int main(int argc, char* argv[])
{
    float x, y, z;

    printf("Digite os lados do triangulo separados por espacos e, no final, de Enter\n");
    scanf("%f %f %f", &x, &y, &z);

    printf("Os valores informados... ");
    switch (func(x, y, z))
    {
        case 0:
            printf("nao sao de um triangulo!\n");
            break;
        case 1:
            printf("sao de um triangulo equilatero\n");
            break;
        case 2:
            printf("sao de um triangulo isosceles\n");
            break;
        case 3:
            printf("sao de um triangulo regular\n");
            break;
    }

    return 0;
}
    
27.05.2018 / 17:33
0

At the end of the *main()* function: if (tri==1) --- > The triangle is equilateral, and not scalene! Check the printf().

The condition:

if((x==y && y==z) || (x==z && y==z) || (x==y && x==z)) 

is an equilateral triangle test that had already been tested in the previous "if."

For isosceles triangle would be: if(x==y || y==z || x==z) .

    
27.05.2018 / 14:17