Struct with Methods

Lab C++ Beginner 10 points

Give struct Rect { double w, h; } methods area(), perimeter() and isSquare(). Read rectangles and print all three (2 decimals, yes/no).

Input

n, then n lines w h.

Output

n lines.

Example

Input

2
3 4
2.5 2.5

Output

12.00 14.00 no
6.25 10.00 yes
Sample input available Sample output available
Editor
C++

Hints

const after a method means it doesn't change the object.

Solution explanation

Marking read-only methods const lets them be called on const objects.

Show solution
#include <iostream>
#include <iomanip>
using namespace std;

struct Rect {
    double w, h;
    double area() const { return w * h; }
    double perimeter() const { return 2 * (w + h); }
    bool isSquare() const { return w == h; }
};

int main() {
    int n;
    cin >> n;
    cout << fixed << setprecision(2);
    for (int i = 0; i < n; i++) {
        Rect r;
        cin >> r.w >> r.h;
        cout << r.area() << " " << r.perimeter() << " " << (r.isSquare() ? "yes" : "no") << "\n";
    }
    return 0;
}
Output
Answer
HOME LEARN COMMUNITY DASHBOARD