Thread: Need help with "triangle" program.

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    5

    Need help with "triangle" program.

    Hi. I'm doing my first lab program for my C class, which is due this tuesday:

    "Input 3 positive integers from the terminal, determine if they are valid sides of a triangle. If the sides do form a valid triangle, output the type of triangle - equilateral, isosceles, or scalene- and the area of the triangle. If the 3 integers are not the valid sides of a triangle, output an appropriate message and the 3 sides. be sure to comment and error check in your program."

    Use: s = (a + b + c)/2.0
    Area = sqrt(s*(s-a)*(s-b)*(s-c))
    do while loop
    char answer

    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    int main ( void )
    {
        char answer;
        char y = 1;
        char n = 0;
        int a, b, c;
    
    
        do {
            printf("Enter 3 positive integers (separated by 1 space) for the triangle: \n");
            scanf("%d %d %d%*c", &a, &b, &c);
    
    
            if((a + b) > c || (b + c) > a || (a + c) > b)
            {
                if(a == b && b == c)
                {
                    double s = (a + b + c)/2.0;
                    double area = sqrt(s*(s - a)*(s - b)*(s - c));
                    printf("This is an equilateral triangle.\n");
                    printf("The area of this triangle is %d.", area);
                }
                else if((a > b && b == c) || (b > a && a == c) || (c > a && a == b) ||
                        (a < b && b == c) || (b < a && a == c) || (c < a && a == b))
                {
                    double s = (a + b + c)/2.0;
                    double area = sqrt(s*(s - a)*(s - b)*(s - c));
                    printf("This is an isosceles triangle.\n");
                    printf("The area of this triangle is %d.", area);
                }
                else
                {
                    double s = (a + b + c)/2.0;
                    double area = sqrt(s*(s - a)*(s - b)*(s - c));
                    printf("This is a scalene triangle.\n");
                    printf("The area of this triangle is %d.", area);
                }
            }
            else
            {
                printf("This is an invalid triangle.\n");
                printf("The 3 sides you have entered are: %d, %d and %d.", a, b, c);
            }
            printf("Continue? y/n.\n");
            scanf("%c", &answer);
        }
        while ( answer != 0 );
    
    
        return 0;
    }
    After i build and run it, this is what i get:

    Enter 3 positive integers (separated by 1 space) for the triangle:
    1 2 3
    This is a scalene triangle.
    The area of this triangle is 0.Continue? y/n.
    y
    Enter 3 positive integers (separated by 1 space) for the triangle:
    0 0 0
    This is an invalid triangle.
    The 3 sides you have entered are: 0, 0 and 0.Continue? y/n.
    n
    Enter 3 positive integers (separated by 1 space) for the triangle:
    0 1 1
    This is an isosceles triangle.
    The area of this triangle is 0.Continue? y/n.
    n
    Enter 3 positive integers (separated by 1 space) for the triangle:
    2 3 4
    This is a scalene triangle.
    The area of this triangle is 83589923.Continue? y/n.


    I'm getting incorrect areas and when i hit 'n', the program doesn't end. The only correct output i'm getting is what kind of triangle it is (when i enter positive integers). What am I doing wrong in my coding? Please help me...

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    For starters, heed your compiler warnings. This is what I got when I built your code.

    Code:
    main.c||In function 'main':|
    main.c|25|warning: format '%d' expects type 'int', but argument 2 has type 'double'|
    main.c|33|warning: format '%d' expects type 'int', but argument 2 has type 'double'|
    main.c|40|warning: format '%d' expects type 'int', but argument 2 has type 'double'|
    main.c|9|warning: unused variable 'n'|
    main.c|8|warning: unused variable 'y'|
    ||=== Build finished: 0 errors, 5 warnings ===|
    The first three tell you that you're using the wrong format specifier in your "printf()" statements.

    --------

    I haven't done a thorough analysis of your code, but glancing at the first case, it seems your equations are wrong. I'm rusty with trig, but at least I can show you where your program is going wrong for the first case.

    Enter 3 positive integers (separated by 1 space) for the triangle:
    1 2 3
    Code:
    /*
        a = 1
        b = 2
        c = 3
    */
    double s = (a + b + c)/2.0;
    /*
        s = (1 + 2 + 3) / 2 = 3
    */
    double area = sqrt(s*(s - a)*(s - b)*(s - c));
    /*
        area = sqrt(s*(s - a)*(s - b)*(s - c));
             = sqrt(3*(3 - 1)*(3 - 2)*(3 - 3));
             = sqrt(3*(  2  )*(  1  )*(  0  ));
             = sqrt( 0 );
        area = 0
    
    */
    printf("This is a scalene triangle.\n");
    printf("The area of this triangle is %d.", area);
    --------

    ...and when i hit 'n', the program doesn't end
    Code:
    printf("Continue? y/n.\n");
    scanf("%c", &answer);
    while ( answer != 0 );
    1. You prompt for input
    2. You scan for a character input
    3. So why are you checking the user input against zero?

  3. #3
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    I can show you where your program is going wrong for the first case.
    If you have sides 1, 2 and 3, your area would be 0

    Get 3 bits of paper, make the correct ratios and you will see that the 1 and 2 sides make a straight line back across the side 3


    Should these be "&&"?
    Code:
    if((a + b) > c || (b + c) > a || (a + c) > b)
    Consider the famous triangle 3/4/5

    (3+4)>5 && (4+5)>3 && (5+3)>4
    Fact - Beethoven wrote his first symphony in C

  4. #4
    Registered User
    Join Date
    Feb 2013
    Posts
    5
    Quote Originally Posted by Matticus View Post
    The first three tell you that you're using the wrong format specifier in your "printf()" statements.
    Ah, thanks thanks.

    Quote Originally Posted by Click_here View Post
    If you have sides 1, 2 and 3, your area would be 0

    Get 3 bits of paper, make the correct ratios and you will see that the 1 and 2 sides make a straight line back across the side 3


    Should these be "&&"?
    Code:
    if((a + b) > c || (b + c) > a || (a + c) > b)
    Consider the famous triangle 3/4/5

    (3+4)>5 && (4+5)>3 && (5+3)>4
    "...your area would be 0". OH! I didn't realize that. I thought that really was a mistake.

    and you're right, they should be && instead of ||. Thanks!

    Quote Originally Posted by Matticus View Post
    Code:
    printf("Continue? y/n.\n");
    scanf("%c", &answer);
    while ( answer != 0 );
    1. You prompt for input
    2. You scan for a character input
    3. So why are you checking the user input against zero?
    thanks. i fixed that. now i don't think i have any more problems with my program except for ending it by typing "n". i decided to use floats instead of double since that's more convenient:

    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    int main ( void )
    {
        char answer;
        char y = 1;
        char n = 0;
        float a, b, c;
    
    
        do {
            printf("Enter 3 positive integers (separated by 1 space) for the triangle: \n");
            scanf("%f %f %f%*c", &a, &b, &c);
    
    
            if((a + b) > c && (b + c) > a && (a + c) > b)
            {
                if(a == b && b == c)
                {
                    float s = (a+b+c)/2;
                    float area = sqrt(s*(s-a)*(s-b)*(s-c));
                    printf("This is an equilateral triangle.\n");
                    printf("The area of this triangle is %.3f. ", area);
                }
                else if((a > b && b == c) || (b > a && a == c) || (c > a && a == b) ||
                        (a < b && b == c) || (b < a && a == c) || (c < a && a == b))
                {
                    float s = (a+b+c)/2;
                    float area = sqrt(s*(s-a)*(s-b)*(s-c));
                    printf("This is an isosceles triangle.\n");
                    printf("The area of this triangle is %.3f. ", area);
                }
                else
                {
                    float s = (a+b+c)/2;
                    float area = sqrt(s*(s-a)*(s-b)*(s-c));
                    printf("This is a scalene triangle.\n");
                    printf("The area of this triangle is %.3f. ", area);
                }
            }
            else
            {
                printf("This is an invalid triangle.\n");
                printf("The 3 sides you have entered are: %f, %f and %f. ", a, b, c);
            }
            printf("Continue? y/n.\n");
            scanf("%c", &answer);
        }
        while ( answer != n );
    
    
        return 0;
    }
    so now, even when i type "n", the program doesn't end. Can anyone help me with this last problem with my program?

  5. #5
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    'n' not n. Literal characters must have the single quotes around them.

  6. #6
    Registered User
    Join Date
    Feb 2013
    Posts
    5
    Quote Originally Posted by Tclausex View Post
    'n' not n. Literal characters must have the single quotes around them.
    Thanks! It works perfectly now.

  7. #7
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    OH! I didn't realize that. I thought that really was a mistake.
    I think that a better approach would be to have pre-calculated inputs, not just random numbers - That way you can actually say whether your code is behaving correctly or not.
    Fact - Beethoven wrote his first symphony in C

  8. #8
    Registered User
    Join Date
    Feb 2013
    Posts
    5
    Quote Originally Posted by Click_here View Post
    I think that a better approach would be to have pre-calculated inputs, not just random numbers - That way you can actually say whether your code is behaving correctly or not.
    Yeah, i'll keep that in mind. thanks again.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Don't duplicate code unnecessarily. Lines 22 & 23 can be move up to where lines 20 & 21 are, then you wont need lines 30, 31, 37, & 38.
    Similarly, line 25 can be move to after the if-statements to about where line 41 is now.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    Registered User
    Join Date
    Feb 2013
    Posts
    5
    Quote Originally Posted by iMalc View Post
    Don't duplicate code unnecessarily. Lines 22 & 23 can be move up to where lines 20 & 21 are, then you wont need lines 30, 31, 37, & 38.
    Similarly, line 25 can be move to after the if-statements to about where line 41 is now.
    alright. thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 10-12-2012, 06:17 AM
  2. Replies: 2
    Last Post: 08-19-2012, 06:15 AM
  3. Just in case: "Odd" Triangle Challenge (for me)
    By BB18 in forum C Programming
    Replies: 3
    Last Post: 10-09-2004, 12:02 AM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM