0

I am trying to solve a simple problem using C++. The problem statement is this :

N triangles are given. And Each triangle is identified by the coordinates of its three corners in the 2-D cartesian plane. The job is to figure out how many of the given triangles are right triangles. A right triangle is a triangle in which one angle is a 90 degree angle. The vertices of the triangles have integer coordinates and all the triangles given are valid( three points aren't colinear ).

Input : The first line of the input contains an integer N denoting the number of triangles. Each of the following N lines contain six space separated integers x1 y1 x2 y2 x3 y3 where (x1, y1), (x2, y2) and (x3, y3) are the vertices of a triangle.

Output : Output one integer, the number of right triangles among the given triangles.

My C++ Program is this :

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin>>n;

    cout<<"\n";

    int ans = 0;
    for(int i=0;i<n;i++)
    {
        int x1,y1,x2,y2,x3,y3;
        cin>>x1>>y1>>x2>>y2>>x3>>y3;

        double side1 = (double)sqrt( ( (x1-x2) * (x1-x2) ) + ( (y1-y2) * (y1-y2) ) );
        double side2 = (double)sqrt( ( (x2-x3) * (x2-x3) ) + ( (y2-y3) * (y2-y3) ) );
        double side3 = (double)sqrt( ( (x1-x3) * (x1-x3) ) + ( (y1-y3) * (y1-y3) ) );

        double A = side1 * side1;
        double B = side2 * side2;
        double C = side3 * side3;

        cout<<"A = "<<A<<"  B = "<<B<<"  C = "<<C<<"\n";

        cout<<"A+B = "<<A+B<<" , ";
        cout<<"B+C = "<<B+C<<" , ";
        cout<<"A+C = "<<A+C;

        cout<<"\n";

        if( (A + B) == C )
        {
            ans++;
            cout<<"first\n";
        }
        else if( (B + C) == A )
        {
            ans++;
            cout<<"second\n";
        }
        else if( (A + C) == B )
        {
            ans++;
            cout<<"third\n";
        }

        cout<<"\n\n";
    }

    cout<<"ans = "<<ans;

}

Output of above program is this :

enter image description here

But the Correct Output should be ans = 3 , because the first triangle and the last two triangles of the input example are right triangles.

I am not getting why my program is giving wrong output.

SAPJV
  • 115
  • 8

1 Answers1

-2

In this piece of code:

        if( (A + B) == C )
        {
            ans++;
            cout<<"first\n";
        }
        else if( (B + C) == A )
        {
            ans++;
            cout<<"second\n";
        }
        else if( (A + C) == B )
        {
            ans++;
            cout<<"third\n";
        }

your program only enters one code block, the first one whose condition is true. Since you put else if, the block after it will only be entered if the else if condition is true, and the previous condition was false.

Thus, you never increment ans by more than 1.

To solve this, you could do either:

        if( (A + B) == C )
        {
            ans++;
            cout<<"first\n";
        }

        if( (B + C) == A )
        {
            ans++;
            cout<<"second\n";
        }

        if( (A + C) == B )
        {
            ans++;
            cout<<"third\n";
        }

which will only work if more than 1 condition can be true at a time, or this:

        if( (A + B) == C )
        {
            ans = 1;
            cout<<"first\n";
        }
        else if( (B + C) == A )
        {
            ans = 2;
            cout<<"second\n";
        }
        else if( (A + C) == B )
        {
            ans = 3;
            cout<<"third\n";
        }
user826955
  • 3,137
  • 2
  • 30
  • 71