I cannot return the message saying if the values form a rectangle triangle

Asked

Viewed 36 times

0

#include<stdio.h>
#include<stdlib.h>
#include<locale.h>
#include<conio.h>
#include<math.h>

int retangulo(int a, int b, int c){


    int maior;

    if(a>b && a>c){// iníco primeira verificação...

        maior = a;

        if( pow(maior,2) == pow(b,2) + pow(c,2) ){

            printf("\n Verdadeiro...");
        }

    }// fim primeira verificação...

    else if(b>a && b>c){// iníco segunda verificação...

            maior = b;

            if( pow(maior,2) == pow(a,2) + pow(c,2) ){

                printf("\n Verdadeiro...");

             }

        }// fim segunda verificação...

        else if(c>a && c>b){// iníco terceira verificação...

                 maior = c;

                if( pow(maior,2) == pow(a,2) + pow(b,2) ){

                    printf("\n Verdadeiro...");

                 }

            }// fim terceira verificação...

            else{

                printf("\n Falso...");
            }
}

int main(){
    setlocale(LC_ALL,"ptb");

    int l1, l2, l3, X;

    printf("\n Digite quatro valores inteiros:\n");
    scanf("%d%d%d", &l1, &l3, &l3);

    X = retangulo(l1,l2,l3);


    getch();
    return 0;
}
  • Take care of equality comparisons for float/double variables. As approximate values may be the comparison may not be true when you judge it to be. Use of round function can eliminate this possible problem.

1 answer

1

I debugged your code and found the following problems:

  1. Your scanf in the method main() is not reading the proper variables. You are reading l1, l3 and l3, when you should be reading l1, l2, l3; the consequence of this is that the l2 (assumed in b) is not read, and therefore the program is not able to do the checks in the method retangulo();
  2. Your if nested (which verifies the Pythagorean theorem) does not have a path in case the condition is false. In other words, the program checks one of the if else if, and once the code is in the if nested that I mentioned above, it has no escape, but to give a "break" and exit the method, and that is why the program finishes its execution without printing anything. Therefore, to get around this problem and get the expected result of verification, it is necessary that the "False..." message is inside the if nestled.

Note: The program will only execute the else { printf("\n Falso..."); } that you did there at the end (before the main()) when none of the if (a > b && a > c) {}, etc., is true.

I will leave below the fix of your code:

#include<stdio.h>
#include<stdlib.h>
#include<locale.h>
#include<conio.h>
#include<math.h>

int retangulo(int a, int b, int c){


    int maior;

    if(a>b && a>c){// iníco primeira verificação...

        maior = a;

        if(pow(maior,2) == pow(b,2) + pow(c,2)){

            printf("\n Verdadeiro...");
        }
        else{

            printf("\n Falso...");
        }

    }// fim primeira verificação...

    else if(b > a && b > c){// iníco segunda verificação...

        maior = b;

        if(pow(maior,2) == pow(a,2) + pow(c,2)){

            printf("\n Verdadeiro...");

        }
        else{

            printf("\n Falso...");
        }

    }// fim segunda verificação...

    else if(c > a && c > b){// iníco terceira verificação...

        maior = c;

        if(pow(maior,2) == pow(a,2) + pow(b,2)){

            printf("\n Verdadeiro...");

        }
        else{

            printf("\n Falso...");
        }

    }// fim terceira verificação...


}

int main(){
    setlocale(LC_ALL,"ptb");

    int l1, l2, l3, X;

    printf("\n Digite quatro valores inteiros:\n");
    scanf("%d%d%d", &l1, &l2, &l3);

    X = retangulo(l1,l2,l3);

    return 0;
}

Browser other questions tagged

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