Search code examples
creturnbooleancs50function-definition

Why is my valid_triangle program from cs50 doesn't work?


I have a problem with this code as I can't seem to find the problem? Here is the problem that I tried to solve: - Declare and write a function called valid_triangle that takes three real numbers representing the lengths of the three sides of a triangle as its arguments, and outputs either true or false, depending on whether those three lengths are capable of making a triangle.

  • Following rules about triangles:
    • A triangle may only have sides with a positive length.
    • The sum of the lengths of any two sides of the triangle must be greater than the length of the third side.
//includes
#include <stdio.h>
#include <cs50.h>

bool valid_triangle(float x, float y, float z);

int main (void)
{
   float x = get_float("x:");
   float y = get_float("y:");
   float z = get_float("z:");
   bool w = valid_triangle(x,  y,  z);
}

bool valid_triangle(float x, float y, float z)
{
    // only positive sides
    if (x <= 0 || y <= 0 || z <= 0)
    {
        return false;
        printf("false\n");
    }

    // sum of the lengths of any two sides of the triangle must be greater than the length of the third side.
    else if (x + y < z || x + z < y || y + z < x)
    {
        return false;
        printf("false\n");
    }
    else
    {
        return true;
        printf("True\n");
    }

    return 0;
}

Solution

  • For starters this if statement

    else if (x + y < z || x + z < y || y + z < x)
    

    is not correct, It should look like

    else if (x + y <= z || x + z <= y || y + z <= x)
    

    And statements after return statements like in this code snippet

        return false;
        printf("false\n");
    

    do not have an effect. It seems you mean

        printf("false\n");
        return false;
    

    Though the calls of printf should not be inside the function.

    You could place only one call of printf in main like

    bool w = valid_triangle(x,  y,  z);
    printf( "%s\n", w == false ? "False" : "True" );
    

    Also the last return statement within the function

    return 0;
    

    is redundant.

    Here is a demonstrative program.

    #include <stdio.h>
    #include <stdbool.h>
    
    bool valid_triangle(float x, float y, float z)
    {
        // only positive sides
        if (x <= 0 || y <= 0 || z <= 0)
        {
            return false;
        }
        // sum of the lengths of any two sides of the triangle must be greater 
        // than the length of the third side.
        else if ( x + y <= z || x + z <= y || y + z <= x)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
    
    int main(void) 
    {
        float a = 4.0f, b = 5.0f, c = 6.0f;
        printf( "%.1f, %.1f, %.1f are sides of a triangle - %s\n", a, b, c,
                valid_triangle( a, b, c ) ? "true" : "false" );
                
        return 0;
    }
    

    The program output is

    4.0, 5.0, 6.0 are sides of a triangle - true