Thread: Pythagoras

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    33

    Pythagoras

    C programming:
    I want to creat a program that asks the user to input the integer lengths of three sides of a triangle, and if so whether the triangle is a right-angled, acute or obtuse one. i am having some doubts about the if, else if statements. I never seem to know how and in what order to use them (could maybe someone explain this to me please) What am i doing wrong here??

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    int main(int argc, char *argv[]){
      int SideA;
      int SideB;
      int SideC;           /*sideC = Hypotenuse*/
    
        printf("Type in the integer lengths of 3 sides of a triangle:\n");
        scanf("%d %d %d", &SideA, &SideB, &SideC); 
        printf("%d %d %d\n", &SideA, &SideB, &SideC);
        
        {
           
         if (SideA*SideA + SideB*SideB == SideC*SideC){
        printf("This is a right-angled triangle.\n");
        }
         
         else if  (SideC*SideC < SideA*SideA + SideB*SideB){
        printf("This is an acute-angled triangle.\n");
         }
           
         else if(SideC*SideC > SideA*SideA + SideB*SideB){
        printf("This is an obtuse-angled triangle.\n");
         }
        
         else {
         printf("This is not a triangle\n");
           
        }
         
         return 0;
         
         }
    Last edited by Se7enUp; 09-06-2013 at 04:02 AM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Unless I've missed something on my quick look, your code won't compile since it is hasn't matched curly braces properly.

    You need to ensure all three lengths exceed zero, and then sort them. The longest side of a triangle is always opposite the largest angle. The shortest side is always opposite the smallest angle.

    If the longest length is greater than or equal to the sum of the other two lengths, the set don't correspond to a triangle. You're not checking for that and need to BEFORE anything else.

    If the length of the longest side squared equals the sum of lengths of the other two, it is a right angle triangle. If the longest side squared is less than the sum of squares of the other two, the triangle is acute. Otherwise it is obtuse.

    You might also want to check for isosceles and equilateral triangles. Note that isosceles triangles may be either acute or obtuse. An equilateral triangle (all three sides equal) is acute by definition.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User Stormboy's Avatar
    Join Date
    Aug 2013
    Location
    Planet Earth
    Posts
    12
    Since you don't know if/else statements clearly here is a quick on-the-spot guide/tutorial:
    if and else statements are used when the expected input/output may be different each time and you want your program to act differently depending on the value. The ordering of if/else statements are as the following:
    use if for the first expected value or the value to check for first, followed by else if for the second expected value or the value to check for if the first one is false and else at the last when all if/else if fails.

    And here is a sample:
    Code:
    #include <stdio.h>
    
    int main()
    {
        int userAge;
        printf("Enter your age: ");
        scanf("%d", &userAge);
        if(userAge < 13)
        {
            printf("You are a kid!\n");
        }
        else if(userAge >= 13 && userAge < 18)
        {
            printf("You are a teen but not an adult.\n");
        }
        else if(userAge >= 18 && userAge < 20)
        {
            printf("You are a teen as well as an adult.\n");
        }
        else
        {
            printf("You are an adult and not a teen.");
        }
        getch();
        return 0;
    }
    And now coming to your code.
    Why are you putting the if/else if/else construct inside curly braces? Its not necessary.
    Code:
        {//No need this
    
            
         if (SideA*SideA + SideB*SideB == SideC*SideC){
        printf("This is a right-angled triangle.\n");
        }
          
         else if  (SideC*SideC < SideA*SideA + SideB*SideB){
        printf("This is an acute-angled triangle.\n");
         }
            
         else if(SideC*SideC > SideA*SideA + SideB*SideB){
        printf("This is an obtuse-angled triangle.\n");
         }
         
         else {
         printf("This is not a triangle\n");
            
        }//No need this
    Also, use 4 spaces inside the body of the if/else if/else statements. And you might want to use double/float instead of int so you can enter decimal lengths.

    Try this code:
    Code:
    #include <stdio.h>
    
    void flush_buf(void); //Function that stops crashing if input is different from variable type
    
    int main()
    {
        float A, B, C;
        printf("Enter side A: ");
        scanf("%f", &A);
        flush_buf();
        printf("Enter side B: ");
        scanf("%f", &B);
        flush_buf();
        printf("Enter side C: ");
        scanf("%f", &C);
        flush_buf();
        
        printf("\nA: %.2f, B: %.2f, C: %.2f\n", A, B, C);
        
        if((int)A == 0 || (int)B == 0 || (int)C == 0) //Check whether user entered 0/letters
        {
            printf("\nNot a valid triangle.\n");
        }
        else if(A*A + B*B == C*C)
        {
            printf("\nThe triangle is a right-angled triangle.\n");
        }
        else if(A*A + B*B > C*C)
        {
            printf("\nThe triangle is an acute-angled triangle.\n");
        }
        else if(A*A + B*B < C*C)
        {
            printf("\nThe triangle is an obtuse-angled triangle.\n");
        }
        
        getch();
        return 0;
    }
    
    void flush_buf(void)
    {
        while(getchar() != '\n');
    }
    Last edited by Stormboy; 09-06-2013 at 05:32 AM.

  4. #4
    Registered User
    Join Date
    Sep 2013
    Posts
    33
    Thx Guys! I figured it out doing just minimal, But as you can see on the picture (added with this post), is that test case number 2 is saying that i am still doing something wrong. The hint is; Does your program still return the same answer if you reorder the input? So what is the problem, what should I change? Pythagoras-schermafdruk-van-2013-09-08-21-01-56-jpg Thank u guys in advance. Bare with me, I am new to this.. but lovin it. Doing Artificial Intelligence at the University..

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
     
    int main(){
        int A, B, C;
        
        printf("Type in the integer lengths of 3 sides of a triangle:\n"); 
        scanf("%d %d %d", &A, &B, &C);
      
    
        if((int)A <= 0 || (int)B <= 0 || (int)C <= 0){
            printf("This is not a triangle.\n");
        }
        else if(A*A + B*B == C*C) {
            printf("This is a right-angled triangle.\n");
        }
        else if(A*A + B*B > C*C){
            printf("This is an acute-angled triangle.\n");
        }
        else if (A*A + B*B < C*C){
            printf("This is an obtuse-angled triangle.\n");
        }
         
        return 0;
    }

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    You should reorder A,B and C - so C be the biggest side.

    Also Note that 1,1,5 is not triangle...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by vart View Post
    Also Note that 1,1,5 is not triangle...
    I assume you meant something like (1,1), (5,1), (5,5)? Or (1,1), (2, 1), (5,1)?

    Anyway I essentially agree, altough some may argue otherwise and call them degenerate triangles.

    Degeneracy (mathematics) - Wikipedia, the free encyclopedia

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by SirPrattlepod View Post
    I assume you meant something like (1,1), (5,1), (5,5)? Or (1,1), (2, 1), (5,1)?
    You are mixing 2 different threads. Here input is not coordinates but side lengths
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Se7enUp View Post
    The hint is; Does your program still return the same answer if you reorder the input? So what is the problem, what should I change?
    Read the second sentence of my previous post in this thread.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  9. #9
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by vart View Post
    You are mixing 2 different threads. Here input is not coordinates but side lengths
    De gads you're right! Oops

  10. #10
    Registered User
    Join Date
    Sep 2013
    Posts
    33

    Figured it out, Thx for replies guys!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
     
    int main(){
        int A, B, C;
        int x;
        
        
        printf("Type in the integer lengths of 3 sides of a triangle:\n"); 
        scanf("%d %d %d", &A, &B, &C);
        
        if(A > B && A > C){ /*set largest value to C*/
           x=A;
           A=C;
           C=x;}
        if(B > A && B > C){
           x=B;
           B=C;
           C=x;}
           
       if(((int)A <= 0 || (int)B <= 0 || (int)C <= 0)||(A+B <= C)||(A+C<=B)||(B+C<=A)){  /*Is this a triangle?*/
            printf("This is not a triangle.\n");
        } 
        
        else if(A*A + B*B == C*C){
            printf("This is a right-angled triangle.\n");
        }
        
     
        else if(A*A + B*B > C*C){
            printf("This is an acute-angled triangle.\n");
        }
        
     
        else if (A*A + B*B < C*C){
            printf("This is an obtuse-angled triangle.\n");
        }
         
        return 0;
    }
    Last edited by Se7enUp; 09-10-2013 at 10:21 AM.

  11. #11
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Well done! On it right. Just a few things which you may want to consider

    Code:
    if(((int)A <= 0 || (int)B <= 0 || (int)C <= 0)||(A+B <= C)
    No need to type cast those variables here. They are already int and can only return a whole value

    Code:
    else if(A*A + B*B == C*C){
    Try start using bracket to isolate the expression and to avoid confusions future when evaluating complex expression. The becomes far much easier avoid run time issues in relation to operator precedence.

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  12. #12
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by ssharish2005 View Post
    Try start using bracket to isolate the expression and to avoid confusions future when evaluating complex expression. The becomes far much easier avoid run time issues in relation to operator precedence.
    To the OP: yes, well done!

    @ssharish2005: I actually disagree with the part of your message quoted. When I see expressions like n = (a*b) + c; I literally cringe. n = a*b + c is much easier to read in my opinion

  13. #13
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Well that is your opinion as to how you want to write your expression. I'm not even considering the scenario the OP has has in this thread, consider an other thread on here which i was reading about macro function

    Code:
    #define PRODUCT(x)  (x*x)
    PRODUCT(i+1);
    What's the answer to it? Altho its much better to write x*x ; but the answer you derive at the end is not what you want. It is not just all about readability, it also about the how the expression get evaluated and you can control that by putting the bracket, because ( has more precedence than any other operator. This is how OPs code would look like if he did us the brackets

    Code:
    else if( ( ( A * A ) + ( B * B ) ) == ( C * C ) ) {
    And makes it no hard to read than it was already and it will only can get better in the later stages in writing a quality code and not putting anything in will only yield making it harder and difficult to debug!

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  14. #14
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by ssharish2005 View Post

    Code:
    else if( ( ( A * A ) + ( B * B ) ) == ( C * C ) ) {
    And makes it no hard to read than it was already and it will only can get better in the later stages in writing a quality code and not putting anything in will only yield making it harder and difficult to debug!

    ssharish
    For me it is a lot harder when the original version
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ssharish2005
    consider an other thread on here which i was reading about macro function
    Code:
    #define PRODUCT(x)  (x*x)
    PRODUCT(i+1);
    What's the answer to it? Altho its much better to write x*x ; but the answer you derive at the end is not what you want. It is not just all about readability, it also about the how the expression get evaluated and you can control that by putting the bracket, because ( has more precedence than any other operator.
    In other words, in the case of function-style macros, defensively programming for correctness trumps readability.

    Quote Originally Posted by ssharish2005
    This is how OPs code would look like if he did us the brackets
    Code:
    else if( ( ( A * A ) + ( B * B ) ) == ( C * C ) ) {
    And makes it no hard to read than it was already
    I disagree. It is far more difficult to read as I have to parse all those nested parentheses. It is equally correct as:
    Code:
    else if(A * A + B * B == C * C) {
    which is far more readable, if the reader knows the relative predence of the operators involved, so correctness is not an issue. The pattern of comparing some arithmetic expression is so common that I find it very hard to fathom that anyone past the rank beginner stage would fail to immediately see that the above is equivalent to:
    Code:
    else if((A * A + B * B) == (C * C)) {
    Generally, I think that students are taught some version of operator precedence in mathematics that corresponds to the precedence of the similiar operators in C, so likewise I find it difficult to believe that someone might fail to interpret the above as being equivalent to:
    Code:
    else if((A * A) + (B * B) == C * C) {
    But still, I grant that it is possible, so I would not object to such use of redundant grouping.

    For:
    Code:
    n = a * b + c;
    I find it hard to believe that anyone beyond the beginner stage would both be unaware of the relative precedence and also choose to interpret with right to left associativity for * and + instead, so I would object to:
    Code:
    n = (a * b) + c;
    but for:
    Code:
    n = a + b * c;
    I think this is acceptable, though I wouldn't bother:
    Code:
    n = a + (b * c);
    By the way, do you also write:
    Code:
    x = y + z;
    as:
    Code:
    x = (y + z);
    ? After all, a rank beginner could interpret it as:
    Code:
    (x = y) + z;
    Last edited by laserlight; 09-11-2013 at 09:04 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pythagoras triplets
    By bvsa in forum C Programming
    Replies: 3
    Last Post: 08-08-2011, 02:17 PM
  2. Pythagoras theorm
    By newboy in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2002, 03:21 PM