Post a New Question

programming

posted by on .

Write a program that asks for the length and width of two rectangles. The program should tell the user which rectangles has the greater area, or if the area are the same.

  • programming - ,

    We do not write code here.

    If you post your code, or have questions on how to code, we'd be glad to help review your code or help you debug.

    Also, as there are many programming languages, it would help to specify the language that you are suppose to use.

  • programming - ,

    #include <iostream>
    using namespace std;

    class Rectangle
    {
    private:
    int width, height;
    public:
    Rectangle(int width=0, int height=0) : width(width), height(height) { }
    int getWidth() {
    return width;
    }
    int getHeight() {
    return height;
    }
    };

    istream& operator>> (istream& is, Rectangle& r) {
    is >> r.width >> r.height;
    return is;
    }

    ostream& operator<< (ostream& os, Rectangle& r) {
    os << r.getWidth() << " " << r.getHeight();
    return os;
    }

    Rectangle operator+ (Rectangle& r1, Rectangle& r2) {
    int w = r1.getWidth() * r2.getHeight() + r2.getWidth() * r1.getHeight();
    int h = r1.getWidth() * r2.getHeight();
    return Rectangle(w, h);
    }

    Rectangle operator- (Rectangle& r1, Rectangle& r2) {
    int w = r1.getWidth() * r2.getHeight() - r2.getWidth() * r1.getHeight();
    int h = r1.getWidth() * r2.getHeight();
    return Rectangle(w, h);
    }

    bool operator< (Rectangle& r1, Rectangle&r2) {
    return r1.getWidth() * r2.getHeight() < r2.getWidth() * r1.getHeight();
    }

    bool operator== (Rectangle& r1, Rectangle&r2) {
    return r1.getWidth() * r2.getHeight() = r2.getWidth() * r1.getHeight();
    }


    int main() {
    int w, h;

    for (int i=0; i<3; i++) {
    cout << "Enter width and height of the first rectangle: ";
    cin >> w >> h;
    Rectangle r1(w,h);

    cout << "Enter width and height of the second rectangle: ";
    cin >> w >> h;
    Rectangle r2(w,h);

    if (r1 == r2)
    cout << "The area of both rectangles are the SAME!" << endl << endl;
    else if (r1 > r2)
    cout << "The area of the first rectangle is LARGER!" << endl << endl;
    else
    cout << "The area of the first rectangle is SMALLER!" << endl << endl;
    }
    return 0;
    }

Answer This Question

First Name:
School Subject:
Answer:

Related Questions

More Related Questions

Post a New Question