Logical error while using If/Else

Asked

Viewed 114 times

1

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

However my problem is that any value typed the program says that the value does not match a triangle.

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

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");

        }
  • Your code seems to be incomplete. Confirm how you have it in your editor and how it was here in the question to match.

  • can send the complete code?

  • Possible duplicate of My program only runs the first if?

5 answers

3

I believe your whole problem lies on this line:

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

I would put it like this:

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

I enclosed the operations on account of precedence. Moreover the three conditions must be true, since no side must be greater than the sum of the other two.

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

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

  • Thank you very much! I managed to solve my problem! Thank you!! Thank you!

3


Only a triangle will exist if, only if, its sides obeyed the following rule: one of its sides must be greater than the absolute value (module) of the difference of the other two sides and smaller than the sum of the other two sides. See the summary of the rule below:

| b – c | < a < b + c

| a – c | < b < a + c

| a – b | < c < a – b

With measurements a=5cm, b=10cm and c=9cm, we can 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, we can form a triangle?

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

When a condition does not obey the rule it is not possible to have a triangle.

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

Isosceles: at least two of their sides have equal measurements a==b || a==c || b==c

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

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 smaller than the sum of the other two and not larger as you tested. Moreover, the measure on 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))) {
...

1

This if/Else set of yours is quite confusing, I don’t quite understand the need for the first if, but one thing that can help you is if/Else if where basically if it’s not a condition, it can be another condition until it’s finished. 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 it in your code for you to see. Note that in the second condition I limit that only the comparison between the variable C needs to be different in order to give the result of an isosceles triangle. Of course, you can put more conditions with the logical operators "&&" (E) and "||" (OU). 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");
 }
 }
  • if A equals B and A equals C that C equals B is unnecessary?

  • Yes, indeed it was unnecessary.

1

I did a search. It doesn’t exactly need a calculation if it’s just the check. If you have any questions 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);

}

Browser other questions tagged

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