Logical error when using If / else

2

I'm trying to get my program to identify a triangle and tell me if it's Equilateral, isosceles or scalene. I am a beginner in the C language, so I created a structure with If / Else to analyze the conditions to identify if the value entered corresponds to a triangle, if yes tell me which type of triangle corresponds and if it does not correspond to a triangle inform that the data entered do not match.

But my problem is that any value entered the program says that the value does not correspond to a triangle.

I wonder if anyone can help me understand my error ??

int main () {

    //VARIAVEIS

    float A = 0;
    float B = 0;
    float C = 0;


    printf ("\nCALCULANDO UM TRIANGULO!\n");
    printf ("\nDIGITE OS VALORES DOS CATETOS:\n");
    getch();

    printf ("\nDIGITE O VALOR DE (A):\n");
    scanf("%f", &A);
    printf ("\nDIGITE O VALOR DE (B):\n");
    scanf("%f", &B);
    printf ("\nDIGITE O VALOR DE (C):\n");
    scanf("%f", &C);

    printf ("\nOS VALORES DIGITADOS: %.2fcm %.2fcm %.2fcm\n", A , B , C);
    printf ("\nPRESSIONE ENTER...\n");
    getch ();

        if ((A + B < C) || (B + C < A) || (A + C < B)) {

            printf ("\nVAMOS CALCULAR O TRIANGULO\n");


            if ((A == B) && (B == C)) {

                printf ("\nTRIANGULO EQUILATERO!\n");

            }
            else {

                if((A == B) || (A == C) || (B == C)) {

                    printf ("\nTRIANGULO ISOCELES!\n");

                }
                else {

                    printf ("\nTRIANGULO ESCALENO!\n");

                }

            }

        }
        else {

            printf ("\nVALORES NAO CORRESPONDEM A UM TRIANGULO!\n");

        }
    
asked by anonymous 04.10.2018 / 23:48

5 answers

3

There will only be one triangle if only its sides obeyed the following rule: one of its sides must be greater than the absolute value (modulus) of the difference of the other two sides and less than the sum of the other two sides . See the rule summary below:

| b – c | < a < b + c

| a – c | < b < a + c

| a – b | < c < a – b

With the measures a = 5cm, b = 10cm and c = 9cm, can we form a triangle?

|10 – 9| < 5 < 10 + 9 
1 < 5 <19 (VERDADEIRO) 

|9 – 5| < 10 < 9 + 5 
4 < 10 < 14 (VERDADEIRO)

|5 – 10| < 9 < 10 + 5 
5 < 9 < 15 (VERDADEIRO)
  

See applying these values to your condition:

((A + B < C) || (B + C < A) || (A + C < B))

((5 + 10 < 9) || (10 + 9 < 5) || (5 + 9 < 10))
  

Totally wrong !!

With the measures 5cm, 10cm and 4cm, can we form a triangle?

|10 – 4| < 5 < 10 + 4 
6 < 5 < 14 (FALSO)
  

When a condition does not obey the rule, a triangle can not exist.

Equilateral: 3 equal sides a == b && b == c

Isosceles: at least two of its sides have equal measures a==b || a==c || b==c

Scalene: 3 different sides a !=b && b !=c

    
05.10.2018 / 01:33
3

I think your whole problem lies in this line:

    if ((A + B < C) || (B + C < A) || (A + C < B)) {

I'd put it like this:

    if ( (C < (A+B)) && (A < (B+C)) && (B < (A+C)) ) {

I put parentheses in operations on account of precedence. In addition the three conditions must be true, since neither side should be greater than the sum of the other two.

Another important point: beware of comparisons with types float . Due to the way the numbers are stored, 3 will not always equal 3;)

Finally, an interesting refinement is not to allow sides equal to zero.

    
05.10.2018 / 00:21
2

This test:

if ((A + B < C) || (B + C < A) || (A + C < B)) {

is invalid to test whether the 3 values can represent a triangle. In a triangle each side is less than the sum of the other two and not larger as you tested. In addition the measure of each side has to be positive. The correct test is:

if ((a > 0) && (b > 0) && (c > 0) && (a<(b+c)) && (b<(a+c)) && (c<(a+b))) {
...
    
05.10.2018 / 01:15
1

This set of if / else is very confusing, I did not quite understand the need for the first if, but one thing that might help you is if / else if where basically if it is not a condition, it may be another condition until finish. Example:

if(Primeira Condição)
{
 Executa esse trecho
} 
else if(Segunda Condição)
{
 Executa esse trecho
}
else < - Quando acabarem todas as condições 
{
 Executa esse trecho
}

I did this in your code for you to see. Note that in the second condition I limit that only the comparison between the variable C must be different to be able to give the result of an isosceles triangle. Of course, you can put more conditions with the "& &" (E) and "||" (OR). Take a look:

#include <stdio.h>
#include <stdlib.h>

int main () {

//VARIAVEIS

float A = 0;
float B = 0;
float C = 0;


printf ("\nCALCULANDO UM TRIANGULO!\n");
printf ("\nDIGITE OS VALORES DOS CATETOS:\n");
getch();

printf ("\nDIGITE O VALOR DE (A):\n");
scanf("%f", &A);
printf ("\nDIGITE O VALOR DE (B):\n");
scanf("%f", &B);
printf ("\nDIGITE O VALOR DE (C):\n");
scanf("%f", &C);

printf ("\nOS VALORES DIGITADOS: %.2fcm %.2fcm %.2fcm\n", A , B , C);
printf ("\nPRESSIONE ENTER...\n");
getch ();

 printf ("\nVAMOS CALCULAR O TRIANGULO\n");

 if(A == B && A == C && C == B)
 {
     printf("\nTriângulo Equilatero");
 } else if(A == B && C != A && C != B)
 {
     printf("\nTriângulo Isósceles");
 }
 else
 {
     printf("Triângulo Escaleno");
 }
 }
    
05.10.2018 / 00:23
1

I gave one a search. You do not exactly need a calculation if it's just the verification. If in doubt just ask.

#include <stdlib.h>
#include <stdio.h>

void calcula_triangulo(int A, int B, int C){
printf ("\nVAMOS CALCULAR O TRIANGULO\n");

if (A == B && B == C) {
printf ("\nTRIANGULO EQUILATERO!\n");
}
  else if(A == B && A != C && B != C) // Agora entra na condição {
  printf ("\nTRIANGULO ISOCELES!\n");
 }
    else {
    printf ("\nTRIANGULO ESCALENO!\n");
}


}




main(){
float A=0;
float B=0;
float C=0;


printf ("\nCALCULANDO UM TRIANGULO!\n");
printf ("\nDIGITE OS VALORES DOS CATETOS:\n");
getch();

printf ("\nDIGITE O VALOR DE (A):\n");
scanf("%f", &A);
printf ("\nDIGITE O VALOR DE (B):\n");
scanf("%f", &B);
printf ("\nDIGITE O VALOR DE (C):\n");
scanf("%f", &C);

printf ("\nOS VALORES DIGITADOS: %.2fcm %.2fcm %.2fcm\n", A , B , C);
printf ("\nPRESSIONE ENTER...\n");
calcula_triangulo(A,B,C);

}

    
05.10.2018 / 00:34