Function for matching points

Asked

Viewed 41 times

0

In an exercise, I need to create a function that returns TRUE if the points are identical. For the points, a structure was implemented as follows:

typedef struct //Estrutura definida para os pontos.
{
    double x; //Coordenadas X e Y.
    double y;
} Ponto;

The function I created was as follows::

int pontoCoincide(Ponto P, Ponto Q)
{
    int coincide = 0;
    if(P.x == Q.x && P.y == Q.y)
        coincide = 1;
    return coincide;
}

I wonder if what I’ve done is right and it’s just that, or if there’s something wrong. Remembering that I need to create the function with exactly these parameters.

1 answer

2


Your reasoning is correct and can be implemented in other ways:

Using ternary operators:

int pontoCoincide( Ponto P, Ponto Q )
{
    return (P.x == Q.x && P.y == Q.y) ? 1 : 0;
}

Or simply:

int pontoCoincide( Ponto P, Ponto Q )
{
    return (P.x == Q.x && P.y == Q.y);
}

Browser other questions tagged

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