My program only runs the first if?

Asked

Viewed 255 times

-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;  
}  
  • 1

    The correct is isósceles and not isoceles.. #valorizeoportugues

  • 2

    How do you know that it only enters the first IF if if you have two IF’s with the same answer?

  • 1

    Only one detail: the function returns int, but the variable "tri" is float type.

2 answers

0

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

The condition:

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

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

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

0


The case in which 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;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.