C++20 sections · 1024 units
Open in Course

Problem - Rectangle Area

Calculate from corners

Define struct Rectangle { Point topLeft; Point bottomRight; };. Write int area(Rectangle r) that calculates area from corners. Area is width times height. Width is the difference between x coordinates: r.bottomRight.x - r.topLeft.x.

Height is difference between y coordinates. Multiply and return. Test with Rectangle rect{{0, 10}, {5, 0}};. Width is 5, height is 10, area is 50. This practices nested structs and chained member access.