Muhammad Ahmed Muhammad Ahmed - 1 month ago 5
C++ Question

Need help solving an assignment

I have the following assignment:


We have the length of four sides. You have to determine if they can form a square. If not, determine if they can form a rectangle. If not, determine if they can form a quadrangle

Hints

Sorting the lengths will help us , so

If L1 == L2 && L2 == L3 && L3 == L4
this will be square

If L1 == L2 && L3 == L4
this will be rectangle

If L1 + L2 + L3 > L4
this will be quadrangle

If it isn't one of them we will call it banana

Input:

The number of test cases, following a line with four positive integer numbers, between 0 and 2^30, the lengths.

Output:

One of four strings: ‘square’, ‘rectangle’, ‘quadrangle’ or ‘banana’.

Sample Input

4
10 8 7 6
9 1 9 1
29 29 29 29
5 12 13 7


Sample Output

quadrangle
rectangle
square



I don't know how to solve this. Here is my code so far:

#include <iostream>

using namespace std;

int main()
{
int L1; int L2; int L3; int L4;
cin>>L1>>L2>>L3>>L4;

cout<<"Enter the 4 lengths";

if(L1 == L2 && L2 == L3 && L3 == L4){
cout<<"You have choosed the square";
}
else if(L1 == L2 && L3 == L4|| L1==L3 && L2==L4){
cout<<"You have choosed the rectangle";
}
else if(L1 + L2 + L3 > L4){
cout<<"You have choosed the quadrangle";
}
else{
cout<<"It's Baanana";
}
return 0;
}

Answer

You are not reading any input from the user, let alone sorting the input. Also, you are not following the first requirement mentioned in the assignment, either (reading the number of test cases).

Try something more like this:

#include <iostream>
#include <algorithm>

using namespace std;

#define L1 Lengths[0]
#define L2 Lengths[1]
#define L3 Lengths[2]
#define L4 Lengths[3]

int main()
{
    int NumTestCases, Lengths[4];

    cout << "How many test cases? ";
    cin >> NumTestCases;

    for(int i = 0; i < NumTestCases; ++i)
    {
        cout << "Enter 4 lengths: ";
        cin >> L1 >> L2 >> L3 >> L4;
        std::sort(Lengths, Lengths+4);

        if ((L1 == L2) && (L2 == L3) && (L3 == L4)) {
            cout << "square" << endl;
        }
        else if (((L1 == L2) && (L3 == L4)) || ((L1 == L3) && (L2 == L4))) {
            cout << "rectangle" << endl;
        }
        else if ((L1 + L2 + L3) > L4) {
            cout << "quadrangle" << endl;
        }
        else {
            cout << "banana" << endl;
        }
    }

    return 0;
}